cssutils is a Python library for parsing and manipulating CSS (Cascading Style Sheets). It provides a full DOM interface for CSS stylesheets, rules, and declarations, allowing for programmatic creation, modification, and serialization of CSS. The library is currently at version 2.11.1 and sees regular maintenance releases, typically every few months.
pip install cssutilsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates parsing a CSS string, accessing and modifying properties within a CSS rule, and creating a new CSS stylesheet programmatically. It also shows how to serialize a `CSSStyleSheet` object back into a CSS string.
Rewrite code to use the main `cssutils` module and its W3C DOM-compliant methods and objects (e.g., `cssutils.parseString()`, `cssutils.CSSStyleSheet()`). Do not use `from cssutils.css import ...`.
Customize serialization preferences using `cssutils.ser.prefs`. For example, `cssutils.ser.prefs.indent = 4` to set indentation or `cssutils.ser.prefs.keepemptyrules = False` to remove empty rules.
Configure the `cssutils.log` level to `INFO` or `DEBUG` to see more detailed parsing messages, especially during development or debugging problematic CSS. Example: `cssutils.log.setLevel(logging.INFO)`.
Install the cssutils library using pip: `pip install cssutils`
First parse the CSS string into a `CSSStyleSheet` object using `cssutils.parseString()`, then pass the resulting sheet object to `cssutils.getUrls()`.
```python
import cssutils
css_string = "@import url('path/to/style.css'); a { color: blue; }"
sheet = cssutils.parseString(css_string)
for url in cssutils.getUrls(sheet):
print(url)
```To suppress these logging messages, configure Python's logging system to a higher level (e.g., ERROR or CRITICAL) for the `cssutils` logger.
```python
import logging
import cssutils
cssutils.log.setLevel(logging.ERROR) # Suppress warnings and info messages
# Or to completely disable cssutils logging for a parser instance:
# parser = cssutils.CSSParser(log=logging.getLogger('null'))
css_text = "@-webkit-keyframes anim { from { opacity: 0; } to { opacity: 1; } } div { -moz-border-radius: 5px; }"
sheet = cssutils.parseString(css_text)
# Your cssutils logic here will now run without these warnings
```Update cssutils to a version compatible with your Python 3 interpreter. This typically involves upgrading the package: `pip install --upgrade cssutils`.
No dependency data recorded yet.