-
Notifications
You must be signed in to change notification settings - Fork 8
/
pdf_merge.py
executable file
·112 lines (79 loc) · 3.19 KB
/
pdf_merge.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
import tkinter as tk
from tkinter import filedialog
import os
from pikepdf import Pdf
class DragDropListbox(tk.Listbox):
""" A Tkinter listbox with drag'n'drop reordering of entries. """
def __init__(self, master, **kw):
kw['selectmode'] = tk.SINGLE
tk.Listbox.__init__(self, master, kw)
self.bind('<Button-1>', self.setCurrent)
self.bind('<B1-Motion>', self.shiftSelection)
self.curIndex = None
def setCurrent(self, event):
self.curIndex = self.nearest(event.y)
def shiftSelection(self, event):
i = self.nearest(event.y)
if i < self.curIndex:
x = self.get(i)
self.delete(i)
self.insert(i+1, x)
self.curIndex = i
elif i > self.curIndex:
x = self.get(i)
self.delete(i)
self.insert(i-1, x)
self.curIndex = i
class pdfMergerApp():
def __init__(self):
self.pdfFolder = ""
self.root = tk.Tk()
self.root.title("PDF Merger")
self.folderSelectButton = tk.Button(self.root, text="Choose a folder", command=self.folderSelect, height=4)
self.folderSelectButton.grid(row=0, column=0, sticky="WE")
self.pdfListbox = DragDropListbox(self.root, width=60, font=('Arial', 15))
self.pdfListbox.grid(row=1, column=0, columnspan=2)
self.pdfMergeButton = tk.Button(self.root, text="Merge into PDF", command=self.pdfMergeFunction, height=4)
self.pdfMergeButton.grid(row=0, column=1, sticky="WE")
self.root.mainloop()
def folderSelect(self):
self.pdfFolder = filedialog.askdirectory()
myFiles = [fileName for fileName in os.listdir(self.pdfFolder) if os.path.splitext(fileName)[1] == '.pdf']
itemNumber = self.pdfListbox.size()
self.pdfListbox.delete(0, itemNumber)
self.pdfListbox.insert('end', *myFiles)
newItemNumber = len(myFiles)
if newItemNumber <= 20:
self.pdfListbox.config(height=newItemNumber)
else:
self.pdfListbox.config(height=20)
print(f"Listbox updated with pdf files in folder {self.pdfFolder}")
def pdfMergeFunction(self):
if self.pdfFolder == "":
itemNumber = self.pdfListbox.size()
self.pdfListbox.delete(0, itemNumber)
self.pdfListbox.insert('end', 'You need to choose a folder first!')
return
itemAmount = self.pdfListbox.size()
fileList = self.pdfListbox.get(0, itemAmount - 1)
fileList = list(fileList)
files = [self.pdfFolder + "/" + fileName for fileName in fileList]
pdf = Pdf.new()
for file in files:
src = Pdf.open(file)
pdf.pages.extend(src.pages)
try:
os.remove(self.pdfFolder + "/merged/OUTPUT.pdf")
except:
pass
try:
os.makedirs(self.pdfFolder + '/merged')
except:
pass
pdf.save(self.pdfFolder + '/merged/OUTPUT.pdf')
print('Success!')
itemNumber = self.pdfListbox.size()
self.pdfListbox.delete(0, itemNumber)
self.pdfListbox.insert('end', 'Merge Successful!')
self.pdfFolder = ""
pdfMergerApp()