Skip to content

Instantly share code, notes, and snippets.

@btel
Created July 22, 2013 14:05
Show Gist options
  • Save btel/6054077 to your computer and use it in GitHub Desktop.
Save btel/6054077 to your computer and use it in GitHub Desktop.
modify SVG line width in python using lxml inline CSS style
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "svg_modify_line_width"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": "from IPython.display import SVG\n\n#first create a sample svg file\nsvg_data = \"\"\"\n <svg>\n <g>\n <rect x=\"10\" y=\"10\" width=\"50\" height=\"50\" \n style=\"fill:white;stroke-width:1;stroke:black\"/>\n <rect x=\"70\" y=\"70\" width=\"50\" height=\"50\" \n style=\"fill:white;stroke-width:1;stroke:black\"/>\n </g>\n </svg>\"\"\"\nSVG(svg_data)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 30,
"svg": "<svg>\n <g>\n <rect height=\"50\" style=\"fill:white;stroke-width:1;stroke:black\" width=\"50\" x=\"10\" y=\"10\"/>\n <rect height=\"50\" style=\"fill:white;stroke-width:1;stroke:black\" width=\"50\" x=\"70\" y=\"70\"/>\n </g>\n </svg>",
"text": "<IPython.core.display.SVG at 0x43cdd10>"
}
],
"prompt_number": 30
},
{
"cell_type": "code",
"collapsed": false,
"input": "from lxml import etree\nfrom StringIO import StringIO\n\nclass CSSStyle:\n \n def __init__(self, data):\n self._dict = {}\n self._dict.update(self._parse_string(data))\n \n def __getattr__(self, attrname):\n return getattr(self._dict, attrname)\n \n def _parse_string(self, style):\n return dict(map(lambda x: x.split(':'), style.split(';')))\n \n def tostring(self):\n return \";\".join(map(lambda x: \"{0}:{1}\".format(*x), self._dict.items()))",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": "f = StringIO(svg_data)\ntree = etree.parse(f)\nelements = tree.findall('//rect') #here change \"rect\" to SVG element you want to modify\n#to achieve more fine-grained control check the XPath docs: http://effbot.org/zone/element-xpath.htm\n\nfor el in elements:\n style = CSSStyle(el.attrib['style'])\n style['stroke-width'] = \"2\"\n print el.tag, style\n el.attrib['style'] = style.tostring()",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "rect {'stroke': 'black', 'stroke-width': '2', 'fill': 'white'}\nrect {'stroke': 'black', 'stroke-width': '2', 'fill': 'white'}\n"
}
],
"prompt_number": 35
},
{
"cell_type": "code",
"collapsed": false,
"input": "mod_svg = StringIO()\ntree.write(mod_svg)\nmod_svg.seek(0)\nSVG(mod_svg.read())",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 29,
"svg": "<svg>\n <g>\n <rect height=\"50\" style=\"stroke:black;stroke-width:2;fill:white\" width=\"50\" x=\"10\" y=\"10\"/>\n <rect height=\"50\" style=\"stroke:black;stroke-width:2;fill:white\" width=\"50\" x=\"70\" y=\"70\"/>\n </g>\n </svg>",
"text": "<IPython.core.display.SVG at 0x4400710>"
}
],
"prompt_number": 29
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment