Reputation: 43
I've a SVG file created with Inkscape, now in the svg file is some text i try to alter but how? The svg looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="1480"
height="800"
version="1.1"
id="svg4184"
sodipodi:docname="2017-05-03_heizung2.svg"
inkscape:version="0.92.1 r15371">
...
<g
inkscape:groupmode="layer"
inkscape:label="05_CURVES"
id="g4182"
transform="translate(0,-322.51962)"
style="display:inline">
...
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:37.33333206px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none"
x="82.288353"
y="920.41907"
id="temp-bu"><tspan
sodipodi:role="line"
id="tspan5025-6-2-1-0"
x="82.288353"
y="920.41907"
style="font-size:32px">TEXT TO CHANGE</tspan></text>
...
</g>
</svg>
I tried using xml.etree.ElementTree didn't get to the TEXT TO CHANGE... I also tried
from xml.dom as minidom
doc = minidom.parse('File.svg')
text = [text.getAttribute('id') for text in doc.getElementsByTagName('text')
but i didn't get to the TEXT TO CHANGE...
how can I alter the text
Upvotes: 0
Views: 7978
Reputation: 21663
Never ignore the possibilities inherent in brute force.
>>> open('newfile.svg', 'w').write(open('temp.svg').read().replace('>TEXT TO CHANGE</tspan>','>What I meant to write</tspan>'))
Result:
Upvotes: 6
Reputation: 43
just to summarize
import xml.dom.minidom
doc = xml.dom.minidom.parse('d:\\Temp\\heizung.svg')
f = open("d:\\Temp\\heizung.svg", "w")
name = doc.getElementsByTagName('tspan')
name[0].childNodes[0].nodeValue = '100'
f.write(doc.toprettyxml())
Upvotes: 3
Reputation: 355
TEXT TO CHANGE is in tspan tag
import xml.dom.minidom
doc = xml.dom.minidom.parse('File.svg')
name = doc.getElementsByTagName('tspan')
for t in name:
if (t.attributes['id'].value=="tspan5025-6-2-1-0"):
print [x.nodeValue for x in t.childNodes]
Upvotes: 2