A random link Home Compute Inkscape Contact   
   
   
   

Altering Style

#!/usr/bin/env python
"""
style.py
Example of accessing and altering the style attribute of an element 
with simplistic methods to parse and format the style attribute.
"""
import sys, getopt
import xml.dom.ext
import xml.dom.ext.reader.Sax2
import xml.xpath

def parseStyle(s):
	"""Create a dictionary from the value of an inline style attribute"""
	return dict([i.split(":") for i in s.split(";")])
def formatStyle(a):
	"""Format an inline style attribute from a dictionary"""
	return ";".join([":".join(i) for i in a.iteritems()])

#collect ids from the arguments
opts = getopt.getopt(sys.argv[1:],'',['id='])
ids = [opt[1] for opt in opts[0] if opt[0]=='--id']

#create xml parser
reader = xml.dom.ext.reader.Sax2.Reader()
#open SVG tempfile
stream = open(sys.argv[-1:][0],'r')
#create DOM object
doc = reader.fromStream(stream)
stream.close()

#Code to modify the DOM object goes here
for id in ids:
	path = '//*[@id="%s"]' % id
	for node in xml.xpath.Evaluate(path,doc):
		style = node.attributes.getNamedItem('style')
		s = parseStyle(style.value)
		s['fill-opacity']='0.5'
		style.value = formatStyle(s)

#serialize DOM object into XML on stdout
xml.dom.ext.Print(doc)

Files:

     
   
   
Home Compute Inkscape Contact