/
SVGtoPDFfromCommaListValuesFile.py
executable file
·76 lines (70 loc) · 2.49 KB
/
SVGtoPDFfromCommaListValuesFile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import sys
import getopt
# Función que lee los parámetros de entrada
def main(argv):
templatefile = ''
listfile = ''
try:
opts, args = getopt.getopt(argv, "ht:l:", ["template=", "list="])
except getopt.GetoptError:
print 'Error: SVGtoPDFfromCommaListValuesFile.py -t <template file> -l <list file>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'Ayuda: SVGtoPDFfromCommaListValuesFile.py -t <template file> -l <list file>'
sys.exit()
elif opt in ("-t", "--template"):
templatefile = arg
elif opt in ("-l", "--list"):
listfile = arg
return [templatefile, listfile]
templatefile = ''
listfile = ''
# Lee los parámetros de entrada
if __name__ == "__main__":
[templatefile, listfile] = main(sys.argv[1:])
# Se abre el archivo separado por comas
try:
file_handle = open(listfile, "r")
lista = file_handle.read()
lista = lista.splitlines()
file_handle.close()
from xml.dom import minidom
xml_documento = minidom.parse(templatefile)
nodos = xml_documento.getElementsByTagName("tspan")
nodo1 = None
nodo2 = None
# Los identificadores deben ser puestos previamente
# en el archivo SVG que sirve como plantilla
ids = ["parametro_1", "parametro_2"]
for nodo in nodos:
if nodo.getAttribute("id") == ids[0]:
nodo1 = nodo
elif nodo.getAttribute("id") == ids[1]:
nodo2 = nodo
# Lee las lineas del archivo especificado como lista CVS
for inputtext in lista:
try:
[tipo, nombre] = inputtext.split(",")
nombre = nombre.decode('utf8')
nodo1.firstChild.replaceWholeText(nombre)
nodo2.firstChild.replaceWholeText(tipo )
out_file = "/tmp/output_generarpdf.svg"
file_handle = open(out_file, "w")
texto = xml_documento.toxml()
file_handle.write(texto.encode('utf8'))
file_handle.close()
import os
outputfile = "Certificado - " + nombre + ".pdf"
comando = "inkscape -f \"%s\" -A \"%s\"" % (out_file, outputfile)
print comando
os.system(comando.encode('utf8'))
os.remove(out_file)
except Exception:
None
except Exception as e:
print 'Ayuda: SVGtoPDFfromCommaListValuesFile.py -t <template file> -l <list file>'
# Se lee el archivo SVG como XML
print "¡¡¡Se ha Terminado!!!"