Skip to content

Instantly share code, notes, and snippets.

@joduplessis
Created December 17, 2017 11:18
Show Gist options
  • Save joduplessis/5f50a5b9548f21d3d217ae70cb1c374b to your computer and use it in GitHub Desktop.
Save joduplessis/5f50a5b9548f21d3d217ae70cb1c374b to your computer and use it in GitHub Desktop.
Add an auto incremented ID to each shape in an SVG file.
import string
svgFile = 'pattern.svg'
svgFileNew = 'pattern1.svg'
with open(svgFile) as f:
thefile = open(svgFileNew, 'w')
lines = f.readlines()
newLines = []
aiid = 0
for line in lines:
newLine = line
index = line.find('/>')
if index != -1:
aiid = aiid + 1
newLine = line.replace('/>', ' id="'+str(aiid)+'" />')
newLines.append(newLine)
# Write the file back
for line in newLines:
thefile.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment