-
Notifications
You must be signed in to change notification settings - Fork 4
/
pypdftk.py
706 lines (640 loc) · 27.2 KB
/
pypdftk.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
The Python PDF Toolkit
Copyright ©2016 Ronan Paixão
Licensed under the terms of the MIT License.
See LICENSE.txt for details.
@author: Ronan Paixão
"""
from __future__ import division, unicode_literals, print_function
import sys
import os
import os.path as osp
import uuid
try:
from cStringIO import StringIO as BytesIO
except ModuleNotFoundError: # Py3
from io import BytesIO
import subprocess
from decimal import Decimal, InvalidOperation
import copy
import ctypes
import traceback
#%% Setup PyQt's v2 APIs
import sip
API_NAMES = ["QDate", "QDateTime", "QString", "QTextStream", "QTime", "QUrl",
"QVariant"]
API_VERSION = 2
for name in API_NAMES:
sip.setapi(name, API_VERSION)
#%%
from qtpy import QtCore, QtGui, QtWidgets, uic
import PyPDF2 as pdf
from PIL import Image
import pdf_images
# Need to import promoted qt classes, to make py2exe process them.
import dragdroplist
import six
if sys.platform == 'darwin':
def show_file(path):
subprocess.Popen(['open', '--', path])
def open_default_program(path):
subprocess.Popen(['start', path])
elif sys.platform == 'linux2':
def show_file(path):
subprocess.Popen(['xdg-open', '--', path])
def open_default_program(path):
subprocess.Popen(['xdg-open', path])
elif sys.platform == 'win32':
def show_file(path):
subprocess.Popen(['explorer', '/select,', path])
open_default_program = os.startfile
#%% PyInstaller Utilities
def frozen(filename):
"""Returns the filename for a frozen file (program file which may be
included inside the executable created by PyInstaller).
"""
if getattr(sys, 'frozen', False):
return osp.join(sys._MEIPASS, filename)
else:
return filename
#%%
class Page(object):
def __init__(self):
self.tmp = BytesIO()
self.uuid = uuid.uuid4()
self.obj = None
self.transforms = ""
self._numbers = []
self._basename = "Invalid Page"
def reload_from_buffer(self):
self.tmp.seek(0)
reader = pdf.PdfFileReader(self.tmp, strict=False)
self.obj = reader.getPage(0)
@classmethod
def from_file(cls, filename):
pages = []
with open(filename, 'rb') as f:
reader = pdf.PdfFileReader(f, strict=False)
total_pages = reader.getNumPages()
for page_number in range(total_pages):
page = Page()
output = pdf.PdfFileWriter()
output.addPage(reader.getPage(page_number))
output.write(page.tmp)
page.reload_from_buffer()
page._numbers = [str(page_number + 1)]
page._basename = osp.basename(filename)
pages.append(page)
return pages
@classmethod
def from_image(cls, filename, page_size_cm):
page = Page()
page.tmp = pdf_images.image_to_pdf(filename, page_size_cm)
page.reload_from_buffer()
page._basename = osp.basename(filename)
page._numbers = ["I"]
return page
def rotateLeft(self):
self.obj.rotateCounterClockwise(90)
self.transforms += '↺'
if self.transforms.endswith('↻↺'):
self.transforms = self.transforms[:-2]
if self.transforms.endswith('↺↺↺'):
self.transforms = self.transforms[:-3]+'↻'
def rotateRight(self):
self.obj.rotateClockwise(90)
self.transforms += '↻'
if self.transforms.endswith('↺↻'):
self.transforms = self.transforms[:-2]
if self.transforms.endswith('↻↻↻'):
self.transforms = self.transforms[:-3]+'↺'
@classmethod
def merge_pageobjs(cls, page1, page2, tx, ty):
rotation = int(page2.get("/Rotate") or 0) % 360
scale = Decimal(1.)
# rotation is clockwise. tx, ty in page1 coordinates: x = right; y = up
if rotation == 90:
# tx += 0
ty += page1.mediaBox.getHeight()
elif rotation == 180:
tx += page2.mediaBox.getWidth()*scale
ty += page1.mediaBox.getHeight()
elif rotation == 270:
tx += page2.mediaBox.getHeight()*scale
ty += page1.mediaBox.getHeight() - page2.mediaBox.getWidth()*scale
page1.mergeRotatedScaledTranslatedPage(page2, -rotation, scale, tx, ty)
def merge(self, page, tx=0.0, ty=0.0, op="merge"):
"""op in ['merge', 'stamp', 'background']"""
assert(op in ["merge", "stamp", "background"])
if op == "background":
page0 = pdf.pdf.PageObject.createBlankPage(self.tmp,
self.obj.mediaBox.getWidth(), self.obj.mediaBox.getHeight())
Page.merge_pageobjs(page0, page.obj, tx, ty)
tx = ty = 0
page2 = self.obj
self.obj = page0
else:
page2 = page.obj
self.merge_pageobjs(self.obj, page2, tx, ty)
# Adjust name
if op == "merge":
if self._basename == page._basename:
self._numbers.append(','.join(page._numbers))
else:
self._numbers.append(u"{0}<{1}>".format(page._basename,
','.join(page._numbers)))
self.transforms += "M"
elif op == "stamp":
self.transforms += "⊙"
elif op == "background":
self.uuid = page.uuid
self._numbers = page._numbers
self.transforms = page.transforms + "▣"
self._basename = page._basename
@property
def name(self):
return u"{0}<{1}>[{2}]".format(self._basename,
','.join(self._numbers),
self.transforms)
class WndMain(QtWidgets.QMainWindow):
######################
### Initialization ###
######################
def __init__(self, *args, **kwargs):
super(WndMain, self).__init__(*args, **kwargs)
# Setup settings storage
self.settings = QtCore.QSettings("settings.ini",
QtCore.QSettings.IniFormat)
# Initialize UI (open main window)
self.initUI()
self.pages = {}
self.last_file = None
def initUI(self):
ui_file = frozen(osp.join('data', 'wndmain.ui'))
uic.loadUi(ui_file, self)
self.supported_files = self.tr("Supported files (*.pdf *.jpg *.jpeg"
" *.bmp *.gif *.j2p *.jpx *.png *.tiff)"
";;PDF file (*.pdf)"
";;Image file (*.jpg *.jpeg *.bmp *.gif"
" *.j2p *.jpx *.png *.tiff)"
";;All files (*.*)")
validator = QtGui.QDoubleValidator()
self.lineStampX.setValidator(validator)
self.lineStampY.setValidator(validator)
self.lineFileDPI.setValidator(validator)
# Load window geometry and state
self.restoreGeometry(self.settings.value("geometry", b""))
self.restoreState(self.settings.value("windowState", b""))
self.show()
### Slots and accessory methods
def open_file(self, filename):
filename = filename.replace("/", osp.sep)
if not osp.exists(filename):
errormsg = self.tr("File <{}> doesn't exist.").format(filename)
QtWidgets.QMessageBox.warning(self, self.tr("Error"), errormsg)
return
item = QtWidgets.QListWidgetItem(osp.basename(filename))
item.setData(QtCore.Qt.ToolTipRole, filename)
self.listFiles.addItem(item)
def load_pages(self, filename):
pages = []
try:
image_exts = ['.jpg', '.jpeg', '.bmp', '.gif', '.j2p', '.jpx',
'.png', '.tiff']
basefile, ext = osp.splitext(filename.lower())
if ext == '.pdf':
pages = Page.from_file(filename)
elif ext in image_exts:
img_size = Image.open(filename).size
try:
dpi = float(self.lineFileDPI.text())
except:
dpi = float(self.lineFileDPI.placeholderText())
img_size = (img_size[0]/dpi*2.54, img_size[1]/dpi*2.54)
pages.append(Page.from_image(filename, img_size))
except Exception as e:
try:
errormsg = (self.tr("Could not load <{}>:\n{}")
.format(filename, traceback.format_exc()))
except:
errormsg = (self.tr("Could not load <{}>:\n{}")
.format(filename, e.message))
QtWidgets.QMessageBox.warning(self, self.tr("Error"), errormsg)
return pages
def on_listFiles_dropped(self, links):
print("filesDropped:", links)
for link in links:
self.open_file(link)
@QtCore.Slot()
def on_btnFileAdd_clicked(self):
filenames = QtWidgets.QFileDialog.getOpenFileNames(self,
self.tr('Open file'),
"",
self.supported_files)[0]
for filename in filenames:
self.open_file(filename)
@QtCore.Slot()
def on_btnFileRem_clicked(self):
for item in self.listFiles.selectedItems():
self.listFiles.takeItem(self.listFiles.row(item))
@QtCore.Slot()
def on_btnFileTop_clicked(self):
rows = [self.listFiles.row(item) for item in self.listFiles.selectedItems()]
rows.sort(reverse=True)
for item in [self.listFiles.item(row) for row in rows]:
row = self.listFiles.row(item)
self.listFiles.insertItem(0, self.listFiles.takeItem(row))
for row in range(len(rows)):
self.listFiles.setCurrentRow(row, QtCore.QItemSelectionModel.Select)
@QtCore.Slot()
def on_btnFileUp_clicked(self):
rows = [self.listFiles.row(item) for item in self.listFiles.selectedItems()]
rows.sort()
# Ignore rows already on top
first_rows = []
first_row = 0
for row in rows[:]:
if row == first_row:
first_rows.append(row)
rows.remove(row)
first_row += 1
for item in [self.listFiles.item(row) for row in rows]:
row = self.listFiles.row(item)
self.listFiles.insertItem(row-1, self.listFiles.takeItem(row))
sys.stdout.flush()
for row in first_rows + list(map(lambda r: r-1, rows)):
self.listFiles.setCurrentRow(row, QtCore.QItemSelectionModel.Select)
@QtCore.Slot()
def on_btnFileDown_clicked(self):
rows = [self.listFiles.row(item) for item in self.listFiles.selectedItems()]
rows.sort(reverse=True)
# Ignore rows already on top
last_rows = []
last_row = self.listFiles.count() - 1
for row in rows[:]:
if row == last_row:
last_rows.append(row)
rows.remove(row)
last_row -= 1
for item in [self.listFiles.item(row) for row in rows]:
row = self.listFiles.row(item)
self.listFiles.insertItem(row+1, self.listFiles.takeItem(row))
sys.stdout.flush()
for row in last_rows + list(map(lambda r: r+1, rows)):
self.listFiles.setCurrentRow(row, QtCore.QItemSelectionModel.Select)
@QtCore.Slot()
def on_btnFileBottom_clicked(self):
rows = [self.listFiles.row(item) for item in self.listFiles.selectedItems()]
rows.sort()
last = self.listFiles.count() - 1
for item in [self.listFiles.item(row) for row in rows]:
row = self.listFiles.row(item)
self.listFiles.insertItem(last, self.listFiles.takeItem(row))
for row in range(len(rows)):
self.listFiles.setCurrentRow(last-row, QtCore.QItemSelectionModel.Select)
def load_pages_from_rows(self, rows):
pitems = []
try:
if self.radioFileLoadSelBef.isChecked():
to_row = self.listPages.row(self.listPages.selectedItems()[0])
elif self.radioFileLoadSelAft.isChecked():
to_row = self.listPages.row(self.listPages.selectedItems()[-1])+1
else:
to_row = self.listPages.count()
except:
to_row = self.listPages.count()
for fitem in [self.listFiles.item(row) for row in rows]:
filename = fitem.data(QtCore.Qt.ToolTipRole)
pages = self.load_pages(filename)
for page in pages:
self.pages[page.uuid] = page
pitem = QtWidgets.QListWidgetItem(page.name)
pitem.setData(QtCore.Qt.UserRole, page.uuid)
if self.radioFileLoadEnd.isChecked():
self.listPages.addItem(pitem)
else:
self.listPages.insertItem(to_row, pitem)
to_row += 1
pitems.append(pitem)
return pitems
@QtCore.Slot()
def on_btnFileLoad_clicked(self):
# sort by row, otherwise it's selection order
rows = [self.listFiles.row(item) for item in self.listFiles.selectedItems()]
rows.sort()
pitems = self.load_pages_from_rows(rows)
self.listPages.selectionModel().clear()
for item in pitems:
item.setSelected(True)
@QtCore.Slot()
def on_btnFileLoadAll_clicked(self):
rows = range(self.listFiles.count())
pitems = self.load_pages_from_rows(rows)
self.listPages.selectionModel().clear()
for item in pitems:
item.setSelected(True)
@QtCore.Slot()
def on_btnFileSortAsc_clicked(self):
self.listFiles.sortItems(QtCore.Qt.AscendingOrder)
@QtCore.Slot()
def on_btnFileSortDesc_clicked(self):
self.listFiles.sortItems(QtCore.Qt.DescendingOrder)
@QtCore.Slot()
def on_btnFilesClear_clicked(self):
self.listFiles.clear()
self.last_file = None
@QtCore.Slot()
def on_btnFileOpenLoc_clicked(self):
filename = self.listFiles.currentItem().data(QtCore.Qt.ToolTipRole)
show_file(filename)
@QtCore.Slot()
def on_btnPageRem_clicked(self):
for item in self.listPages.selectedItems():
page_uuid = item.data(QtCore.Qt.UserRole)
del self.pages[page_uuid]
self.listPages.takeItem(self.listPages.row(item))
@QtCore.Slot()
def on_btnPageClear_clicked(self):
self.listPages.clear()
self.pages = {}
@QtCore.Slot()
def on_btnPageSortAsc_clicked(self):
self.listPages.sortItems(QtCore.Qt.AscendingOrder)
@QtCore.Slot()
def on_btnPageSortDesc_clicked(self):
self.listPages.sortItems(QtCore.Qt.DescendingOrder)
@QtCore.Slot()
def on_btnPageTop_clicked(self):
rows = [self.listPages.row(item) for item in self.listPages.selectedItems()]
rows.sort(reverse=True)
for item in [self.listPages.item(row) for row in rows]:
row = self.listPages.row(item)
self.listPages.insertItem(0, self.listPages.takeItem(row))
item.setSelected(True)
@QtCore.Slot()
def on_btnPageUp_clicked(self):
rows = [self.listPages.row(item) for item in self.listPages.selectedItems()]
rows.sort()
# Ignore rows already on top
first_rows = []
first_row = 0
for row in rows[:]:
if row == first_row:
first_rows.append(row)
rows.remove(row)
first_row += 1
for item in [self.listPages.item(row) for row in rows]:
row = self.listPages.row(item)
self.listPages.insertItem(row-1, self.listPages.takeItem(row))
item.setSelected(True)
@QtCore.Slot()
def on_btnPageDown_clicked(self):
rows = [self.listPages.row(item) for item in self.listPages.selectedItems()]
rows.sort(reverse=True)
# Ignore rows already on top
last_rows = []
last_row = self.listPages.count() - 1
for row in rows[:]:
if row == last_row:
last_rows.append(row)
rows.remove(row)
last_row -= 1
for item in [self.listPages.item(row) for row in rows]:
row = self.listPages.row(item)
self.listPages.insertItem(row+1, self.listPages.takeItem(row))
item.setSelected(True)
@QtCore.Slot()
def on_btnPageBottom_clicked(self):
rows = [self.listPages.row(item) for item in self.listPages.selectedItems()]
rows.sort()
last = self.listPages.count() - 1
for item in [self.listPages.item(row) for row in rows]:
row = self.listPages.row(item)
self.listPages.insertItem(last, self.listPages.takeItem(row))
item.setSelected(True)
@QtCore.Slot()
def on_btnPageRotLeft_clicked(self):
for item in self.listPages.selectedItems():
page_uuid = item.data(QtCore.Qt.UserRole)
page = self.pages[page_uuid]
page.rotateLeft()
item.setText(page.name)
@QtCore.Slot()
def on_btnPageRotRight_clicked(self):
for item in self.listPages.selectedItems():
page_uuid = item.data(QtCore.Qt.UserRole)
page = self.pages[page_uuid]
page.rotateRight()
item.setText(page.name)
@QtCore.Slot()
def on_btnPageMerge_clicked(self):
rows = [self.listPages.row(item) for item in self.listPages.selectedItems()]
rows.sort()
if len(rows)<2:
QtWidgets.QMessageBox.warning(self, self.tr("Warning"),
self.tr("You must select at least "
"two pages to merge."))
return
first_item = self.listPages.item(rows[0])
variant = first_item.data(QtCore.Qt.UserRole)
first_page = self.pages[variant]
for item in [self.listPages.item(row) for row in rows[1:]]:
row = self.listPages.row(item)
merged_page_uuid = item.data(QtCore.Qt.UserRole)
first_page.merge(self.pages[merged_page_uuid])
del self.pages[merged_page_uuid]
self.listPages.takeItem(row)
first_item.setText(first_page.name)
@QtCore.Slot()
def on_btnPageStamp_clicked(self):
if len(self.listPages.selectedItems()) == 0:
QtWidgets.QMessageBox.warning(self, self.tr("Warning"),
self.tr("You must select at least "
"one page to stamp."))
return
try:
tx = Decimal(self.lineStampX.text() or 0)
ty = Decimal(self.lineStampY.text() or 0)
except InvalidOperation:
QtWidgets.QMessageBox.critical(self, self.tr("Error"),
self.tr("x and y must be numbers."))
return
# 1 PDF unit = 1/72 inches
if self.radioStampCm.isChecked():
mult = 72/Decimal(2.54)
else:
mult = 72/Decimal(1.0)
filename = QtWidgets.QFileDialog.getOpenFileName(self,
self.tr('Open file'),
"",
self.supported_files)[0]
page2 = self.load_pages(filename)[0] # Always first page
sys.stdout.flush()
for item in self.listPages.selectedItems():
page1 = self.pages[item.data(QtCore.Qt.UserRole)]
page1.merge(page2, tx*mult, ty*mult, "stamp")
item.setText(page1.name)
@QtCore.Slot()
def on_btnPageBackground_clicked(self):
if len(self.listPages.selectedItems()) == 0:
QtWidgets.QMessageBox.warning(self, self.tr("Warning"),
self.tr("You must select at least "
"one page to apply background."))
return
try:
if self.chkBackgroundStampLoc.isChecked():
tx = Decimal(self.lineStampX.text() or 0)
ty = Decimal(self.lineStampY.text() or 0) # 1 PDF unit = 1/72 inches
if self.radioStampCm.isChecked():
mult = 72/Decimal(2.54)
else:
mult = 72/Decimal(1.0)
else:
tx = ty = Decimal(0)
mult = Decimal(1)
except InvalidOperation:
QtWidgets.QMessageBox.critical(self, self.tr("Error"),
self.tr("x and y must be numbers."))
return
filename = QtWidgets.QFileDialog.getOpenFileName(self,
self.tr('Open file'),
"",
self.supported_files)[0]
page2 = self.load_pages(filename)[0] # Always first page
for item in self.listPages.selectedItems():
page1 = self.pages[item.data(QtCore.Qt.UserRole)]
page1.merge(page2, tx*mult, ty*mult, "background")
item.setText(page1.name)
@QtCore.Slot()
def on_btnPageSelectAll_clicked(self):
self.listPages.selectAll()
@QtCore.Slot()
def on_btnWriteSingle_clicked(self):
if self.listPages.count() == 0:
QtWidgets.QMessageBox.critical(self, self.tr("Error"),
self.tr("No pages to save!"))
return
supported_files = self.tr("PDF file (*.pdf)")
try:
if self.last_file:
filename = self.last_file
else:
filename = self.listFiles.item(0).data(QtCore.Qt.ToolTipRole)
filename = osp.join(osp.dirname(filename), 'output.pdf')
except:
filename = ""
filename = QtWidgets.QFileDialog.getSaveFileName(self,
self.tr('Save file'),
filename,
supported_files)[0]
if filename:
filename = filename.replace("/", osp.sep)
self.last_file = filename
output_pdf = pdf.PdfFileWriter()
rows = range(self.listPages.count())
for item in [self.listPages.item(row) for row in rows]:
page_uuid = item.data(QtCore.Qt.UserRole)
output_pdf.addPage(self.pages[page_uuid].obj)
try:
with open(filename, 'wb') as f:
output_pdf.write(f)
if self.chkOpenOnSave.isChecked():
open_default_program(filename)
except IOError as e:
errmsg = self.tr("I/O error({0}): {1}\n"
"Please check if the file is open in another program."
).format(e.errno, e.strerror)
QtWidgets.QMessageBox.critical(self, self.tr("Error"), errmsg)
@QtCore.Slot()
def on_btnWriteMulti_clicked(self):
if self.listPages.count() == 0:
QtWidgets.QMessageBox.critical(self, self.tr("Error"),
self.tr("No pages to save!"))
return
supported_files = self.tr("PDF file (*.pdf)")
filename = QtWidgets.QFileDialog.getSaveFileName(self,
self.tr('Save file'),
"",
supported_files)[0]
if filename:
filename = filename.replace("/", osp.sep)
fileprefix = osp.splitext(filename)[0]
rows = range(self.listPages.count())
# pre-check filenames to see if we're overwriting something
for i in rows:
filename_i = "{}{:04}.pdf".format(fileprefix, i)
if osp.exists(filename_i):
errmsg = self.tr("File {} already exists!\nWe don't want "
"to overwrite it. Aborting.").format(filename_i)
QtWidgets.QMessageBox.critical(self, self.tr("Error"),
errmsg)
return
for i, item in enumerate([self.listPages.item(row) for row in rows]):
filename_i = "{}{:04}.pdf".format(fileprefix, i)
output_pdf = pdf.PdfFileWriter()
page_uuid = item.data(QtCore.Qt.UserRole)
output_pdf.addPage(self.pages[page_uuid].obj)
with open(filename_i, 'wb') as f:
output_pdf.write(f)
if self.chkOpenOnSave.isChecked():
open_default_program(filename_i)
@QtCore.Slot()
def on_btnExtractImages_clicked(self):
if self.listPages.count() == 0:
QtWidgets.QMessageBox.critical(self, self.tr("Error"),
self.tr("No pages to look for images!"))
return
supported_files = self.tr("Image file prefix (*)")
try:
filename = self.listFiles.item(0).data(QtCore.Qt.ToolTipRole)
filename = osp.splitext(filename)[0] + '_IMG_'
except:
filename = ""
filename = QtWidgets.QFileDialog.getSaveFileName(self,
self.tr('Save file'),
filename,
supported_files)[0]
if filename:
fileprefix, ext = osp.splitext(filename)
rows = range(self.listPages.count())
i = 0
for row, item in [(row, self.listPages.item(row)) for row in rows]:
page_uuid = item.data(QtCore.Qt.UserRole)
try:
i = pdf_images.extract_images(self.pages[page_uuid].obj, filename, i)
except:
QtWidgets.QMessageBox.critical(self, self.tr("Error"),
self.tr("There was a problem extracting images from "
"page {}.<br>Please file a bug report, "
"attaching the problematic file if possible, in <br>"
"<a href='https://github.com/ronanpaixao/PyPDFTK/issues'>"
"https://github.com/ronanpaixao/PyPDFTK/issues"
"</a>".format(row + 1)))
@QtCore.Slot()
def on_btnCredits_clicked(self):
ui_file = frozen(osp.join('data', 'about.ui'))
dialog = QtWidgets.QDialog()
uic.loadUi(ui_file, dialog)
dialog.exec_()
### Method overrides:
def closeEvent(self, e):
# Write window geometry and state to config file
self.settings.setValue("geometry", self.saveGeometry())
self.settings.setValue("windowState", self.saveState())
e.accept()
#%%
if __name__ == '__main__':
myappid = u'br.com.dapaixao.pypdftk.1.0'
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
existing = QtWidgets.qApp.instance()
if existing:
app = existing
else:
app = QtWidgets.QApplication(sys.argv)
wnd = WndMain()
if existing:
self = wnd
else:
sys.exit(app.exec_())