Created
December 18, 2025 07:47
-
-
Save GaryLee/8314d881cc747722956e98584ab352ee to your computer and use it in GitHub Desktop.
A python script to list the pages of drawio file and export the specified pages.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| import os | |
| import sys | |
| from pathlib import Path | |
| import platform | |
| import xml.etree.ElementTree as ET | |
| # Define the output figure format. Could be 'svg', 'png', 'pdf', etc. | |
| FIGURE_FMT = "svg" | |
| def get_drawio_executable(): | |
| """Find the draw.io executable based on the operating system.""" | |
| system = platform.system() | |
| if system == "Windows": | |
| exe = "draw.io.exe" | |
| elif system == "Darwin": | |
| exe = "/Applications/draw.io.app/Contents/MacOS/draw.io" | |
| elif system == "Linux": | |
| exe = "draw.io" | |
| else: | |
| raise RuntimeError(f"Unsupported platform: {system}") | |
| assert Path(exe).is_file(), f"Draw.io executable not found: {exe}" | |
| return exe | |
| def get_pages(filename): | |
| """Extract page information from a drawio file. Returns a dictionary mapping page numbers (1-based) to page names.""" | |
| root = ET.parse(filename).getroot() | |
| pages = {} | |
| for page, elm in enumerate(root.findall(".//diagram"), start=1): | |
| pages[page] = elm.attrib.get("name", "<unnamed>") | |
| return pages | |
| def main(): | |
| pages = get_pages(sys.argv[1]) | |
| if len(sys.argv) <= 2: | |
| for page, name in pages.items(): | |
| print(f"{page}: {name}") | |
| else: | |
| drawio_exe = get_drawio_executable() | |
| for page in sys.argv[2:]: | |
| page = int(page) | |
| if page not in pages: | |
| print('Invalid page number:', page) | |
| continue | |
| page_name = pages[page] | |
| output = f"{page_name}.{FIGURE_FMT}" | |
| cmd = f"{drawio_exe} --export --format {FIGURE_FMT} --page-index {page} --output \"{output}\" \"{sys.argv[1]}\"" | |
| os.system(cmd) | |
| if __name__ == "__main__": | |
| if len(sys.argv) < 2: | |
| print("Usage: drawio.py <file.drawio> [page_numbers...]") | |
| print("The page numbers are 1-based.") | |
| sys.exit(1) | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment