我从未在 python 中使用过 cssutils。当我打印一条规则时,我得到以下字符串,如何在 python 中编辑 css 规则?例如,如果我想更改背景颜色?
<cssutils.css.CSSStyleRule object selectorText='#panel' style='color: #eee;\nbackground-color: #000;\nfont-weight: bold;\nheight: 32px' _namespaces={} at 0x7f9b5d76bd30>
最佳答案
您可以简单地通过更改rule.style
属性来编辑规则
#!/usr/bin/python
import cssutils
stylesheet = cssutils.parseString(style)
for rule in stylesheet.cssRules:
if rule.type == cssutils.css.CSSRule.STYLE_RULE:
rule.style.background = "#abcdef"
rule.style
是 cssutils.css.CSSStyleDeclaration
类的实例。更多信息在 cssutils docs .
关于python - 我如何在 python 中使用 cssutils 编辑规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30362301/