{"id":1434,"library":"cssutils","title":"cssutils CSS Parser and Manipulator","description":"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.","status":"active","version":"2.11.1","language":"python","source_language":"en","source_url":"https://github.com/jaraco/cssutils","tags":["css","parser","stylesheet","css-parser","dom"],"install":[{"cmd":"pip install cssutils","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"cssutils","correct":"import cssutils"},{"note":"The 'cssutils.css' module and custom classes were removed in version 2.x. All functionality is now accessed through the main `cssutils` module via the W3C DOM API.","wrong":"from cssutils.css import CSSStyleSheet","symbol":"CSSStyleSheet","correct":"import cssutils\nsheet = cssutils.CSSStyleSheet()"}],"quickstart":{"code":"import cssutils\nimport logging\n\n# Suppress cssutils logging for cleaner output in example\ncssutils.log.setLevel(logging.CRITICAL)\n\n# Parse a CSS string\nsheet = cssutils.parseString('a { color: red; font-size: 16px; }')\n\n# Accessing rules and properties\nrule = sheet.cssRules[0] # Get the first rule\nprint(f\"Selector: {rule.selectorText}\") # Output: Selector: a\nprint(f\"Color: {rule.style.color}\") # Output: Color: red\nprint(f\"Font Size: {rule.style.fontSize}\") # Output: Font Size: 16px\n\n# Modifying a rule\nrule.style.color = 'blue'\n\n# Creating a new stylesheet and adding a rule\nnew_sheet = cssutils.CSSStyleSheet()\nnew_sheet.add('p { margin: 10px; }')\n\n# Add the modified rule from the first sheet to the new one\nnew_sheet.add(rule)\n\n# Serialize the new stylesheet to a string\nprint('\\n--- New Stylesheet ---')\nprint(new_sheet.cssText)","lang":"python","description":"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."},"warnings":[{"fix":"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 ...`.","message":"Major API overhaul in version 2.0.0. All custom API previously found in `cssutils.css` (e.g., `cssutils.css.CSSStyleSheet`, `cssutils.css.Rule`) and other custom classes were removed. All functionality is now exclusively available through the W3C DOM interface.","severity":"breaking","affected_versions":"2.0.0 and later"},{"fix":"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.","message":"Default serialization (`cssText`) might not produce the exact output format expected (e.g., indentation, line breaks, specific property ordering).","severity":"gotcha","affected_versions":"All 2.x versions"},{"fix":"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)`.","message":"cssutils uses Python's `logging` module for warnings and errors during parsing. By default, only `WARNING` and above messages might be shown, potentially masking minor parsing issues.","severity":"gotcha","affected_versions":"All 2.x versions"}],"env_vars":null,"search_vec":"'2.11.1':46 'allow':31 'cascad':16 'creation':34 'css':2,15,26,39,56,60 'css-parser':59 'cssutil':1,6 'current':43 'declar':30 'dom':23,62 'everi':53 'full':22 'interfac':24 'librari':10,41 'mainten':50 'manipul':5,14 'modif':35 'month':55 'pars':12 'parser':3,57,61 'programmat':33 'provid':20 'python':9 'regular':49 'releas':51 'rule':28 'see':48 'serial':37 'sheet':18 'style':17 'stylesheet':27,58 'typic':52 'version':45","created_at":"2026-04-09T03:47:46.835560+00:00","updated_at":"2026-04-16T04:17:58.900465+00:00","problems":[{"fix":"Install the cssutils library using pip: `pip install cssutils`","cause":"The cssutils library is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'cssutils'"},{"fix":"First parse the CSS string into a `CSSStyleSheet` object using `cssutils.parseString()`, then pass the resulting sheet object to `cssutils.getUrls()`.\n```python\nimport cssutils\ncss_string = \"@import url('path/to/style.css'); a { color: blue; }\"\nsheet = cssutils.parseString(css_string)\nfor url in cssutils.getUrls(sheet):\n    print(url)\n```","cause":"The `cssutils.getUrls` function expects a parsed `CSSStyleSheet` object as input, not a raw CSS string or is incorrectly called directly on the module.","error":"AttributeError: module 'cssutils' has no attribute 'getUrls' OR AttributeError: 'str' object has no attribute 'type' (when using cssutils.getUrls)"},{"fix":"To suppress these logging messages, configure Python's logging system to a higher level (e.g., ERROR or CRITICAL) for the `cssutils` logger.\n```python\nimport logging\nimport cssutils\n\ncssutils.log.setLevel(logging.ERROR) # Suppress warnings and info messages\n# Or to completely disable cssutils logging for a parser instance:\n# parser = cssutils.CSSParser(log=logging.getLogger('null'))\n\ncss_text = \"@-webkit-keyframes anim { from { opacity: 0; } to { opacity: 1; } } div { -moz-border-radius: 5px; }\"\nsheet = cssutils.parseString(css_text)\n# Your cssutils logic here will now run without these warnings\n```","cause":"cssutils issues logging messages (warnings or errors) for non-standard CSS, unknown `@rules` (like vendor prefixes), or syntactical issues it encounters during parsing, even if `validate=False` is set.","error":"WARNING CSSStylesheet: Unknown @rule found. OR ERROR MediaQuery: Unexpected syntax"},{"fix":"Update cssutils to a version compatible with your Python 3 interpreter. This typically involves upgrading the package: `pip install --upgrade cssutils`.","cause":"An older version of cssutils, which uses Python 2 exception handling syntax (`except Exception, e:`), is being run in a Python 3 environment that requires `except Exception as e:`.","error":"SyntaxError: invalid syntax (often pointing to 'except xml.dom.HierarchyRequestErr, e:')"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"2.15.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/jaraco/cssutils","docs":null,"changelog":null,"pypi":"https://pypi.org/project/cssutils/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":"verified"}}