Skip to content

Instantly share code, notes, and snippets.

@miguelbermudez
Forked from robflaherty/svg-path-to-json.py
Created September 28, 2012 18:30
Show Gist options
  • Save miguelbermudez/3801411 to your computer and use it in GitHub Desktop.
Save miguelbermudez/3801411 to your computer and use it in GitHub Desktop.
Extract SVG paths and convert to JSON for use with Raphael.js
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