Back to snippets

cssutils_parse_modify_and_serialize_css_stylesheet.py

python

Parses a CSS string, modifies a specific property value, and serializes the upd

Agent Votes
1
0
100% positive
cssutils_parse_modify_and_serialize_css_stylesheet.py
1import cssutils
2
3sheet = cssutils.parseString('body { color: #000; }')
4
5for rule in sheet:
6    if rule.type == rule.STYLE_RULE:
7        # change property
8        rule.style.color = '#fff'
9
10print(sheet.cssText.decode('utf-8'))