Skip to content

Instantly share code, notes, and snippets.

@GaryLee
Created May 22, 2024 04:22
Show Gist options
  • Save GaryLee/2a30418128d8b1df8bda6dccb9b64ca6 to your computer and use it in GitHub Desktop.
Save GaryLee/2a30418128d8b1df8bda6dccb9b64ca6 to your computer and use it in GitHub Desktop.
An example to get the root of mxGraphModel from draw io file.
#!python
# coding: utf-8
import sys
import zlib
import base64
import xml.etree.ElementTree as ET
from urllib.parse import unquote
def decode_drawio(filename):
tree = ET.parse(filename)
root = tree.getroot()
is_compressed = root.attrib.get('compressed', None) == 'true'
if is_compressed:
raw = tree.find('diagram')
data = base64.b64decode(raw.text)
zobj = zlib.decompressobj(wbits=-15)
xml = zobj.decompress(data).decode('utf-8')
xml = unquote(xml)
xml = ET.ElementTree(ET.fromstring(xml))
xml = xml.getroot()
else:
raw = tree.find('diagram/mxGraphModel')
xml = raw
return xml
if __name__ == '__main__':
xml = decode_drawio(sys.argv[1])
print(xml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment