Skip to content

Instantly share code, notes, and snippets.

@notasausage
Forked from su-narthur/svg-extract.py
Last active September 1, 2026 13:54
Show Gist options
  • Select an option

  • Save notasausage/bfd6877ad1d804dffd1a2c81fddbfaff to your computer and use it in GitHub Desktop.

Select an option

Save notasausage/bfd6877ad1d804dffd1a2c81fddbfaff to your computer and use it in GitHub Desktop.
Convert SVG font to individual SVG files
# http://helpfulsheep.com/2015-03-25-converting-svg-fonts-to-svg/
import sys, re
if len(sys.argv) < 2:
print ('Usage: python {} webfont-file.svg').format(sys.argv[0])
sys.exit()
with open(sys.argv[1], 'r') as r:
font = r.read()
# Glyph attributes (including the path's d attribute) may span several lines.
# Match through the end of each element rather than extracting only its first
# line. SVG fonts normally use self-closing glyph elements, but handle an
# explicit closing tag as well.
glyphs = re.findall(r'<glyph\b.*?(?:/>|</glyph\s*>)', font, re.DOTALL)
# for every glyph element in the file
for i in range(0, len(glyphs)):
filename = re.search(r'glyph-name="([^"]+)"', glyphs[i])
filename = filename.group(1) if filename else str(i + 1).rjust(3, '0')
with open(filename + ".svg", 'w') as w:
w.write('<svg width="2000" height="2000" viewBox="0 0 2000 2000" xmlns="http://www.w3.org/2000/svg">\n')
# replace 'glyph' with 'path' and flip vertically
path = re.sub(r'<glyph\b', '<path transform="scale(1, -1) translate(0, -2000)"', glyphs[i], count=1)
path = re.sub(r'</glyph\s*>', '</path>', path)
w.write(path + '\n')
w.write('</svg>')
@notasausage

Copy link
Copy Markdown
Author

Fixed a few issues including a missing closing path tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment