-
Notifications
You must be signed in to change notification settings - Fork 0
/
twoFacesOnePage.py
402 lines (345 loc) · 16.7 KB
/
twoFacesOnePage.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# Form implementation generated from reading ui file '.\twoFacesOnePage.ui'
#
# Created by: PyQt6 UI code generator 6.4.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
'''
Requeriments.txt
PyPdf2==3.0.1
pyqt6==6.5.1
pyqt6-tools==6.4.2.3.3
'''
'''
Esta aplicación te permite combinar dos archivos PDF escaneados en uno solo, de forma rápida y sencilla.
Solo tienes que cargar un archivo con las páginas impares y otro con las páginas pares en orden inverso, y la aplicación generará automáticamente
un PDF de salida ordenado correctamente.
LeoCroms@2023
'''
from PyQt6.QtWidgets import QMainWindow, QApplication
import sys
import os,time
import PyPDF2
import subprocess
import winsound
from PyQt6 import QtCore, QtGui, QtWidgets
class MainWidget(QMainWindow):
def __init__(self):
super().__init__()
self.setObjectName("MainWindowT")
self.resize(640, 350)
self.setAcceptDrops(True)
self.setMinimumSize(QtCore.QSize(640, 350))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
self.setFont(font)
self.centralwidget = QtWidgets.QWidget(parent=self)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.imparButton = QtWidgets.QPushButton(parent=self.centralwidget)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
self.imparButton.setFont(font)
self.imparButton.setObjectName("imparButton")
self.gridLayout.addWidget(self.imparButton, 0, 0, 1, 1)
self.imparFile = QtWidgets.QLineEdit(parent=self.centralwidget)
self.imparFile.setText("")
self.imparFile.setObjectName("imparFile")
self.gridLayout.addWidget(self.imparFile, 0, 1, 1, 1)
self.parButton = QtWidgets.QPushButton(parent=self.centralwidget)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(10)
font.setBold(True)
self.parButton.setFont(font)
self.parButton.setObjectName("parButton")
self.gridLayout.addWidget(self.parButton, 0, 2, 1, 1)
self.parFile = QtWidgets.QLineEdit(parent=self.centralwidget)
self.parFile.setObjectName("parFile")
self.gridLayout.addWidget(self.parFile, 0, 3, 1, 1)
self.OutputButton = QtWidgets.QPushButton(parent=self.centralwidget)
self.OutputButton.setObjectName("OutputButton")
self.gridLayout.addWidget(self.OutputButton, 2, 0, 1, 1)
self.outFile = QtWidgets.QLineEdit(parent=self.centralwidget)
self.outFile.setObjectName("outFile")
self.gridLayout.addWidget(self.outFile, 2, 1, 1, 3)
self.startButton = QtWidgets.QPushButton(parent=self.centralwidget)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
self.startButton.setFont(font)
self.startButton.setObjectName("startButton")
self.gridLayout.addWidget(self.startButton, 3, 0, 1, 1)
self.progressBar = QtWidgets.QProgressBar(parent=self.centralwidget)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
self.progressBar.setFont(font)
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName("progressBar")
self.gridLayout.addWidget(self.progressBar, 3, 1, 1, 3)
self.listWidget = QtWidgets.QListWidget(parent=self.centralwidget)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
self.listWidget.setFont(font)
self.listWidget.setObjectName("listWidget")
self.gridLayout.addWidget(self.listWidget, 1, 0, 1, 4)
self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
self.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(parent=self)
self.menubar.setEnabled(True)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 23))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
self.menubar.setFont(font)
self.menubar.setAutoFillBackground(False)
self.menubar.setObjectName("menubar")
self.menuAbout = QtWidgets.QMenu(parent=self.menubar)
self.menuAbout.setGeometry(QtCore.QRect(269, 128, 274, 72))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
self.menuAbout.setFont(font)
self.menuAbout.setToolTip("")
self.menuAbout.setObjectName("menuAbout")
self.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(parent=self)
self.statusbar.setObjectName("statusbar")
self.setStatusBar(self.statusbar)
self.actionTwoFacesOnePage_LeoCroms2023 = QtGui.QAction(parent=self)
self.actionTwoFacesOnePage_LeoCroms2023.setObjectName("actionTwoFacesOnePage_LeoCroms2023")
self.menuAbout.addAction(self.actionTwoFacesOnePage_LeoCroms2023)
self.menubar.addAction(self.menuAbout.menuAction())
self.retranslateUi(self)
self.imparFile.editingFinished.connect(self.imparFile_edit) # type: ignore
self.parFile.editingFinished.connect(self.parFile_edit) # type: ignore
self.outFile.editingFinished.connect(self.outFile_edit) # type: ignore
self.imparButton.clicked.connect(self.imparButton_click) # type: ignore
self.parButton.clicked.connect(self.parButton_click) # type: ignore
self.OutputButton.clicked.connect(self.OutputButton_click) # type: ignore
self.startButton.clicked.connect(self.startButton_click) # type: ignore
self.startButton.setEnabled(False)
global checkImpar
global checkPar
global checkDir
checkImpar = False
checkPar = False
checkDir = False
QtCore.QMetaObject.connectSlotsByName(self)
def retranslateUi(self, MainWindowT):
_translate = QtCore.QCoreApplication.translate
self.setWindowTitle (_translate("MainWindowT", "TwoFacesOnePage"))
self.imparButton.setToolTip(_translate("MainWindowT", "Selecciona el fichero pdf con la pagina impares"))
self.imparButton.setText(_translate("MainWindowT", "Impar File"))
self.imparFile.setToolTip(_translate("MainWindowT", "Pdf con las paginas impares"))
self.parButton.setToolTip(_translate("MainWindowT", "Selecciona el fichero pdf con la pagina pares en orden inverso"))
self.parButton.setText(_translate("MainWindowT", "Par File"))
self.parFile.setToolTip(_translate("MainWindowT", "Pdf con las paginas pares en orden inverso"))
self.OutputButton.setToolTip(_translate("MainWindowT", "Selecciona la carpeta de destino para el nuevo fichero pdf combinado"))
self.OutputButton.setText(_translate("MainWindowT", "Output Dir"))
self.outFile.setToolTip(_translate("MainWindowT", "Carpeta de destino"))
self.startButton.setToolTip(_translate("MainWindowT", "GoGoGooo"))
self.startButton.setText(_translate("MainWindowT", "Start"))
self.menubar.setToolTip(_translate("MainWindowT", "Acerca de ..."))
self.menuAbout.setTitle(_translate("MainWindowT", "About"))
self.actionTwoFacesOnePage_LeoCroms2023.setText(_translate("MainWindowT", "TwoFacesOnePage@LeoCroms2023"))
def imparButton_click(self):
global imparfilename
global checkImpar
global checkPar
global checkDir
imparfilename = QtWidgets.QFileDialog.getOpenFileName(None,'Selecciona Fichero con las paginas impares',None,("Pdf Files (*.pdf)"))
imparfilename=imparfilename[0]
if imparfilename != "" :
imparbasename= os.path.split(imparfilename)
self.imparFile.setText(imparfilename)
self.listWidget.addItem(f"El fichero con las paginas impares es {imparbasename[1]}")
self.listWidget.scrollToBottom()
checkImpar=True
self.progressBar.setProperty("value", 0)
if ((checkImpar and checkPar and checkDir)==True): self.startButton.setEnabled(True)
else:
self.imparFile.setText(imparfilename)
self.listWidget.addItem("No se ha seleccionado ningun fichero")
self.listWidget.scrollToBottom()
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
checkImpar=False
self.startButton.setEnabled(False)
self.progressBar.setProperty("value", 0)
def parButton_click(self):
global parfilename
global checkImpar
global checkPar
global checkDir
parfilename= QtWidgets.QFileDialog.getOpenFileName(None,'Selecciona Fichero con las paginas pares',None,("Pdf Files (*.pdf)"))
parfilename=parfilename[0]
if parfilename!="":
parbasename=os.path.split(parfilename)
self.parFile.setText(parfilename)
self.listWidget.addItem(f"El fichero con las paginas pares es {parbasename[1]}")
self.listWidget.scrollToBottom()
checkPar= True
self.progressBar.setProperty("value", 0)
if (checkImpar==True and checkPar == True and checkDir==True): self.startButton.setEnabled(True)
else:
self.parFile.setText(parfilename)
self.listWidget.addItem("No se ha seleccionado ningun fichero")
self.listWidget.scrollToBottom()
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
checkPar=False
self.startButton.setEnabled(False)
self.progressBar.setProperty("value", 0)
def OutputButton_click(self):
global dirname
global checkImpar
global checkPar
global checkDir
dirname = QtWidgets.QFileDialog.getExistingDirectory(None,'Selecciona Directorio Destino')
if dirname!="":
self.outFile.setText(dirname)
self.listWidget.scrollToBottom()
checkDir=True
if (checkImpar==True and checkPar == True and checkDir==True): self.startButton.setEnabled(True)
self.progressBar.setProperty("value", 0)
else:
self.outFile.setText(dirname)
self.listWidget.addItem("No se ha seleccionado directorio de destino")
self.listWidget.scrollToBottom()
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
checkDir=False
self.startButton.setEnabled(False)
self.progressBar.setProperty("value", 0)
def startButton_click(self):
try:
pdf_file1 = open(self.imparFile.text(),'rb')
pdf_file2 = open(self.parFile.text(),'rb')
imparPdfFile=PyPDF2.PdfReader(pdf_file1)
parPdfFile=PyPDF2.PdfReader(pdf_file2)
finalPdfFile=PyPDF2.PdfWriter()
dateTime=time.strftime("%Y%M%d%H%M%S")
output_pdf=os.path.join(dirname,dateTime+"finalOutputScan.pdf")
totalPagImpar=len(imparPdfFile.pages)
totalPagPar=len(parPdfFile.pages)
if (totalPagImpar==totalPagPar):
totalPages=totalPagPar
for xx in range(totalPages):
self.progressBar.setValue(int((xx+1)*100/totalPages))
finalPdfFile.add_page(imparPdfFile.pages[xx])
finalPdfFile.add_page(parPdfFile.pages[-(xx+1)])
with open(output_pdf, "wb") as fp:
finalPdfFile.write(fp)
self.listWidget.addItem("Operacion realizada")
self.listWidget.scrollToBottom()
subprocess.Popen(output_pdf,shell=True)
else:
self.listWidget.addItem("Error numero de paginas no se corresponde")
self.listWidget.addItem("los ficheros de entrada deben tener el mismo numero de paginas")
self.listWidget.scrollToBottom()
except:
self.listWidget.addItem("Error desconocido")
self.listWidget.scrollToBottom()
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
def imparFile_edit(self):
global checkImpar
global checkPar
global checkDir
if os.path.exists(self.imparFile.text())==True:
imparfilename=self.imparFile.text()
self.listWidget.addItem(f"El fichero con las paginas impares es: {self.imparFile.text()}")
self.listWidget.scrollToBottom()
checkImpar=True
self.progressBar.setProperty("value", 0)
if ((checkImpar and checkPar and checkDir)==True): self.startButton.setEnabled(True)
else:
self.listWidget.addItem("Error el fichero no existe")
self.listWidget.scrollToBottom()
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
checkImpar=False
self.startButton.setEnabled(False)
self.progressBar.setProperty("value", 0)
pass
def parFile_edit(self):
#global dirname
global checkImpar
global checkPar
global checkDir
if os.path.exists(self.parFile.text())==True:
parfilename=self.parFile.text()
self.listWidget.addItem(f"El fichero con las paginas pares es: {self.parFile.text()}")
self.listWidget.scrollToBottom()
checkPar= True
self.progressBar.setProperty("value", 0)
if (checkImpar==True and checkPar == True and checkDir==True): self.startButton.setEnabled(True)
else:
self.listWidget.addItem("Error el fichero no existe")
self.listWidget.scrollToBottom()
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
checkPar=False
self.startButton.setEnabled(False)
self.progressBar.setProperty("value", 0)
def outFile_edit(self):
if os.path.isdir(self.outFile.text()):
dirname=self.outFile.text()
self.listWidget.addItem(self.outFile.text())
self.listWidget.scrollToBottom()
checkDir=True
if (checkImpar==True and checkPar == True and checkDir==True): self.startButton.setEnabled(True)
self.progressBar.setProperty("value", 0)
else:
self.listWidget.addItem("Error, el directorio no existe")
self.listWidget.scrollToBottom()
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
checkDir=False
self.startButton.setEnabled(False)
self.progressBar.setProperty("value", 0)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
global checkImpar
global checkPar
global checkDir
global parfilename
global imparfilename
files = [u.toLocalFile() for u in event.mimeData().urls()]
for f in files:
#print(f,os.path.getctime(f))
if (checkImpar==False):
imparbasename= os.path.split(f)
#print (imparbasename)
#print (imparbasename[0],imparbasename[1])
imparfilename=f
self.imparFile.setText(imparfilename)
self.listWidget.addItem(f"El fichero con las paginas impares es {imparbasename[1]}")
self.listWidget.scrollToBottom()
checkImpar=True
self.progressBar.setProperty("value", 0)
elif (checkPar==False):
parbasename=os.path.split(f)
parfilename=f
self.parFile.setText(parfilename)
self.listWidget.addItem(f"El fichero con las paginas pares es {parbasename[1]}")
self.listWidget.scrollToBottom()
checkPar= True
self.progressBar.setProperty("value", 0)
if ((checkImpar and checkPar)==True):
if (os.path.getctime(imparfilename)>(os.path.getctime(parfilename))):
self.imparFile.setText(parfilename)
self.parFile.setText(imparfilename)
self.listWidget.addItem(f"Correccion automatica, ficheros de entrada intercambiados")
self.listWidget.scrollToBottom()
if ((checkImpar and checkPar and checkDir)==True): self.startButton.setEnabled(True)
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = MainWidget()
ui.show()
sys.exit(app.exec())