この記事では,drawioの全タブを一括出力する方法を記述する.
環境
- OS:windows 11
- Python環境:conda 22.9.0
全体の流れ
以下の手順で変化するコードを作成する。
- xml.etree.ElementTreeを用いて,各タブの名前を取得する.
- 各タブごとに,画像出力する(pythonからdrawioコマンド実行).
全タブを一括出力するコード
draw.ioの各タブを一括出力するスクリプトを次に示す。
このコードでは,実行ディレクトリに’input.drawio’があることを前提とする.
import xml.etree.ElementTree as namereader
import subprocess
input_file_name = 'input'
drawio_exe_path = "C:\Program Files\draw.io\draw.io.exe"
file_type = 'png'
drawio_input_file = input_file_name + '.drawio'
root = namereader.parse(drawio_input_file).getroot()
for i in range(int(root.get('pages'))):
### get name of tab
page_name = root[i].get('name')
### make output file name
output_name = input_file_name + '-' + page_name + '.' + file_type
### make command
# "C:\Program Files\draw.io\draw.io.exe" input.drawio -xf png -o output.png -p 0
cmd = ' '.join([drawio_exe_path, drawio_input_file, '-xf', file_type, '-o', output_name, '-p', str(i)])
### export image
subprocess.run(cmd)
コードの解説
タブ名の取得とコマンド文字列生成
下記でタブ名の名前取得を行っている.
root = namereader.parse(drawio_input_file).getroot()
for i in range(int(root.get('pages'))):
### get name of tab
page_name = root[i].get('name')
drawioは次で紹介する通り,コマンドラインから実行することができる.
残りの部分は,次で紹介するコマンドの文字列を生成しているため,drawioのコマンドを理解すれば,全体が理解できる.
生成したコマンドは,次のコードで実行される.
subprocess.run(cmd)
draw.ioのコマンド
下記コマンドをコマンドプロンプトで実行することで,drawioから画像を出力できる.
"C:\Program Files\draw.io\draw.io.exe" input.drawio -xf png -o output.png -p 0
各コマンドのオプションの意味は次の通り.

その他のオプション
下記コマンドで,drawioのコマンドのヘルプを表示できる.
"C:\Program Files\draw.io\draw.io.exe" --help
ヘルプの表示結果は次.
Options:
-V, --version output the version number
-c, --create creates a new empty file if no file is
passed
-k, --check does not overwrite existing files
-x, --export export the input file/folder based on the
given options
-r, --recursive for a folder input, recursively convert
all files in sub-folders also
-o, --output <output file/folder> specify the output file/folder. If
omitted, the input file name is used for
output with the specified format as
extension
-f, --format <format> if output file name extension is
specified, this option is ignored (file
type is determined from output extension,
possible export formats are pdf, png, jpg,
svg, vsdx, and xml) (default: "pdf")
-q, --quality <quality> output image quality for JPEG (default:
90)
-t, --transparent set transparent background for PNG
-e, --embed-diagram includes a copy of the diagram (for PNG,
SVG and PDF formats only)
--embed-svg-images Embed Images in SVG file (for SVG format
only)
-b, --border <border> sets the border width around the diagram
(default: 0)
-s, --scale <scale> scales the diagram size
--width <width> fits the generated image/pdf into the
specified width, preserves aspect ratio.
--height <height> fits the generated image/pdf into the
specified height, preserves aspect ratio.
--crop crops PDF to diagram size
-a, --all-pages export all pages (for PDF format only)
-p, --page-index <pageIndex> selects a specific page, if not specified
and the format is an image, the first page
is selected
-g, --page-range <from>..<to> selects a page range (for PDF format only)
-u, --uncompressed Uncompressed XML output (for XML format
only)
-h, --help display help for command
注意点
drawioインストールパス
本記事の例では,drawioのインストールパスは
"C:\Program Files\draw.io\draw.io.exe"
としている.
windowsであれば,パスが同じである確率が高いが,動作しない場合は要チェックされたい.
-a オプション
drawioのコマンドで,-a
で全てのタブを出力できるオプションがあるが,
pdfで出力するときしか使用できないため注意.
まとめ
この記事では,draw.ioの各タブの画像を一括出力する方法を紹介した.
タブが多いと手動で各タブをエクスポートするのが面倒なため,活用いただきたい.
参考文献
- https://qiita.com/ryo_i6/items/6ebbeadd181da4d0b5b3