'Creating a script in python to alter the text in an SVG file
I'm new to python and I'm trying to create a script to open an existing .svg file, change the text/font/colour/size, and then save it as a separate .svg
I've managed to create another script using svgwrite but this can only make new files instead of editing existing ones.
I've had a look at a few other threads suggesting using the inkscape extension inkex.py but I can't figure out how to write anything working with this.
I've also tried lxml but I don't really understand what I'm doing with this. From everything I found I know I have to parse the .svg and from this I got the below code but this seems to be a dead end as the result is blank
import lxml.etree as ET
xml = ET.parse('C:\\Users\\Admin\\Desktop\\Test2.svg')
svg = xml.getroot()
print(svg)
print(svg.findall(".//{Element {http://www.w3.org/2000/svg}svg at 0x3e107a8"))
--Edit
In case anyone is trying to do something similar I did some more digging and found the best way to do it for my purpose was to open the svg, replace the string where it was needed and write to file, e.g.
Change = open(/files/file.svg, "rt")
data = change.read()
data = data.replace('original text', 'new text')
Change.close()
Change = open(/files/file.svg "wt")
Change.write(data)
Change.close()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
