Skip to content

Commit 3ce1a2f

Browse files
authored
Add files via upload
1 parent 7aa2dbc commit 3ce1a2f

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed

LO.py

+215
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
#-------------------------------------------------------------------------------
2+
# Name: Licencias v3
3+
# Purpose: Emisión de PDF con Licencias en funcion de un PDF mayor
4+
# con ciertas caracteristicas, al cual, hay que borrarle y agregarle cosas en una
5+
# pagina y agregarle paginas modelo.
6+
#
7+
# Author: Ing. Esp. Leonardo Barenghi
8+
#
9+
# Created: 14/09/2021
10+
# Copyright: (c) lbarenghi 2021 https://github.com/leogbar/Licencias
11+
# Licence: BSD 3
12+
#-------------------------------------------------------------------------------
13+
14+
import PyPDF2 as pypdf #https://mstamy2.github.io/PyPDF2/
15+
from unidecode import unidecode #(c) Tomaz Solc (GPLv2+) (GPL) https://pypi.org/project/Unidecode/ https://github.com/avian2/unidecode
16+
from reportlab.lib.pagesizes import A4 #(c) Andy Robinson, Robin Becker, the ReportLab team and the community (BSD) https://pypi.org/project/reportlab/
17+
from reportlab.lib.units import mm
18+
from reportlab.pdfgen.canvas import Canvas
19+
from reportlab.pdfbase.ttfonts import TTFont
20+
from reportlab.pdfbase import pdfmetrics
21+
from reportlab.pdfbase.pdfmetrics import stringWidth
22+
import datetime
23+
import PySimpleGUI as sg #(LGPLv3+) PySimpleGUI https://github.com/PySimpleGUI/PySimpleGUI
24+
from sys import platform
25+
26+
if platform == "linux" or platform == "linux2":
27+
separador = "/"
28+
elif platform == "win32" or platform == "win64":
29+
separador = "\\"
30+
31+
32+
salida=[]
33+
pdfmetrics.registerFont(TTFont('Arial', 'Arial.ttf'))
34+
pdfmetrics.registerFont(TTFont('Arial-Bold', 'Arial.ttf'))
35+
36+
def current_date_format(date):
37+
months = ("Enero", "Febrero", "Marzo", "Abri", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre")
38+
day = date.day
39+
month = months[date.month - 1]
40+
year = date.year
41+
messsage = "{} de {} del {}".format(day, month, year)
42+
return messsage
43+
44+
now = datetime.datetime.now()
45+
46+
47+
event, values = sg.Window('Generacion de Licencias v1', [[sg.Text('(c) Ing. Esp. Leonardo Barenghi - 2021 - Licencia bajo BSD 3')],[sg.Text(' ')],[sg.Text('Ingrese cantidad de dias a partir de hoy para la emisión:')],[sg.InputText()],
48+
[sg.Radio('Autorización de Operación Provisoria', "RADIO1", default=False, key="Licencia0")],
49+
[sg.Radio('Licencia de Operación', "RADIO1", default=False, key="Licencia1")],
50+
[sg.Radio('Registro', "RADIO1", default=False, key="Licencia2")],
51+
[sg.Text(' ')],
52+
[sg.Radio('Una pagina por Documento', "RADIO2", default=False, key="cantidad0")],
53+
[sg.Radio('Un Documento con multiples paginas', "RADIO2", default=False, key="cantidad1")],
54+
[sg.Text('Cargue el Archivo con el/los documento/s de entrada')], [sg.Input(), sg.FileBrowse()],
55+
[sg.Text('Cargue el Archivo que contiene los nombres de las Entidades Responsables:')], [sg.Input(), sg.FileBrowse()],
56+
[sg.Text('Seleccione carpeta de salida:')], [sg.Input(),sg.FolderBrowse()], [sg.OK(), sg.Cancel()]
57+
58+
]).read(close=True)
59+
60+
encabezadof=""
61+
adjuntof=""
62+
63+
if values["Licencia0"] == True:
64+
encabezadof = "EncLicProv.pdf"
65+
adjuntof ="AdjLicProv.pdf"
66+
titulo = "Aut_Op_Prov"
67+
else:
68+
if values["Licencia1"] == True:
69+
encabezadof ="EncLicOp.pdf"
70+
adjuntof ="AdjLicOp.pdf"
71+
titulo = "Lic_Op"
72+
else:
73+
74+
if values["Licencia2"] == True:
75+
encabezadof = "EncReg.pdf"
76+
adjuntof ="AdjReg.pdf"
77+
titulo = "Reg"
78+
else:
79+
print("Operación invalida")
80+
exit()
81+
82+
83+
explorar = values["Browse"]
84+
nombres = values["Browse0"]
85+
canvas = Canvas("firma.pdf", pagesize=A4)
86+
if len(values[0])<1:
87+
print("No ingresó la cantidad de días...")
88+
exit()
89+
else:
90+
dias = int(values[0])
91+
92+
AnchoA4 =A4[0]
93+
94+
FirmaIni=open("firma.ini","r", encoding="utf-8")
95+
NombreFirma=FirmaIni.readline()
96+
PuestoFirma1=FirmaIni.readline()
97+
PuestoFirma2=FirmaIni.readline()
98+
99+
100+
dias=datetime.timedelta(days=dias)
101+
lugar = "BUENOS AIRES, "
102+
fecha = current_date_format(now+dias)
103+
lugarFecha = lugar + fecha
104+
canvas.setFont("Arial", 10)
105+
106+
tamanoTexto = stringWidth(lugarFecha, "Arial", 10)
107+
ubicacionTexto=(AnchoA4*3/4-tamanoTexto)/2
108+
canvas.drawString(ubicacionTexto, 60, lugarFecha )
109+
110+
tamanoTexto = stringWidth(NombreFirma, "Arial", 10)
111+
ubicacionTexto=(AnchoA4*3/4-tamanoTexto)/2
112+
canvas.drawString(ubicacionTexto, 50, NombreFirma[:-1]) #Nombre de quien firma
113+
114+
canvas.setFont("Times-Roman", 8)
115+
116+
tamanoTexto = stringWidth(PuestoFirma1, "Times-Roman", 8)
117+
ubicacionTexto=(AnchoA4*3/4-tamanoTexto)/2
118+
canvas.drawString(ubicacionTexto, 40, PuestoFirma1[:-1]) #Area a la que pertenece
119+
120+
tamanoTexto = stringWidth(PuestoFirma2, "Times-Roman", 8)
121+
ubicacionTexto=(AnchoA4*3/4-tamanoTexto)/2
122+
canvas.drawString(ubicacionTexto, 30, PuestoFirma2[:-1]) #Area a la que pertenece
123+
124+
canvas.save()
125+
126+
encabezado=open(encabezadof, "rb")
127+
firma=open("firma.pdf", "rb")
128+
borrar=open("borrar.pdf", "rb")
129+
linea=open("linea.pdf", "rb")
130+
adjunto=open(adjuntof, "rb")
131+
anexo=open("anexo.pdf", "rb")
132+
anexoBorrar=open("anexoBorrar.pdf", "rb")
133+
carpeta= values["Browse1"]
134+
if len(explorar)<1:
135+
print("No ingresó el archivo con las Licencias...")
136+
exit()
137+
else:
138+
recorrer=open(explorar, "rb")
139+
140+
if len(nombres)<1:
141+
nombres="Licencia"
142+
else:
143+
nombres=open(nombres,"r", encoding="utf-8")
144+
if len(carpeta)<1:
145+
print("No ingresó el archivo con la carpeta de salida...")
146+
exit()
147+
148+
borrarPdf = pypdf.PdfFileReader(borrar).getPage(0)
149+
encabezadoPdf = pypdf.PdfFileReader(encabezado).getPage(0)
150+
firmaPdf = pypdf.PdfFileReader(firma).getPage(0)
151+
adjuntoPdf = pypdf.PdfFileReader(adjunto).getPage(0)
152+
lineaPdf = pypdf.PdfFileReader(linea).getPage(0)
153+
anexoPdf = pypdf.PdfFileReader(anexo).getPage(0)
154+
anexoBorrarPdf = pypdf.PdfFileReader(anexoBorrar).getPage(0)
155+
original = pypdf.PdfFileReader(recorrer)
156+
x=1
157+
158+
if values["cantidad0"] == True:
159+
for i in range(original.getNumPages()):
160+
archivo=nombres.readline()
161+
licencia1 = pypdf.PdfFileWriter()
162+
licencia1=original.getPage(i)
163+
164+
licencia1.mergePage(borrarPdf)
165+
licencia1.mergePage(encabezadoPdf)
166+
licencia1.mergePage(lineaPdf)
167+
licencia1.mergePage(firmaPdf)
168+
169+
licencia = pypdf.PdfFileWriter()
170+
licencia.addPage(licencia1)
171+
licencia.addPage(adjuntoPdf)
172+
173+
archivo = unidecode(archivo)
174+
archivo=archivo[:-1]
175+
salida=carpeta+separador+str(x)+"_"+archivo+".pdf"
176+
x=x+1
177+
print("Generando "+salida+ "..."+"\n")
178+
with open(salida, "wb") as outFile:
179+
licencia.write(outFile)
180+
print("Proceso Terminado..."+"\n")
181+
exit()
182+
else:
183+
if values["cantidad1"] == True:
184+
fecha='{:%Y%m%d-%H%M%S}'.format(now)
185+
archivo=titulo+"_"+fecha
186+
licencia = pypdf.PdfFileWriter()
187+
188+
for i in range(original.getNumPages()):
189+
if(x==1):
190+
licencia1 = pypdf.PdfFileWriter()
191+
licencia1=original.getPage(i)
192+
licencia1.mergePage(borrarPdf)
193+
licencia1.mergePage(encabezadoPdf)
194+
licencia1.mergePage(lineaPdf)
195+
licencia1.mergePage(firmaPdf)
196+
licencia.addPage(licencia1)
197+
licencia.addPage(adjuntoPdf)
198+
x=x+1
199+
else:
200+
licencia1 = pypdf.PdfFileWriter()
201+
licencia1=original.getPage(i)
202+
licencia1.mergePage(anexoBorrarPdf)
203+
licencia1.mergePage(anexoPdf)
204+
licencia.addPage(licencia1)
205+
x=x+1
206+
salida=carpeta+separador+archivo+".pdf"
207+
print("Generando "+salida+ "..."+"\n")
208+
209+
with open(salida, "wb") as outFile:
210+
licencia.write(outFile)
211+
print("Proceso Terminado..."+"\n")
212+
#exit()
213+
else:
214+
print("No ingresó la cantidad de hojas...")
215+
#exit()

0 commit comments

Comments
 (0)