D
D
Danil2021-03-24 22:44:06
Python
Danil, 2021-03-24 22:44:06

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.

SvgScale

<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>



It is necessary to process all Groups and not affect g, whose id is numbers
In this example, you need to replace "Group 182" with id from the 3rd line, that is, replace it with "1478"
3rd line in groups is always an integer, 4th float

Below bring what needs to be done
SvgShka in the end

<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>



I started throwing in something simple, but I didn’t deal with parsing in python before, so I’m asking for help with the implementation :(

The file is too large for manual replacement of each value

upd: id="Group any_order_number_none"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeniy _, 2021-03-25
@GeneD88

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 question

Ask a Question

731 491 924 answers to any question