-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdftools.py
470 lines (421 loc) · 17.4 KB
/
pdftools.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
"""
Tkinter GUI for pdftools
Usage: python pdftools.py
- Combine pdf files
- Reorder, Rotate, and Extract pages of a pdf file
Requires:
1. PyPDF2 version 1.26.0 or newer (pip install PyPDF2)
2. python version 3.6 or newer (includes tkinter)
"""
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
from tkinter import messagebox as mb
from tkinter import scrolledtext as st
import PyPDF2
import os
import pdfcombine as comb
import pdfreorder as reorder
import pdfrotate as rotator
import pdfinfo as pdfinfo
import pdftools_utils as pu
openString = 'Click to open file '
def get_default_dir():
'''
Return default directory for file open operations
First try ~/Downloads
Else try ~/Documents
Else try ~
Else return None
'''
if os.name == 'nt':
# Windows
import ctypes.wintypes
CSIDL_PERSONAL = 5 # My Documents
SHGFP_TYPE_CURRENT = 0 # Get current, not default value
buf= ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
ctypes.windll.shell32.SHGetFolderPathW(None
, CSIDL_PERSONAL
, None,
SHGFP_TYPE_CURRENT, buf)
docs = buf.value
home = os.path.split(docs)[0]
else:
# Linux
home = os.path.expanduser('~')
docs= os.path.join(home, 'Documents')
dlds = os.path.join(home, 'Downloads')
if os.path.isdir(dlds):
default_dir = dlds
elif os.path.isdir(docs):
default_dir = docs
elif os.path.isdir(home):
default_dir = home
else:
default_dir = None
return default_dir
class PdfTools(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
master.title("PDF Tools")
self.init_combiner_gui()
self.init_reorderer_gui()
self.init_rotator_gui()
self.init_info_gui()
self.Co = comb.PdfCombiner()
self.Re = reorder.PdfReorderer()
self.Ro = rotator.PdfRotator()
self.Pi = pdfinfo.PdfInfo()
# Setup notebook, style
customed_style = ttk.Style()
customed_style.configure('PDFtools.TNotebook.Tab'
, padding=[2, 2]
, font=('TkDefaultFont', 10, "bold"))
self.notebook = ttk.Notebook(master, style='PDFtools.TNotebook')
self.tab1 = ttk.Frame(self.notebook)
self.tab2 = ttk.Frame(self.notebook)
self.tab3 = ttk.Frame(self.notebook)
self.tab4 = ttk.Frame(self.notebook)
self.notebook.add(self.tab1, text="Combine")
self.notebook.add(self.tab2, text="Reorder")
self.notebook.add(self.tab3, text="Rotate")
self.notebook.add(self.tab4, text="Info")
self.notebook.grid(row=0, column=0)
# Populate widgets
self.create_widgets_tab1()
self.create_widgets_tab2()
self.create_widgets_tab3()
self.create_widgets_tab4()
def create_widgets_tab1(self):
''''
Place widgets for Combiner applet
'''
self.mainframe = tk.Frame(self.tab1)
self.mainframe.grid(row=3, column=0, ipadx=40)
# File selection buttons
self.FileButton1 = tk.Button(self.tab1
, text=openString + '1'
, activebackground='red'
, command=self.setfile1)
self.FileButton1.config(font=("TkDefaultFont", 10))
self.FileButton1.grid(row=0, column=0, sticky='NSEW')
self.FileButton2 = tk.Button(self.tab1
, text=openString + '2'
, activebackground='red'
, command=self.setfile2)
self.FileButton2.config(font=("TkDefaultFont", 10))
self.FileButton2.grid(row=1, column=0, sticky='NSEW')
# Overwrite checkbox
self.ClobberButton = tk.Checkbutton(self.tab1
, text="Overwrite File 1"
, variable=self.overwrite1)
self.ClobberButton.grid(row=2, column=0)
# Rotation labels and dropdowns
self.rotlabel1 = tk.Label(self.mainframe,text = 'Rotate file 1: ')
self.rotlabel1.config(font=("TkDefaultFont", 10))
self.rotlabel1.grid(row=3, column=0, ipadx=10, sticky="W")
self.rotlabel2 = tk.Label(self.mainframe,text = 'Rotate file 2: ')
self.rotlabel2.config(font=("TkDefaultFont", 10))
self.rotlabel2.grid(row=4, column=0, ipadx=10, sticky="W")
self.v1 = tk.StringVar()
self.v1.set(self.Co.rotOptionList[0])
self.v2 = tk.StringVar()
self.v2.set(self.Co.rotOptionList[0])
self.om1 = tk.OptionMenu(self.mainframe
, self.v1
, *self.Co.rotOptionList
, command=lambda value: self.setrot(value, 0))
self.om1.grid(row=3, column=1, ipadx=10)
self.om2 = tk.OptionMenu(self.mainframe
, self.v2
, *self.Co.rotOptionList
, command=lambda value: self.setrot(value, 1))
self.om2.grid(row=4, column=1, ipadx=10)
# Combine! button
self.CombineButton = tk.Button(self.tab1
, text='Combine!'
, activebackground='red'
, bg='green', fg='white'
, command=self.do_combine)
self.CombineButton.config(font=("TkDefaultFont", 12, "bold"))
self.CombineButton.grid(row=5, column=0, sticky='NESW', columnspan=5)
def create_widgets_tab2(self):
''''
Place widgets for Reorder applet
'''
# File selection button
self.FileButton3 = tk.Button(self.tab2
, text=openString
, activebackground='red'
, command=self.setfile3)
self.FileButton3.config(font=("TkDefaultFont", 12))
self.FileButton3.pack(side='top', fill=tk.X, expand=True)
# Label to dispay number of pages in document
self.npagelabel1 = tk.Label(self.tab2, text = ' ')
self.npagelabel1.config(font=("TkDefaultFont", 10))
self.npagelabel1.pack(side='top', fill=tk.X, expand=True)
# Label the text enty box
self.rotlabel4 = tk.Label(self.tab2
, text = 'Pages to reorder (eg. 1-3,5,9,...):')
self.rotlabel4.config(font=("TkDefaultFont", 12))
self.rotlabel4.pack(fill="both", expand=True)
# Text enty box
self.reorder_pages = ''
self.entry1 = tk.Entry(self.tab2, textvariable=self.reorder_pages)
self.entry1.config(font=("TkDefaultFont", 12))
self.entry1.pack(fill=tk.X, expand=True)
# Reorder! button
self.ReorderButton = tk.Button(self.tab2
, text='Reorder!'
, activebackground='red'
, bg='green', fg='white'
, command=self.do_reorder)
self.ReorderButton.config(font=("TkDefaultFont", 12, "bold"))
self.ReorderButton.pack(side="bottom"
, fill=tk.X
, expand=True)
def create_widgets_tab3(self):
''''
Place widgets for Rotator applet
'''
# File selection button
self.FileButton4 = tk.Button(self.tab3
, text=openString
, activebackground='red'
, command=self.setfile4)
self.FileButton4.config(font=("TkDefaultFont", 12))
self.FileButton4.pack(side='top', fill='both', expand=True)
# Label to dispay number of pages in document
self.npagelabel2 = tk.Label(self.tab3, text = ' ')
self.npagelabel2.config(font=("TkDefaultFont", 10))
self.npagelabel2.pack(fill='both', expand=True)
# Label for the text enty box
self.rotlabel5 = tk.Label(self.tab3
, text = 'Pages to rotate (eg. 1-3,5,9,...):')
self.rotlabel5.config(font=("TkDefaultFont", 12))
self.rotlabel5.pack(fill="both", expand=True)
# Text enty box
self.rotate_pages = ''
self.entry2 = tk.Entry(self.tab3, textvariable=self.rotate_pages)
self.entry2.config(font=("TkDefaultFont", 12))
self.entry2.pack(fill='both', expand=True)
# Rotation dropdown
self.v3 = tk.StringVar()
self.v3.set(self.Ro.rotOptionList[0])
self.om3 = tk.OptionMenu(self.tab3
, self.v3
, *self.Ro.rotOptionList
, command=self.setpagerot)
self.om3.config(font=("TkDefaultFont", 12))
self.om3.pack(side='top', fill=tk.X, expand=True)
# Rotate! button
self.RotateButton = tk.Button(self.tab3
, text='Rotate!'
, activebackground='red'
, bg='green', fg='white'
, command=self.do_rotate)
self.RotateButton.config(font=("TkDefaultFont", 12, "bold"))
self.RotateButton.pack(side="bottom"
, fill=tk.X
, expand=True)
def create_widgets_tab4(self):
''''
Place widgets for Info applet
'''
# File selection button
self.FileButton5 = tk.Button(self.tab4
, text=openString
, activebackground='red'
, command=self.setfile5)
self.FileButton5.config(font=("TkDefaultFont", 12))
self.FileButton5.pack(side='top', fill='both', expand=True)
# Scrolled text box
self.textArea1 = st.ScrolledText(self.tab4
, wrap = tk.WORD
, width = 27
, height = 9
, font = ("TkDefaultFont", 10))
self.textArea1.pack(side='top', fill=tk.X, expand=True)
def init_combiner_gui(self):
self.file1 = None
self.file2 = None
self.ofile = None
self.defdir1 = get_default_dir()
self.defdir2 = get_default_dir()
self.overwrite1 = tk.BooleanVar()
self.rotation = ['NONE', 'NONE'] # rotation flag for file1, file2
def init_reorderer_gui(self):
self.defdir3 = get_default_dir()
def init_rotator_gui(self):
self.defdir4 = get_default_dir()
self.rotate = 'NONE'
def init_info_gui(self):
self.defdir5 = get_default_dir()
self.mru_file = None
self.mru_dir = self.defdir5
def setrot(self, choice, value):
'''
Configure rotation used by PDF combiner
'''
if choice == self.Co.rotOptionList[1]:
self.rotation[value] = 'CW'
elif choice == self.Co.rotOptionList[2]:
self.rotation[value] = 'CCW'
elif choice == self.Co.rotOptionList[3]:
self.rotation[value] = 'FV'
else:
self.rotation[value] = 'NONE'
def setpagerot(self, choice):
'''
Configure rotation used by PDF rotator
'''
if choice == self.Ro.rotOptionList[1]:
self.rotate = 'CW'
elif choice == self.Ro.rotOptionList[2]:
self.rotate = 'CCW'
elif choice == self.Ro.rotOptionList[3]:
self.rotate = 'FV'
else:
self.rotate = 'NONE'
def setfile1(self):
'''
Setup file 1 input for PDF combiner
'''
self.file1= fd.askopenfilename(initialdir=self.defdir1,
filetypes=[("PDF Files", "*.pdf"), ("All Files", "*.*")])
if self.file1:
self.FileButton1["text"] = self.file1
self.FileButton1["bg"] = "yellow"
self.defdir1 = os.path.split(self.file1)[0]
self.updateMostRecentFile(self.file1)
print("file 1 is " + self.file1)
def setfile2(self):
'''
Setup file 2 input for PDF combiner
'''
self.file2= fd.askopenfilename(initialdir=self.defdir2,
filetypes=[("PDF Files", "*.pdf"), ("All Files", "*.*")])
if self.file2:
self.FileButton2["text"] = self.file2
self.FileButton2["bg"] = "yellow"
self.defdir2 = os.path.split(self.file2)[0]
self.updateMostRecentFile(self.file2)
print("file 2 is " + self.file2)
def setfile3(self):
'''
Setup file input for PDF reorderer
'''
self.file3= fd.askopenfilename(initialdir=self.defdir3,
filetypes=[("PDF Files", "*.pdf"), ("All Files", "*.*")])
if self.file3:
self.FileButton3["text"] = self.file3
self.FileButton3["bg"] = "yellow"
self.defdir3 = os.path.split(self.file3)[0]
N = pu.getNumPages(self.file3)
displayPages= 'Document contains {0} pages'.format(N)
self.npagelabel1["text"] = displayPages
self.updateMostRecentFile(self.file3)
print("file 3 is " + self.file3)
def setfile4(self):
'''
Setup file input for PDF rotator
'''
self.file4= fd.askopenfilename(initialdir=self.defdir4,
filetypes=[("PDF Files", "*.pdf"), ("All Files", "*.*")])
if self.file4:
self.FileButton4["text"] = self.file4
self.FileButton4["bg"] = "yellow"
self.defdir4 = os.path.split(self.file4)[0]
N = pu.getNumPages(self.file4)
displayPages= 'Document contains {0} pages'.format(N)
self.npagelabel2["text"] = displayPages
self.updateMostRecentFile(self.file4)
print("file 4 is " + self.file4)
def setfile5(self):
'''
Setup file input for PDF info
'''
self.file5= fd.askopenfilename(initialdir=self.mru_dir,
filetypes=[("PDF Files", "*.pdf"), ("All Files", "*.*")])
if self.file5:
self.updateMostRecentFile(self.file5)
print("file 5 is " + self.file5)
def updateMostRecentFile(self, pathfilename):
'''
Update the most recently selected file
for displaying file info
'''
self.mru_file = pathfilename
self.mru_dir = os.path.split(self.mru_file)[0]
# Update button label on the Info tab
self.FileButton5["text"] = pathfilename
self.FileButton5["bg"] = "yellow"
self.defdir5 = os.path.split(pathfilename)[0]
self.do_info()
def set_reorder_text(self, dummy):
'''
Read pages text box in the PDF reorderer
'''
self.reorder_pages = self.entry1.get()
print("entry1: entered text is " + self.reorder_pages)
def set_rotate_text(self, dummy):
'''
Read pages text box in the PDF rotator
'''
self.rotate_pages = self.entry2.get()
print("entry2: entered text is " + self.rotate_pages)
def do_combine(self):
'''
Setup inputs and call PDF combiner
'''
args = {'inpath1' : self.file1
, 'inpath2' : self.file2
, 'rotate1' : self.rotation[0]
, 'rotate2' : self.rotation[1]
, 'clobber' : self.overwrite1.get()}
if self.Co.validate_inputs(**args) and self.Co.process():
mb.showinfo(title=None, message="Created " + self.Co.get_ofile())
else:
mb.showinfo(title=None, message=self.Co.status())
print(self.Co.status())
def do_reorder(self):
'''
Setup inputs and call PDF reorderer
'''
self.set_reorder_text('')
args = {'inpath' : self.file3,
'pages' : self.reorder_pages}
if self.Re.validate_inputs(**args) and self.Re.process():
mb.showinfo(title=None, message="Created " + self.Re.get_ofile())
else:
mb.showinfo(title=None, message=self.Re.status())
print(self.Re.status())
def do_rotate(self):
'''
Setup inputs and call PDF rotator
'''
self.set_rotate_text('')
args = {'inpath' : self.file4,
'pages' : self.rotate_pages,
'rotation' : self.rotate}
if self.Ro.validate_inputs(**args) and self.Ro.process():
mb.showinfo(title=None, message="Created " + self.Ro.get_ofile())
else:
mb.showinfo(title=None, message=self.Ro.status())
print(self.Ro.status())
def do_info(self):
'''
Setup inputs and call PDF file info
'''
args = {'inpath' : self.mru_file,}
self.textArea1.configure(state ='normal')
self.textArea1.delete('1.0', tk.END)
if self.Pi.validate_inputs(**args) and self.Pi.process():
self.textArea1.insert(tk.INSERT, self.Pi.get_doc_info())
else:
self.textArea1.insert(tk.INSERT, '\n' + self.Pi.status())
print(self.Pi.status())
self.textArea1.configure(state ='disabled')
root = tk.Tk()
P = PdfTools(master=root)
P.mainloop()