A CSS Cascading Style Sheets library for Python implementing DOM Level 2 Style specifications
npx @tessl/cli install tessl/pypi-cssutils@1.0.0A comprehensive CSS Cascading Style Sheets library for Python that implements DOM Level 2 Style specifications. cssutils enables parsing, manipulation, and generation of CSS stylesheets with complete CSS 2.1 and CSS3 module support.
pip install cssutilsimport cssutilsCommon imports for CSS parsing and manipulation:
from cssutils import CSSParser, CSSSerializerAccess CSS classes through the css module:
import cssutils.css as css
# Then use: css.CSSStyleSheet, css.CSSStyleRule, css.CSSStyleDeclarationImport all main functionality:
from cssutils import *
# Imports: css, stylesheets, CSSParser, CSSSerializerimport cssutils
# Parse CSS from string
css_text = """
a {
color: red;
font-weight: bold;
}
"""
# Using convenience function
stylesheet = cssutils.parseString(css_text)
print(stylesheet.cssText)
# Using CSSParser directly
parser = cssutils.CSSParser()
stylesheet = parser.parseString(css_text)
# Access and modify rules
for rule in stylesheet:
if rule.type == rule.STYLE_RULE:
print(f"Selector: {rule.selectorText}")
print(f"Properties: {rule.style.cssText}")
# Modify properties
rule.style.color = 'blue'
rule.style.setProperty('background-color', 'yellow')
# Serialize back to CSS
print(stylesheet.cssText)cssutils implements the DOM Level 2 CSS specification with the following key components:
The library provides both high-level convenience functions and low-level DOM access, enabling use cases from simple CSS processing to complex stylesheet manipulation and validation.
Parse CSS from strings, files, and URLs with comprehensive error handling and validation. Support for CSS 2.1, CSS3 modules, and real-world CSS variations.
def parseString(cssText, encoding=None, href=None, media=None, title=None, validate=None):
"""Parse CSS from string"""
def parseFile(filename, encoding=None, href=None, media=None, title=None, validate=None):
"""Parse CSS from file"""
def parseUrl(href, encoding=None, media=None, title=None, validate=None):
"""Parse CSS from URL"""
def parseStyle(cssText, encoding='utf-8', validate=None):
"""Parse CSS style attribute"""Create, manipulate, and manage CSS stylesheets with full DOM Level 2 compliance. Handle rules, namespaces, media queries, and variables.
class CSSStyleSheet:
"""Complete CSS stylesheet representation"""
cssRules: CSSRuleList
media: MediaList
href: str
title: str
def insertRule(rule, index): ...
def deleteRule(index): ...
def add(rule): ...Complete implementation of all CSS rule types including style rules, @-rules, and nested structures with proper inheritance and containment.
class CSSStyleRule:
"""CSS style rule (selector + declarations)"""
selectorText: str
style: CSSStyleDeclaration
class CSSMediaRule:
"""@media rule with nested rules"""
media: MediaList
cssRules: CSSRuleList
class CSSImportRule:
"""@import rule"""
href: str
media: MediaList
styleSheet: CSSStyleSheetParse, validate, and manipulate CSS selectors with support for CSS3 selector syntax and pseudo-classes.
class Selector:
"""Individual CSS selector"""
selectorText: str
specificity: tuple
class SelectorList:
"""List of CSS selectors"""
selectorText: str
def appendSelector(selector): ...Manage CSS property declarations with validation, shorthand expansion, and value parsing.
class CSSStyleDeclaration:
"""CSS property declarations container"""
cssText: str
length: int
def getPropertyValue(propertyName): ...
def setProperty(propertyName, value, priority=''): ...
def removeProperty(propertyName): ...
def getProperties(all=False): ...Convert CSS objects back to formatted CSS text with extensive customization options for output formatting.
class CSSSerializer:
"""CSS serialization with formatting control"""
prefs: Preferences
def do_CSSStyleSheet(stylesheet): ...
def do_CSSStyleRule(rule): ...
def do_css_CSSStyleDeclaration(style): ...Helper functions for CSS processing, URL handling, import resolution, and stylesheet manipulation.
def getUrls(sheet):
"""Generator yielding all URLs in stylesheet"""
def replaceUrls(sheetOrStyle, replacer, ignoreImportRules=False):
"""Replace URLs in stylesheet or style"""
def resolveImports(sheet, target=None):
"""Recursively resolve @import rules"""
def setSerializer(serializer):
"""Set global serializer"""Console scripts for CSS processing and manipulation from the command line.
# Installed as console scripts via pip install cssutils
cssparse # Parse and format CSS files
csscombine # Combine CSS files by resolving @import rules
csscapture # Capture CSS from web pagesGlobal objects for controlling cssutils behavior across all parsing and serialization operations.
cssutils.log # Global error handler (ErrorHandler instance)
cssutils.ser # Global serializer (CSSSerializer instance)
cssutils.profile # Global validation profiles (Profiles instance)
cssutils.VERSION # Package version string