Created
February 9, 2016 15:33
-
-
Save mraerino/a568e99d64667fb72cda to your computer and use it in GitHub Desktop.
svg generator script i hacked for automation of email header generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from xml.sax.saxutils import escape as xmlescape | |
import subprocess | |
import os | |
import re | |
import sys | |
def get_valid_filename(s): | |
""" | |
Returns the given string converted to a string that can be used for a clean | |
filename. Specifically, leading and trailing spaces are removed; other | |
spaces are converted to underscores; and anything that is not a unicode | |
alphanumeric, dash, underscore, or dot, is removed. | |
>>> get_valid_filename("john's portrait in 2004.jpg") | |
'johns_portrait_in_2004.jpg' | |
""" | |
s = s.strip().replace(' ', '_') | |
return re.sub(r'(?u)[^-\w.]', '', s) | |
template = "20160209_gradar_header_vorlage.svg" | |
key = "$subject" | |
replacements = [ | |
"welcome", | |
"willkommen", | |
"bienvenido/a", | |
"thank you", | |
"vielen dank", | |
"gracias", | |
"reminder", | |
"erinnerung", | |
"recordatorio", | |
"last reminder", | |
"letzte erinnerung", | |
"último recordatorio", | |
"your impression", | |
"erster eindruck", | |
"primera impresión", | |
"your feedback", | |
"erstes feedback", | |
"su opinión", | |
"missing you", | |
"sie fehlen uns", | |
"nos hace falta", | |
"please confirm", | |
"bitte bestätigen", | |
"confirme su cuenta" | |
] | |
genfile = ".gen.svg" | |
outdir = "output" | |
if not os.path.exists(outdir): | |
os.makedirs(outdir) | |
if __name__ == '__main__': | |
print("opening template") | |
with open(template, 'r') as fp: | |
svgstr = fp.read() | |
for index, value in enumerate(replacements): | |
print(value) | |
svggen = svgstr.replace(key, xmlescape(str(value))) | |
with open(genfile, 'w') as genfp: | |
genfp.write(svggen) | |
genfp.close() | |
errorReturn = subprocess.check_output( | |
'inkscape --export-png={0}/{1:02d}_{2}.png {3} >/dev/null 2>&1'. | |
format(outdir, index, get_valid_filename(value), genfile), | |
shell=True, universal_newlines=True) | |
if errorReturn != '': | |
print("inkscape exitted with error\n" + errorReturn) | |
sys.exit(42) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment