Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python CSS Parser

Tags:

python

html

css

I am experimenting with CSS files in Python to assist colour blind people. I need to get inside every selector block and change the 'background:' and 'color:'. I tried using CSS parsers like tinycss but they are not concentrating on getting selectors.

Example input:

body {background:#fff; color:#ccc}

And output:

body {background:#000; color:#aaa}
like image 296
VikkyB Avatar asked Nov 03 '22 06:11

VikkyB


1 Answers

try this:

http://cthedot.de/cssutils/

parser = CSSParser()
# optionally
parser.setFetcher(fetcher)
sheet = parser.parseFile('test1.css', 'ascii')
print sheet.cssText

it's pretty simple to use in css processing.

to work with selectors you could use cssutils.css.SelectorList and cssutils.css.Selector

like image 142
Dmitry Zagorulkin Avatar answered Nov 09 '22 15:11

Dmitry Zagorulkin