Skip to content

Instantly share code, notes, and snippets.

@kingoflolz
Created August 30, 2015 11:01
Show Gist options
  • Save kingoflolz/20b8ce9ed5af5448d9fe to your computer and use it in GitHub Desktop.
Save kingoflolz/20b8ce9ed5af5448d9fe to your computer and use it in GitHub Desktop.
from xml.etree import ElementTree
from pprint import pprint
tree = ElementTree.parse('diagram.svg')
out = {}
def walk(node, attribs):
at = attribs.copy()
at.update(node.attrib)
# out[node.tag] = attribs
#print(node.tag, attribs)
if node.tag.endswith("}rect") or node.tag.endswith("}circle") or node.tag.endswith("}path"):
#if node.tag.endswith("}circle"):
# print("magic")
if "fill" not in at:
at["fill"] = "#000000"
if "stroke" not in at:
at["stroke"] = "#000000"
if "stroke-dasharray" not in at:
at["stroke-dasharray"] = "none"
if "stroke-width" not in at:
at["stroke-width"] = "1"
out[at["id"]] = "fill=\"" + at["fill"] + "\" stroke=\"" + at["stroke"] + \
'" stroke-dasharray="' + at["stroke-dasharray"] + '" stroke-width="' + at[
"stroke-width"] + '"'
for child in node:
walk(child, at)
walk(tree.getroot(), {})
for key in sorted(out):
print(key + " " + out[key])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment