Skip to content

Instantly share code, notes, and snippets.

@sparkslabs
Forked from johnjohnlin/odg2svgs.py
Created June 4, 2024 14:43
Show Gist options
  • Save sparkslabs/598ff65da4dde8f94ee87e61235f3ab8 to your computer and use it in GitHub Desktop.
Save sparkslabs/598ff65da4dde8f94ee87e61235f3ab8 to your computer and use it in GitHub Desktop.
A simple script converting an odg file into individual SVG pages, works for libreoffice 7.0
#!python
# usage: if you have an xxx.odg, then execute `./odg2svgs.py xxx`
import sys
import subprocess
import xml.etree.ElementTree as ET
kBASENAME = sys.argv[1]
subprocess.run(["libreoffice", "--draw", "--headless", "--convert-to", "svg", kBASENAME+".odg"], check=True)
tree = ET.parse(kBASENAME + ".svg")
root = tree.getroot()
pages_top = root.find('.//{http://www.w3.org/2000/svg}g[@class="SlideGroup"]')
pages = list(pages_top)
for page in pages:
pages_top.remove(page)
for pagenum, page in enumerate(pages):
pages_top.append(page)
tree.write(f"{kBASENAME}-{pagenum:04d}.svg")
pages_top.remove(page)
subprocess.run(["rm", kBASENAME+".svg"], check=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment