Python 用cssutils替换@font-face CSS规则中的URI值
我想用替换@font-face CSS规则的URL值 My style.css如下所示:Python 用cssutils替换@font-face CSS规则中的URI值,python,css,python-3.x,Python,Css,Python 3.x,我想用替换@font-face CSS规则的URL值 My style.css如下所示: @font-face { font-family:"Palatino"; font-style:normal; font-weight:normal; src : url("../font/Palatino.ttf"); } p { # some stuff } 还有我的python脚本: #!/usr/bin/env python3 import cssuti
@font-face {
font-family:"Palatino";
font-style:normal;
font-weight:normal;
src : url("../font/Palatino.ttf");
}
p {
# some stuff
}
还有我的python脚本:
#!/usr/bin/env python3
import cssutils
sheet = cssutils.parseFile('style.css')
for rule in sheet:
# Pick up only @font-face rules
if rule.type == cssutils.css.CSSFontFaceRule.FONT_FACE_RULE:
## TODO
## replace the src value
如何将规则规则的URI值替换为类似url(../font/Palatino.otf)格式('opentype')
我希望使用一个cssutils
class方法(),它允许我这样做:class\u方法\u替换url(object,new\u url)
,但我不能真正让它工作
您有什么建议吗?如中所示,您可以迭代规则.style
以查找src
属性,并替换其值。以下是一个例子:
import cssutils
sheet = cssutils.parseFile('style.css')
for rule in sheet:
if rule.type == cssutils.css.CSSFontFaceRule.FONT_FACE_RULE:
for property in rule.style:
if property.name == 'src':
property.value = "url(../font/Palatino.otf) format('opentype')"
print(sheet.cssText.decode())