2015-05-20 30 views
1

我從來沒有在python中使用過cssutils。當我打印一條規則時,我得到下面的字符串,如何編輯python中的css規則?例如,如果我想改變背景顏色?我如何編輯python中的cssutils規則

<cssutils.css.CSSStyleRule object selectorText='#panel' style='color: #eee;\nbackground-color: #000;\nfont-weight: bold;\nheight: 32px' _namespaces={} at 0x7f9b5d76bd30> 

回答

0

你可以簡單地通過改變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.stylecssutils.css.CSSStyleDeclaration類的實例。更多信息在cssutils docs