I have an svg template that I am copying and customizing to create several different cards and tiles for a game. I want to programmatically (in Python, preferably) change elements from the template per-card. I seem to have no trouble finding ways to change attributes or css, but I'm having trouble finding a library where I can easily parse an existing svg and replace elements.
The svg of my template looks somewhat like this:
<!--Square 2" Tile Template -->
<svg xmlns="http://www.w3.org/2000/svg" width="181" height="181">
<text id="tile_text" y="90" width="100%"
style="text-align:center;font-family:Verdana;font-size:20">
TEXT TO REPLACE
</text>
</svg>
I have looked at Python's lxml
and xml.dom.minidom
but neither of them seem to support something like tile_text_element.innerHTML = "New Tile Name"
. Help?
EDIT:
To add a little bit about my workflow, I am creating a bunch of individualized svgs for each card, then batch rendering them to pdf through inkscape.
Another way is to use lxml, seems to work, WARNING bad quality code:
$ cat card.py
#!/usr/bin/env python
from lxml import etree
with open('card.svg') as card:
root = etree.fromstring(card.read())
root.find('.//{http://www.w3.org/2000/svg}text').text = "foobar"
print etree.tostring(root)
$ python card.py
<svg xmlns="http://www.w3.org/2000/svg" width="181" height="181">
<text id="tile_text" y="90" width="100%" style="text-align:center;font-family:Verdana;font-size:20">foobar</text>
</svg>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With