-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfMerger.pyw
More file actions
333 lines (306 loc) · 14.4 KB
/
pdfMerger.pyw
File metadata and controls
333 lines (306 loc) · 14.4 KB
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
#!python3
import os
import sys
from tkinter import *
from tkinter import _setit, filedialog
from tkinter.ttk import *
from ttkthemes.themed_tk import *
import tkinter.messagebox as messagebox
import PyPDF2
import subprocess
import img2pdf
f = open(os.devnull, 'w') # change for debug
sys.stderr = sys.stdout = f # must redirect stderr/stdout in .pyw files, since theres no terminal
class pdfLine():
# Defines a GUI line with file input, and start/end buttons to pick pages
def __init__(self, app, name, lineNum):
# initiate all of the line's widgets, and add them to the grid in line "lineNum"
self.lineNum = lineNum
self.label = [Label(app, text=name)]
self.label[0].grid(row=lineNum, column=0)
self.file = Entry(app, state='readonly')
self.file.grid(row=lineNum, column=1)
self.dir_button = Button(app, text='...',
command=self.Browse) # make a button with press action - call self.Browse()
self.dir_button.grid(row=lineNum, column=2)
self.PageStart = StringVar(app)
self.PageEnd = StringVar(app)
self.label.append(Label(app, text='Start'))
self.label[1].grid(row=lineNum, column=3)
self.startPop = OptionMenu(app, self.PageStart, 0)
self.startPop.grid(row=lineNum, column=4)
self.label.append(Label(app, text='End'))
self.label[2].grid(row=lineNum, column=5)
self.endPop = OptionMenu(app, self.PageEnd, 0)
self.endPop.grid(row=lineNum, column=6)
def grid_remove(self):
# remove all of the line's widgets from the grid
for label in self.label:
label.grid_forget()
self.file.grid_forget()
self.dir_button.grid_forget()
self.startPop.grid_forget()
self.endPop.grid_forget()
def Browse(self):
# used for the '...' button, opens a file dialog to get pdf name, and puts it into the entry
dir = filedialog.askopenfilename(filetypes=[("PDF Files", "*.pdf")])
if dir == '':
return
self.file.configure(state='normal')
self.file.delete(0, 'end')
self.file.insert(0, dir)
self.file.configure(state='readonly')
self.pdfPageOptions()
def pdfPageOptions(self):
# update the start/end options with relevant page numbers
pdfFile = open(self.file.get(), 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFile)
pageNum = pdfReader.getNumPages()
pdfFile.close()
if pageNum == 0:
return
self.startPop['menu'].delete(0, 'end') # remove old options from start/end
self.PageStart.set('1')
self.endPop['menu'].delete(0, 'end')
self.PageEnd.set(pageNum)
for i in range(1, pageNum + 1): # add the new options
self.startPop['menu'].add_command(label=i, command=_setit(self.PageStart, i))
self.endPop['menu'].add_command(label=i, command=_setit(self.PageEnd, i))
def getEntry(self, _type):
if _type == 'dir':
return self.file.get()
elif _type == 'start':
return self.PageStart.get()
elif _type == 'end':
return self.PageEnd.get()
class outLine():
# Defines a GUI line with output folder and output filename input
def __init__(self, app, name, lineNum):
# initiate all of the line's widgets, and add them to the grid in line "lineNum"
self.name_label = Label(app, text=name)
self.name_label.grid(row=lineNum, column=0)
self.dir = Entry(app)
self.dir.insert(0,os.getcwd())
self.dir.configure(state='readonly')
self.dir.grid(row=lineNum, column=1)
self.dir_button = Button(app, text='...', command=self.Browse)
self.dir_button.grid(row=lineNum, column=2)
self.out_label = Label(app, text='File name:')
self.out_label.grid(row=lineNum, column=3)
self.outName = Entry(app)
self.outName.insert(0, 'MergedPDF')
self.outName.grid(row=lineNum, column=4, columnspan=3)
def grid_remove(self):
# remove all of the line's widgets from the grid
self.name_label.grid_forget()
self.dir.grid_forget()
self.dir_button.grid_forget()
self.out_label.grid_forget()
self.outName.grid_forget()
def Browse(self):
# used for the '...' button
dir = filedialog.askdirectory()
if dir == '':
return
self.dir.configure(state='normal')
self.dir.delete(0, 'end')
self.dir.insert(0, dir)
self.dir.configure(state='readonly')
def getEntry(self, _type):
if _type == 'dir':
return self.dir.get()
if _type == 'out':
return self.outName.get()
class FolderLine():
# Defines a GUI line with folder input only
def __init__(self, app, name, lineNum):
# initiate all of the line's widgets, and add them to the grid in line "lineNum"
self.name_label = Label(app, text=name)
self.name_label.grid(row=lineNum, column=0)
self.dir = Entry(app)
self.dir.insert(0,os.getcwd())
self.dir.configure(state='readonly')
self.dir.grid(row=lineNum, column=1)
self.dir_button = Button(app, text='...', command=self.Browse)
self.dir_button.grid(row=lineNum, column=2)
def grid_remove(self):
# remove all of the line's widgets from the grid
self.name_label.grid_forget()
self.dir.grid_forget()
self.dir_button.grid_forget()
def Browse(self):
# used for the '...' button
dir = filedialog.askdirectory()
if dir == '':
return
self.dir.configure(state='normal')
self.dir.delete(0, 'end')
self.dir.insert(0, dir)
self.dir.configure(state='readonly')
def getEntry(self, _type):
if _type == 'dir':
return self.dir.get()
class Application(Frame):
# main window class
def __init__(self, master=None):
# initiate main window, and add first line
super().__init__(master)
self.grid()
self.Line = []
self.actionButton = []
self.fileNum()
def fileNum(self):
# adds the first line, defaulted to 2 files
lineNum = len(self.Line)
default = 2
maxFiles = 15
Label(self, text='Number of files:').grid(row=lineNum, column=0)
self.numFiles = StringVar(self)
self.Line.append(
OptionMenu(self, self.numFiles, str(default), *(tuple(range(1, maxFiles + 1))+('JPG Folder',)), command=self.addFileLines))
self.Line[-1].grid(row=lineNum, column=1)
self.addFileLines(default) # add the rest of the lines
def addFileLines(self, selection):
# add/remove file input lines to match *num* input lines, followed by output line and Merge button
length = len(self.Line)
num = 1 if selection == 'JPG Folder' else selection
if length == 1: # only file amount line exists
fileLines = 0
if length > 1: # there are atleast 4 lines: file amount, file output, merge button, atleast 1 input line
fileLines = length - 3
if fileLines == num and (type(self.Line[1]) == (FolderLine if selection == 'JPG Folder' else pdfLine)):
return
else:
# delete output/button lines and spare input lines if needed
self.Line[-2].grid_remove()
del self.Line[-2]
self.Line[-1].grid_remove()
del self.Line[-1]
if fileLines > num:
for i in range(num + 1, fileLines + 1):
self.Line[i].grid_remove()
del self.Line[num + 1:]
if selection == 'JPG Folder': # add Folder request, instead of pdf lines
self.Line[-1].grid_remove()
del self.Line[-1]
self.create_input('JPG Folder:', Type='JPG')
self.create_input('Destination:', Type='out')
self.create_action("Merge", self.mergeJpg)
else: # standard pdf merge
if type(self.Line[-1]) == FolderLine:
self.Line[-1].grid_remove()
del self.Line[-1]
fileLines -= 1
while fileLines < num:
fileLines = fileLines + 1
self.create_input('File ' + str(fileLines) + ':', Type='pdf')
self.create_input('Destination:', Type='out')
self.create_action("Merge", self.mergePages)
def create_input(self, name, Type='Standard'):
# wrapper function for creating different user-input lines
lineNum = len(self.Line)
if Type == 'Standard':
Label(self, text=name).grid(row=lineNum, column=0)
self.Line.append(Entry(self))
self.Line[-1].grid(row=lineNum, column=1)
if Type == 'pdf':
self.Line.append(pdfLine(self, name, lineNum))
if Type == 'out':
self.Line.append(outLine(self, name, lineNum))
if Type == 'JPG':
self.Line.append(FolderLine(self, name, lineNum))
def create_action(self, name, func):
# wrapper function for creating different action lines, only adds Merge button at the moment
self.Line.append(Button(self, text=name, command=func))
self.Line[-1].grid(column=5, columnspan=2)
def mergePages(self):
# get all the arguments given, and merge the relevant pages
pdfWriter = PyPDF2.PdfFileWriter() # pdf object for the output pdf
pdffiles = []
if self.Line[-2].getEntry('out') == "" or self.Line[-2].getEntry('dir') == "":
messagebox.showerror("Error","Missing output parameters")
return
outname = os.path.join(self.Line[-2].getEntry('dir'), self.Line[-2].getEntry('out')) + '.pdf'
try:
for line in self.Line:
if type(line) is not pdfLine:
continue
cDir = line.getEntry('dir')
if cDir == "":
messagebox.showerror("PDF Merger","Missing file #"+str(line.lineNum))
raise IOError
if line.getEntry('start') == "" or line.getEntry('end') == "":
messagebox.showerror("PDF Merger","File #"+str(line.lineNum)+" is empty")
raise IOError
pdffiles.append(open(cDir, 'rb'))
pdfreader = PyPDF2.PdfFileReader(pdffiles[-1]) # pdf object for the file in the current line
start = int(line.getEntry('start'))
end = int(line.getEntry('end'))
pages = range(start, end + 1) if start < end else range(start, end - 1,-1) # determine the page range, and direction (normal/backwards)
for pagenum in pages:
pdfWriter.addPage(pdfreader.getPage(pagenum - 1)) # pages begin at 0
except FileNotFoundError as e: # failed to open one of the files
for file in pdffiles:
file.close()
messagebox.showerror("Error", "Failed to open file: "+e.filename)
return
except IOError:
for file in pdffiles:
file.close()
return
if os.path.exists(outname): # handle file overwriting
choice = messagebox.askyesnocancel("PDF Merger","Do you want to overwrite "+outname+" ?\n(Choosing no will create the file with a different name)")
if choice is None:
for file in pdffiles:
file.close()
return
elif not choice:
i = 1
while os.path.exists(os.path.join(self.Line[-2].getEntry('dir'), self.Line[-2].getEntry('out')) + '_'+ str(i) +'.pdf'):
i+=1
outname = os.path.join(self.Line[-2].getEntry('dir'), self.Line[-2].getEntry('out')) + '_'+ str(i) +'.pdf'
pdfout = open(outname + '.temp', 'wb')
pdfWriter.write(pdfout) # write the new pdf to pdfout
pdfout.close()
for file in pdffiles:
file.close()
try:
os.replace(outname + '.temp', outname)
# TODO: update lines if the file was overwritten, below code doesn't work for some reason
#for line in self.line:
# if type(line) is pdfline:
# if line.getEntry('dir') == outname:
# line.pdfPageOptions()
except:
os.remove(outname+'.temp')
messagebox.showerror('Error','Error overwriting file:\n'+outname+'\nMake sure the file isn\'t open elsewhere')
return
subprocess.Popen(outname, shell=True) # open output pdf for preview by the user
def mergeJpg(self):
folderName = self.Line[1].getEntry('dir')
outname = os.path.join(self.Line[-2].getEntry('dir'), self.Line[-2].getEntry('out')) + '.pdf'
if os.path.exists(outname): # handle file overwriting
choice = messagebox.askyesnocancel("PDF Merger","Do you want to overwrite "+outname+" ?\n(Choosing no will create the file with a different name)")
if choice is None:
return
elif not choice:
i = 1
while os.path.exists(os.path.join(self.Line[-2].getEntry('dir'), self.Line[-2].getEntry('out')) + '_'+ str(i) +'.pdf'):
i+=1
outname = os.path.join(self.Line[-2].getEntry('dir'), self.Line[-2].getEntry('out')) + '_'+ str(i) +'.pdf'
with open(outname + '.temp', "wb") as f:
f.write(img2pdf.convert([folderName+'\\'+i for i in os.listdir(folderName) if i.endswith((".jpg",".JPG",".png",".PNG",".JPEG",".jpeg"))]))
f.close()
try:
os.replace(outname + '.temp', outname)
except:
os.remove(outname+'.temp')
messagebox.showerror('Error','Error overwriting file:\n'+outname+'\nMake sure the file isn\'t open elsewhere')
return
subprocess.Popen(outname, shell=True) # open output pdf for preview by the user
# main tkinter loop
root = ThemedTk()
root.title("PDF Merger")
root.set_theme('arc')
app = Application(master=root)
app.mainloop()