-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf-surgeon.py
381 lines (316 loc) · 13.1 KB
/
pdf-surgeon.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
import tkinter as tk,subprocess,os,threading
from PyPDF2 import PdfFileWriter,PdfFileReader
from time import sleep
from pdfoperator import PdfSetOperator,PdfGetOperator
from tkinter import *
from tkinter import ttk,messagebox,font
from tkinter.filedialog import askopenfilenames,askopenfilename
class PdfGui():
def __init__(self,master):
#variables
self.outputDir = os.path.expanduser("~/Desktop");
self.bindUploadFiles = '';
self.extractUploadFile='';
self.extractUploadFileName = StringVar();
self.pageNumList = StringVar();
self.numPages = StringVar();
self.combine = BooleanVar();
self.combine.set(False);
self.getOperator=None;
self.NameDirectoryDict = {};
self.files=0;
self.blankPageCount = 0;
self.background = "gray90"
self.master = master
master.configure(background=self.background)
master.title("PDF-Surgeon® [1.0.0v]")
master.resizable(width=False, height=False);
default_font = font.nametofont("TkDefaultFont")
default_font.configure(size=12,family="Candara")
#mainframe
self.mainframe = ttk.Frame(master,relief=tk.RIDGE,width=500,height=500,style="main.TFrame");
# label for frame-one
self.labelone = ttk.Label(text="Bind Files")
# label for frame-two
self.labeltwo = ttk.Label(text="Extract Pages")
#labelframe - one
self.labelframeone = ttk.LabelFrame(self.mainframe,labelwidget=self.labelone)
#copy right
self.copyright = ttk.Label(self.mainframe,text="copyrightⓒ2018 min.naung",style="i8nc.TLabel")
self.copyright.grid(row=1,column=2,pady=3)
#labelframe for [uploadfiles,up,down,view,remove ]
self.newframe = ttk.Frame(self.labelframeone);
#labelframe for [up,down]
self.updownframe = ttk.Frame(self.newframe);
#labelframe for [view,remove]
self.viewremoveframe = ttk.Frame(self.newframe);
#upload files button
self.uploadfilesbtn = ttk.Button(self.newframe,text="Upload Files",command=self.bindUpload,style="i12n.TButton")
# up,down,remove button
self.upbtn = ttk.Button(self.updownframe,text="↑", command=self.up,style="r9n.TButton")
self.downbtn =ttk.Button(self.updownframe,text="↓", command=self.down, style="r9n.TButton")
self.viewbtn = ttk.Button(self.viewremoveframe,text="⬜",command=self.view, style="r9n.TButton")
self.removebtn = ttk.Button(self.viewremoveframe,text="X",command=self.remove, style="r9nc.TButton")
self.blankbtn = ttk.Button(self.viewremoveframe,text="➕",command=self.addBlank,style="r9n.TButton")
#files info list
self.lbframe = ttk.Frame(self.labelframeone,relief=SUNKEN)
self.uploadfileslist = tk.Listbox(self.lbframe,exportselection=False,height=6,width=35)
self.scrollbar= ttk.Scrollbar(self.lbframe,orient=VERTICAL,command=self.uploadfileslist.yview)
self.uploadfileslist["yscrollcommand"]=self.scrollbar.set;
#bind confirm button
self.bindokbtn = ttk.Button(self.labelframeone,text="Bind PDFs",command=self.bind,style="i12b.TButton");
#events [double-click open]
self.uploadfileslist.bind("<Double-Button-1>",self.doubleClickOpen);
#labelframe - two
self.labelframetwo = ttk.LabelFrame(self.mainframe,labelwidget=self.labeltwo)
#upload file button
self.uploadframe = ttk.Frame(self.labelframetwo)
self.l2uploadbtn = ttk.Button(self.uploadframe,text="Upload File",command=self.extractUpload,style="i12n.TButton")
self.l2uploadlabel = ttk.Label(self.uploadframe,textvariable=self.extractUploadFileName)
self.l2uploadpages = ttk.Label(self.uploadframe,textvariable=self.numPages);
#numbers of page
self.numlbframe = ttk.Frame(self.labelframetwo)
self.pagenumlb = ttk.Label(self.numlbframe,text="Page Number:")
self.pagenumbers = ttk.Entry(self.numlbframe,width=15,textvariable=self.pageNumList,style="i10n.TEntry")
self.pagenumbers.insert(0,"eg: 1,3-5,9,11-17");
self.pagenumbers.bind("<FocusIn>",self.hideText)
self.combinepage = ttk.Checkbutton(self.labelframetwo,text="Combine",variable=self.combine,
onvalue=True,offvalue=False)
#extract confirm button
self.l2extractok = ttk.Button(self.labelframetwo,text="Extract PDFs",command=self.extract,style="i12b.TButton");
#tooltips
tooltip(self.uploadfilesbtn,"Upload PDFs to merge");
tooltip(self.upbtn,"Up");
tooltip(self.downbtn,"Down");
tooltip(self.viewbtn,"Open");
tooltip(self.removebtn,"Remove");
tooltip(self.blankbtn,"add blank page")
tooltip(self.bindokbtn,"Files merge to single PDF")
tooltip(self.l2uploadbtn,"Upload PDF to extract");
tooltip(self.l2extractok,"Extract files from uploaded PDF")
#grids
self.mainframe.grid(row=0,column=0)
#labelframeone -grids
self.labelframeone.grid(row=0,column=0,padx=10,pady=10,sticky=(N))
self.newframe.grid(row=0,column=0,pady=5)
self.updownframe.grid(row=0,column=0,sticky=(W,N,S),pady=5)
self.viewremoveframe.grid(row=0,column=2,sticky=(E,N,S),pady=5)
self.uploadfilesbtn.grid(row=0,column=1,ipadx=8,padx=40)
self.upbtn.grid(row=0,column=0,sticky=(N,E))
self.downbtn.grid(row=0,column=1,sticky=(N,W,E))
self.viewbtn.grid(row=0,column=2,sticky=(N,W,E))
self.removebtn.grid(row=0,column=3,sticky=(N,W,E))
self.blankbtn.grid(row=0,column=4,sticky=(N,E))
self.lbframe.grid(row=1,column=0,padx=10)
self.uploadfileslist.grid(row=0,column=0,sticky=(N,W,E,S))
self.scrollbar.grid(row=0,column=1,sticky=(N,S))
self.bindokbtn.grid(row=3,column=0,pady=6,ipadx=10);
#labelframetwo -grids
self.labelframetwo.grid(row=0,column=2,padx=10,pady=10,sticky=(N))
self.uploadframe.grid(row=0,column=0,padx=35,pady=7)
self.l2uploadbtn.grid(row=0,column=0,ipadx=10)
self.l2uploadlabel.grid(row=1,column=0,sticky=(N))
self.l2uploadpages.grid(row=2,column=0,sticky=(N))
self.numlbframe.grid(row=2,column=0,sticky=(N))
self.pagenumlb.grid(row=0,column=0)
self.pagenumbers.grid(row=1,column=0)
self.combinepage.grid(row=4,column=0,pady=3)
self.l2extractok.grid(row=5,column=0,ipadx=10,pady=8);
# styles
self.style = ttk.Style();
self.style.configure("main.TFrame",background=self.background)
self.style.configure("label.TLabel",background="AntiqueWhite1")
self.style.configure("lframe.TLabelframe.Label",background="AntiqueWhite1")
self.style.configure("r9n.TButton",font=("Candara",9,"normal","bold"),height=4,width=3)
self.style.configure("r9nc.TButton",font=("Candara",9,"normal","bold"),height=4,width=3,foreground="red")
self.style.configure("i8nc.TLabel",font=("Candara",9,"italic","bold"),foreground="gray33",background=self.background)
self.style.configure("i12b.TButton",font = ("Candara",12,"italic","bold"))
self.style.configure("i12n.TButton",font = ("Candara",12,"italic","normal"),relief=RAISED)
self.style.configure("i10n.TEntry",font = ("Candara",10,"italic","normal"))
self.style.configure("i12.TFrame",font=("Candara",12))
# display window
master.mainloop()
def bindUpload(self):
if self.outputDir:
initialDir = self.outputDir;
else:
initialDir = "./"
self.bindUploadFiles = askopenfilenames(initialdir = initialDir,title="Select PDF Files",
filetypes=(("PDF File/Files","*.pdf"),))
if not self.bindUploadFiles:
return '';
self.outputDir = "/".join(self.bindUploadFiles[0].split("/")[:-1]);
for i in self.bindUploadFiles:
self.uploadfileslist.insert("end",i.split("/")[-1]);
self.NameDirectoryDict[i.split("/")[-1]] = i;
def bind(self):
if len(self.NameDirectoryDict.items())<2:
return ;
newList = self.uploadfileslist.get(0,len(self.NameDirectoryDict.items()))
reorderList = [];
[reorderList.append(self.NameDirectoryDict[i]) for i in newList];
# progress bar thread
self.files = 0
self.files = range(len(self.bindUploadFiles));
prg_thread = threading.Thread(name="progressbar",target=self.progress(0.15));
# pdf-binding thread
pdf = PdfSetOperator();
pdf_thread = threading.Thread(name="pdf-binding",target=pdf.bind(*tuple(reorderList),outputDir=self.outputDir));
# start threads
pdf_thread.start()
prg_thread.start()
messagebox.showinfo("PDF Operation Completed","Your output-binder file is ready!")
def extractUpload(self):
if self.outputDir:
initialDir = self.outputDir;
else:
initialDir = "./"
self.extractUploadFile = askopenfilename(initialdir = initialDir,
title="Select PDF File", filetypes=(("PDF File","*.pdf"),))
if not self.extractUploadFile:
return '';
self.outputDir= "/".join(self.extractUploadFile.split("/")[:-1]);
self.getOperator = PdfGetOperator(self.extractUploadFile);
numpages= self.getOperator.numberOfPage()
isPlural = "pages" if numpages > 1 else "page";
self.numPages.set(" (%s%s)" %(numpages,isPlural));
self.extractUploadFileName.set(self.extractUploadFile.split("/")[-1]);
def extract(self):
if not self.extractUploadFile or self.pageNumList.get()=="eg: 1,3-5,9,11-17":
return '';
if not self.pageNumList.get():
messagebox.showerror("Page Number Error!","Please type in your page number 'eg: 1,3-5,9,11-17'.")
return;
# progress bar thread
self.files = 0
self.files = range(len(self.check(self.pageNumList.get())));
prg_thread = threading.Thread(name="progressbar",target=self.progress(0.05));
# pdf-extract thread
self.getOperator = PdfGetOperator(self.extractUploadFile);
pdf_thread = threading.Thread(name="pdf-extracting",target=self.getOperator.extract(self.check(self.pageNumList.get()),self.outputDir,self.combine.get()));
isMultiple = "file is" if len(self.check(self.pageNumList.get()))<2 else "files are"
message = "combined file is" if self.combine.get() else isMultiple;
# threads start
pdf_thread.start()
prg_thread.start()
messagebox.showinfo("PDF Operation Completed","Your %s ready!" %message);
def check(self,strList):
strList = strList.split(",")
numberList = []
for i in strList:
if "-" in i:
fnum = i.split("-")[0];
snum = i.split("-")[1];
numberList.extend(range(int(fnum),int(snum)+1))
else:
numberList.append(int(i));
return numberList;
def hideText(self,event):
self.pagenumbers.delete(0,END)
def showText(self,event):
self.pagenumbers.insert(0,"eg: 1,3-5,9,11-17");
def doubleClickOpen(self,event):
self.index = self.uploadfileslist.curselection();
if self.index:
path = os.path.join(self.outputDir, self.uploadfileslist.get(self.index));
subprocess.Popen(r"%s" %path,shell=True);
def view(self):
self.doubleClickOpen(None);
def remove(self):
index = self.uploadfileslist.curselection();
if not index:
return ;
self.uploadfileslist.delete(index)
def move(self,sign):
index = self.uploadfileslist.curselection();
if not index:
return;
item = self.uploadfileslist.get(index);
self.uploadfileslist.delete(index);
self.uploadfileslist.insert(index[0]+ sign,item);
self.uploadfileslist.select_set(index[0]+ sign);
self.uploadfileslist.event_generate("<<ListboxSelect>>");
def up(self):
self.move(-1);
def down(self):
self.move(+1);
def addBlank(self):
# if not uploaded any files, return default page size
# get last page from upload
if len(self.bindUploadFiles) > 0:
last_page = self.bindUploadFiles[-1]
readLastPage = PdfFileReader(open(last_page,"rb"))
page_size = readLastPage.getPage(0).mediaBox;
width = page_size.upperRight[0]
height = page_size.upperRight[1]
else:
width = 580
height = 750
self.blankPageCount += 1;
name = "blank-page %s.pdf" %self.blankPageCount
file_writer = PdfFileWriter()
file_writer.addBlankPage(height=height,width=width)
file = open(name,"wb")
file_writer.write(file)
file.close()
self.uploadfileslist.insert("active",name)
self.NameDirectoryDict[name] = name;
def progress(self,sleep_time):
popup = tk.Toplevel()
lab= tk.Label(popup,text="Processing files...")
x, y, _cx, cy = self.mainframe.bbox('insert')
x = x + self.mainframe.winfo_rootx()+200
y = y + cy + self.mainframe.winfo_rooty()-300
popup.wm_geometry("+%d+%d" %(x,y))
lab.grid(row=0,column=0)
progress = 0
progress_var = tk.DoubleVar()
progress_bar = ttk.Progressbar(popup, variable=progress_var,length=300)
progress_bar.grid(row=1,column=0)
popup.pack_slaves()
try:
progress_step = float(100.0/len(self.files))
except ZeroDivisionError:
progress_step = 10.0
for file in self.files:
popup.update()
sleep(sleep_time)
progress += progress_step
progress_var.set(progress)
return popup.destroy()
class ToolTip():
def __init__(self, widget):
self.widget = widget
self.tipwindow = None
self.id = None
self.x = self.y = 0
def showtip(self, text):
'Display text in tooltip window'
self.text = text
if self.tipwindow or not self.text:
return
x, y, _cx, cy = self.widget.bbox('insert')
x = x + self.widget.winfo_rootx()
y = y + cy + self.widget.winfo_rooty()+28
self.tipwindow = tw = tk.Toplevel(self.widget)
tw.wm_overrideredirect(1)
tw.wm_geometry("+%d+%d" %(x,y))
label = ttk.Label(tw, text = self.text, justify= tk.LEFT,
relief = tk.GROOVE, borderwidth = 1, font = ('Candara', '11', 'normal'))
label.pack(ipadx=1)
def hidetip(self):
tw = self.tipwindow
self.tipwindow = None
if tw:
tw.destroy()
def tooltip(widget, text):
toolTip = ToolTip(widget)
def enter(event):
toolTip.showtip(text)
def leave(event):
toolTip.hidetip()
widget.bind('<Enter>', enter)
widget.bind('<Leave>', leave)
if __name__ == "__main__":
root = tk.Tk();
PdfGui(root);