Skip to content

Instantly share code, notes, and snippets.

@zacharyneveu
Last active May 10, 2019 18:09
Show Gist options
  • Save zacharyneveu/fbac3c2df61ec72c0e49394d29c32410 to your computer and use it in GitHub Desktop.
Save zacharyneveu/fbac3c2df61ec72c0e49394d29c32410 to your computer and use it in GitHub Desktop.
Python Script to convert each page of a drawio file to a png image with the page name as the file name. Requires https://github.com/languitar/drawio-batch to be installed
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Runs drawio-batch for all pages of drawio file
"""
import sys, os
xsl="""<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text"/>
<xsl:template match="/mxfile">
<xsl:for-each select="diagram">
<xsl:value-of select="concat(@name,'&#xA;')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
"""
SCRIPT=os.path.expanduser('~')+'/Downloads/drawio.xsl'
if not os.path.isfile(SCRIPT):
with open(SCRIPT, 'w+') as script:
script.write(xsl)
try:
f = sys.argv[1]
except:
f = os.popen('ls *.drawio').read()[:-1]
pages = os.popen('xsltproc '+SCRIPT+' '+f).read().split('\n')[:-1]
try:
os.makedirs('imgs')
except:
pass
for idx, name in enumerate(pages):
i = str(idx+1) # 1 indexing
#op = f[:-7]+'_'+i+'.png'
op = 'imgs/'+name+'.png'
try:
os.popen('rm '+op+' > /dev/null')
except:
pass
print(op)
cmd = ' '.join(['drawio-batch', '-d', str(idx), f, op])
os.popen(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment