【问题标题】:How do i edit rule with cssutils in python如何在 python 中使用 cssutils 编辑规则
【发布时间】:2015-08-02 10:42:23
【问题描述】:
我从未在 python 中使用过 cssutils。当我打印一条规则时,我得到以下字符串,如何在 python 中编辑 css 规则?例如,如果我想更改背景颜色?
<cssutils.css.CSSStyleRule object selectorText='#panel' style='color: #eee;\nbackground-color: #000;\nfont-weight: bold;\nheight: 32px' _namespaces={} at 0x7f9b5d76bd30>
【问题讨论】:
标签:
python
css
edit
rules
【解决方案1】:
您只需更改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。