I'm trying to modify an SVG file with python and the app that generated the SVG file added namespaces. But the namespaces only appear on the SVG tag itself. So the python script fails. Is there any way to specify a namespace when getting the root?
first the SVG ("sample-plain.svg"):
and now the python:
And unless I remove both `xmlns` and `xmlns:svg`, the file remains unchanged and the tspan isn't found. Any solutions (besides manually removing those two attributes)?
first the SVG ("sample-plain.svg"):
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg width="512" height="512" viewBox="0 0 135.46666 135.46667" version="1.1" id="svg1" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg"> <g id="layer1"> <rect style="fill:none;stroke:#000000;stroke-width:7.63634;" id="rect1" width="127.83032" height="127.83032" x="3.8181739" y="3.8181739" rx="12.48343" ry="12.483429" /> <text xml:space="preserve" style="font-size:160px;font-family:Roboto;text-align:center;text-anchor:middle;fill:#000000" x="67.694031" y="124.96131" id="text1"><tspan id="tspan1" x="69.87413" y="124.96131">A</tspan></text> </g></svg>Code:
from secrets import choiceimport xml.etree.ElementTree as ETfrom string import ascii_uppercasetree=ET.parse("sample-plain.svg")root=tree.getroot()for tspan in root.iter("tspan"): print(tspan.text) tspan.text=choice(ascii_uppercase)tree.write("sample-plain-changed.svg")And unless I remove both `xmlns` and `xmlns:svg`, the file remains unchanged and the tspan isn't found. Any solutions (besides manually removing those two attributes)?
Statistics: Posted by valkyrie44 — Sun Feb 01, 2026 6:48 pm