Skip to content

Instantly share code, notes, and snippets.

@unconditional
Forked from robflaherty/svg-path-to-json.py
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unconditional/049b6a81eea1ca064550 to your computer and use it in GitHub Desktop.
Save unconditional/049b6a81eea1ca064550 to your computer and use it in GitHub Desktop.
from xml.dom import minidom
import json
config = {
'svg_file' : 'map.svg',
'js_file' : 'map.js',
'js_var' : 'svgMap'
}
svg = minidom.parse(config['svg_file'])
paths = svg.getElementsByTagName('path')
items = {}
for node in paths:
if node.getAttributeNode('id'):
path_id = str(node.getAttributeNode('id').nodeValue)
path = str(node.getAttributeNode('d').nodeValue)
items[path_id] = path
json = json.dumps(items, indent=2)
f = open(config['js_file'], 'w')
f.write('var %s = ' % config['js_var'])
f.write(json)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment