cssutils
A Python package to parse and build CSS Cascading Style Sheets.
Actually querying a parsed stylesheet on a given document or element is planned for probably 0.9.7, for now see the example examples/style.py.
example
# -*- coding: utf-8 -*-
import cssutils
css = u'''/* a comment with umlaut รค */
@namespace html "http://www.w3.org/1999/xhtml";
html|a { color:red; }'''
sheet = cssutils.parseString(css)
for rule in sheet:
if rule.type == rule.STYLE_RULE:
for property in rule.style:
property.value = 'green'
property.priority = 'IMPORTANT'
rule.style['margin'] = '01.0eM' # or: ('1em', 'important')
sheet.encoding = 'ascii'
sheet.namespaces['xhtml'] = 'http://www.w3.org/1999/xhtml'
sheet.namespaces['atom'] = 'http://www.w3.org/2005/Atom'
sheet.add('atom|title {color: #000000 !important}')
sheet.add('@import "sheets/import.css";')results in
@charset "ascii";
@import "sheets/import.css";
/* a comment with umlaut \E4 */
@namespace xhtml "http://www.w3.org/1999/xhtml";
@namespace atom "http://www.w3.org/2005/Atom";
xhtml|a {
color: green !important;
margin: 1em
}
atom|title {
color: #000 !important
}development version (currently alpha)
Release 0.9.6b1 090609. A few non-backwards compatible changes have been made, please see the online docs help to migrate to 0.9.6. See the docs for full details but the main points here:
- 0.9.6b1: cssutils passes all tests now on Jython from version 2.5RC4
- 0.9.4a4: Started review of cssutils with regard to CSS 2.1 rev 1, e.g. all examples from http://www.w3.org/TR/2009/CR-CSS2-20090423/syndata.html#illegalvalues should work now
- 0.9.4a4: New preference option `keepUnkownAtRules = False`
- 0.9.6a4: Fixes for Issue 22 , Issue 23 and Issue 24
- 0.9.6a4: CHANGE FROM 0.9.6a0: xml.dom.DOMExceptions raised do contain infos about the position where the exception occured in e.line, e.col but due to a problem with PyXML the exception raised can be used like this again: msg = str(e)
- 0.9.6a3: Works with Python 2.6.2 which added a backwards incompatible change for the reversed iterator
- 0.9.6a3: Fixed issue #21
- 0.9.6a3: Updated included http://cthedot.de/encutils/ to version 0.9
- 0.9.6a2: Refactored Profiles including Custom profiles, added cssutils.profile
- 0.9.6a2: Added CSS3_PAGED_MEDIA properties
- 0.9.6a2: p.valid == False is now set for Properties not valid in the current profile even if they are valid in a different profile
- 0.9.6a2: Logging reports have been refactored
- 0.9.6a2: cssutils.resolveImports adds only given href to resulting sheet but not the complete path anymore (security opt)
- 0.9.6a2: IE alpha values are parsed now
- 0.9.6a1: New documentation in Sphinx format for download (for 0.9.6a1) or online, see Links above
- 0.9.6a1: cssutils.resolveImports(sheet) added and csscombine should work with nested imports now too
- 0.9.6a1: Script csscombine has new option -u URL which parses the given URL
- 0.9.6a1: Should work on GAE as is
- 0.9.6a0: SEE 0.9.6a4!!!: xml.dom.DOMExceptions raised do now contain infos about the position where the exception occured. Use e.g. msg, line, col = e.args[0], e.args[1], e.args[2]
- 0.9.6a0: @page rule accepts named pages in addition to pseudo pages :first :left :right now
- 0.9.6a0: Script cssparse has new option -u URL which parses the given URL
- 0.9.6a0: Added optimizations to some values: #112233 => #123, 010.0em => 10em etc
- 0.9.6a0: Most DEPRECATED attributes are finally removed in this release, CSSStyleSheet.replaceUrls is gone in favor of cssutils.replaceUrls(), Property.normalname is gone in favor of Property.name
More info on the pages or downloads referred to on the sitebar.
deprecated version
Release 0.9.5.1 080811. See the README and CHANGELOG for details. Please see MIGRATE for help to migrate to 0.9.5 from lower versions.