I currently have some code that combines SVG files into one combined svg file.
My problem is that these SVG files have layers/groups and nested sublayers/subgroups in them.
At the moment it just creates a new main layer with the same sublayers for each SVG file added.
For example this is what it currently looks like when it combines multiple SVG files:
START
-Layer1
--Layer2
---Layer3
----Layer3a
-----PATH
-Layer1
--Layer2
---Layer3
----Layer3b
-----PATH
-Layer1
--Layer2
---Layer2a
----PATH
-Layer1
--Layer2
---Layer2b
----PATH
This is how it should look like:
START
-Layer1
--Layer2
---Layer2a
----PATH
---Layer2b
----PATH
---Layer3
----Layer3a
-----PATH
----Layer3b
-----PATH
I hope this makes sense. I also attached some images below.
This is my current code:
import os
from xml.etree import ElementTree as ET
#SVG files input folder
svg_dir = './Import/'
#New SVG
merged_svg = ET.Element('svg')
#Loop through SVG in folder
for i, filename in enumerate(os.listdir(svg_dir)):
#Check if SVG ends with "update.svg"
if filename.endswith('update.svg'):
#Open SVG and parse content
svg_path = os.path.join(svg_dir, filename)
svg_tree = ET.parse(svg_path)
#Get root element of SVG
svg_root = svg_tree.getroot()
#Check if root element exists
if svg_root is not None:
if i == 0:
#Add root element of first SVG
merged_svg = svg_root
else:
#Merge root element of other SVG to the first
for attr, value in svg_root.items():
merged_svg.set(attr, value)
#Loop through each child element in root
for child in svg_root:
print(child)
if isinstance(child, ET.Element):
#Find element with same ID in merged SVG file
existing_element = merged_svg.find('.//*[@id="%s"]' % child.get('id'))
if existing_element is not None:
#If element already exists - append to sub-child
print("element already exists - append to sub-child")
for sub_child in child:
if isinstance(sub_child, ET.Element):
existing_element.append(sub_child)
else:
#If the element does not exist
print("element does not exist - append to child")
merged_svg.append(child)
# Write the merged SVG to a file
merged_svg_file = open('./Export/COMBINED.svg', 'w')
merged_svg_file.write(ET.tostring(merged_svg).decode())
merged_svg_file.close()
Here is an example of what one of the SVG files looks like:
<?xml version="1.0" encoding="utf-8"?>
<svg width="1080" height="1920" version="1.1" id="START" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1080 1920" style="enable-background:new 0 0 1080 1920;" xml:space="preserve">
<g id="Layer1"><g id="Layer2"><g id="Layer3"><g id="Layer4"><path style="stroke:none;" d="M534 155L533 156L532 157L531.216 158.086L529.007 163.005L527.289 165.999L525.956 167.985L525.239 168.961L523.674 170.826L519.985 174.058L517.999 175.362L514 177.058L511 177.703L502 178.24L494 177L493 176L492 175L491 176L492 177L493 178L495.319 179.354L500.329 180.802L502 181L504 181L505 182L503 182L500.845 182.386L497.208 182.759L495.638 183.642L494.97 184.333L493.367 187.019L492.376 190L491.484 195.996L491.488 197.961L492.067 199.83L493.321 201.576L495.976 204.17L499.362 208.004L501.164 211L502.069 213L503.073 216L504.589 222.61C504.963 223.893 506.271 224.364 507.148 225.161L509.019 226.03C511.335 226.653 513.57 227.134 516 226.981L520 226.714L525 226.103L531 226.29L539 226L539.84 222.839L540 205L538.329 205.596L533.003 207.902L530.16 207.786C529.132 207.731 527.729 206.296 527 206L527.004 201.955C527.06 199.615 527.142 197.303 527.615 195C528.72 189.621 530.012 184.258 531.608 179L534.412 172C534.958 170.061 534.299 167.946 534.169 166L534 157L535 156L534 155M506 180L505 181L506 180z" />
</g></g></g></g></svg>
Any ideas how I can get the properly formatted/cleaned SVG files to work?
EDIT: as requested here are a few more SVG file examples.
Test1
<?xml version="1.0" encoding="utf-8"?>
<svg width="1080" height="1920" version="1.1" id="START" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1080 1920" style="enable-background:new 0 0 1080 1920;" xml:space="preserve">
<g id="Layer1"><g id="Layer2"><g id="Layer3"><g id="Test1"><path style="stroke:none;" d="M534 155L533 156L532 157L531.216 158.086L529.007 163.005L527.289 165.999L525.956 167.985L525.239 168.961L523.674 170.826L519.985 174.058L517.999 175.362L514 177.058L511 177.703L502 178.24L494 177L493 176L492 175L491 176L492 177L493 178L495.319 179.354L500.329 180.802L502 181L504 181L505 182L503 182L500.845 182.386L497.208 182.759L495.638 183.642L494.97 184.333L493.367 187.019L492.376 190L491.484 195.996L491.488 197.961L492.067 199.83L493.321 201.576L495.976 204.17L499.362 208.004L501.164 211L502.069 213L503.073 216L504.589 222.61C504.963 223.893 506.271 224.364 507.148 225.161L509.019 226.03C511.335 226.653 513.57 227.134 516 226.981L520 226.714L525 226.103L531 226.29L539 226L539.84 222.839L540 205L538.329 205.596L533.003 207.902L530.16 207.786C529.132 207.731 527.729 206.296 527 206L527.004 201.955C527.06 199.615 527.142 197.303 527.615 195C528.72 189.621 530.012 184.258 531.608 179L534.412 172C534.958 170.061 534.299 167.946 534.169 166L534 157L535 156L534 155M506 180L505 181L506 180z" />
</g></g></g></g></svg>
Test2
<?xml version="1.0" encoding="utf-8"?>
<svg width="1080" height="1920" version="1.1" id="START" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1080 1920" style="enable-background:new 0 0 1080 1920;" xml:space="preserve">
<g id="Layer_1"><g id="Layer_2"><g id="Layer_3"><g id="Test2"><path style="stroke:none;" d="M524 976L523 977L527.043 979.976L529.772 982.213L531.239 984.039L531.767 985.001C532.103 985.901 532.463 986.841 532.316 987.83C531.996 989.985 531.109 991.824 531.613 994.039L533.461 998L536.046 1002.84L538 1006L539.656 1005.88L542.681 1004.82L543.437 1004.26L544.737 1002.77L547.204 997.999C548.02 996.357 548.921 994.842 549.196 993C549.448 991.314 548.541 989.628 548.269 988.001C548.092 986.94 548.299 986.047 548.6 985.04L549.067 984.093L550.397 982.329C551.947 980.9 553.506 979.789 555.329 978.733L557 978L557 976L551.975 975.791L545 974.209L538 974.018C534.864 974.084 532.059 975.11 529.025 975.741L524 976z" />
</g></g></g></g></svg>
Test3
<?xml version="1.0" encoding="utf-8"?>
<svg width="1080" height="1920" version="1.1" id="START" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1080 1920" style="enable-background:new 0 0 1080 1920;" xml:space="preserve">
<g id="Layer_1"><g id="Layer_2"><g id="Layer3_"><g id="Layer_4"><g id="Layer_5"><g id="Test3"><path style="stroke:none;" d="M630 1425L631.714 1418L632.83 1410L633.17 1404L633.911 1398L633.814 1389L635.209 1383.16L635.357 1381.67L635 1380L631.84 1380L625.015 1380.39L623.089 1381.01L622.171 1381.46L620.439 1382.65L616.493 1387.02L615.13 1389L614 1391C613.345 1392.5 612.344 1394.32 612.329 1396L612.093 1398L612.105 1403L612.394 1405L613.414 1408L618.786 1417L621.185 1419.98L622.093 1420.94L623.039 1421.85L626.84 1424.4L628.329 1424.84L630 1425z" />
</g></g></g></g></g></g></svg>
Test4
<?xml version="1.0" encoding="utf-8"?>
<svg width="1080" height="1920" version="1.1" id="START" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1080 1920" style="enable-background:new 0 0 1080 1920;" xml:space="preserve">
<g id="Layer_1"><g id="Layer_2"><g id="Layer_3"><g id="Test4"><path style="stroke:none;" d="M545 155L545 157L545.585 158.671L545.958 164.002L545.594 168C545.454 169.288 545.225 170.724 545.588 172L548.7 180L551.335 190L552.714 198L552.941 200.999L552.848 202.985C552.949 204.237 552.103 205.587 551.397 206.526L550.682 207.164L549.852 207.632L547.981 207.999L546.004 207.632L543.16 206.328L540 205L540 225L541.671 225.436L547.002 226.24L555 226.053L561 226.714C563.734 226.927 566.281 227.046 568.999 226.581C569.857 226.434 571.267 225.997 571.941 225.427C572.931 224.592 574.467 223.991 574.988 222.671L575.786 220.907L577.45 215C577.919 213.378 578.237 211.441 579.164 210L580.433 208.004L583.996 204.17L586.678 201.576L587.933 199.83L588.512 197.961L588.516 195.996L587.624 190L586.633 187.019L585.03 184.333L584.362 183.642L582.792 182.759L579.155 182.386L577 182L575 182L574 181L575 180L576 181L579.16 180.816L587 179L588 178L588 176L589 175L588 176L586 176L584.329 176.897C583.005 177.413 581.435 177.989 579.995 178.027L576 178.289L568 177.532L564 176.362C562.581 175.944 561.23 174.893 560.039 174.058L559.09 173.316L557.301 171.699L554.21 167.985L552.877 165.999L551.681 164L549.558 159.161L549.202 157.671L549 156L547 156L547 155L545 155z" />
</g></g></g></g></svg>