Answer the question
In order to leave comments, you need to log in
How to change id value in svg using python?
Hello!
The task was to process a rather large file with a map in order to simplify our work in the future.
<g id="Group 182">
<path id="Vector_791" d="" fill="#C0D898" stroke="#806239" stroke-width="0.75" stroke-miterlimit="10"/>
<path id="Vector_792" d="" fill="white"/>
<path id="1478" d="" fill="#3F5714"/>
<path id="7.47" d="" fill="#454545"/>
</g>
<g id="1495">
<path id="Vector_795" d="" fill="#C0D898" stroke="#806239" stroke-width="0.75" stroke-miterlimit="10"/>
<path id="Vector_796" d="" fill="white"/>
<path id="1495_2" d="" fill="#3F5714"/>
<path id="7.47" d="" fill="#454545"/>
</g>
<g id="1478">
<path id="Vector_791" d="" fill="#C0D898" stroke="#806239" stroke-width="0.75" stroke-miterlimit="10"/>
<path id="Vector_792" d="" fill="white"/>
<path id="1478" d="" fill="#3F5714"/>
<path id="7.47" d="" fill="#454545"/>
</g>
<g id="1495">
<path id="Vector_795" d="" fill="#C0D898" stroke="#806239" stroke-width="0.75" stroke-miterlimit="10"/>
<path id="Vector_796" d="" fill="white"/>
<path id="1495_2" d="" fill="#3F5714"/>
<path id="7.47" d="" fill="#454545"/>
</g>
Answer the question
In order to leave comments, you need to log in
xml_ = '''<svg>
<g id="Group 182">
<path id="Vector_791" d="" fill="#C0D898" stroke="#806239" stroke-width="0.75" stroke-miterlimit="10"/>
<path id="Vector_792" d="" fill="white"/>
<path id="1478" d="" fill="#3F5714"/>
<path id="7.47" d="" fill="#454545"/>
</g>
<g id="1495">
<path id="Vector_795" d="" fill="#C0D898" stroke="#806239" stroke-width="0.75" stroke-miterlimit="10"/>
<path id="Vector_796" d="" fill="white"/>
<path id="1495_2" d="" fill="#3F5714"/>
<path id="7.47" d="" fill="#454545"/>
</g>
</svg>'''
import lxml.etree as ET
root = ET.fromstring(xml_)
tree = ET.ElementTree(root)
svg_ = tree.getroot()
for i in svg_.findall(".//g"):
if "Group" in i.attrib['id']:
i.attrib['id'] = i.getchildren()[2].attrib['id']
print(ET.tostring(tree, pretty_print=True).decode("utf-8"))
<svg>
<g id="1478">
<path id="Vector_791" d="" fill="#C0D898" stroke="#806239" stroke-width="0.75" stroke-miterlimit="10"/>
<path id="Vector_792" d="" fill="white"/>
<path id="1478" d="" fill="#3F5714"/>
<path id="7.47" d="" fill="#454545"/>
</g>
<g id="1495">
<path id="Vector_795" d="" fill="#C0D898" stroke="#806239" stroke-width="0.75" stroke-miterlimit="10"/>
<path id="Vector_796" d="" fill="white"/>
<path id="1495_2" d="" fill="#3F5714"/>
<path id="7.47" d="" fill="#454545"/>
</g>
</svg>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question