-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge_2_pdf.py
84 lines (71 loc) · 3.13 KB
/
merge_2_pdf.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
from os import read
import tkinter as tk
from tkinter import messagebox as msg,filedialog as fd
from typing import final
import PyPDF2
def upload_file():
global reader1,save_as
try:
pdf1 = fd.askopenfile()
#the image file uploaded from filedialog can be extracted using img.name
save_as,ext = pdf1.name.split('.') #we dont need an extension to be saved as filename
if ext.lower() == 'pdf': #valid image extensions supported by cv2
file = open(pdf1.name,'rb')
reader1 = PyPDF2.PdfFileReader(file)
else:
msg.showerror('INVALID',"Select Valid PDF")
except ValueError:
msg.showerror("Invalid","Select Valid PDF")
def upload_file1():
global reader2
try:
pdf2 = fd.askopenfile()
save_as,ext = pdf2.name.split('.')
if ext.lower() == 'pdf':
file = open(pdf2.name,'rb')
reader2 = PyPDF2.PdfFileReader(file)
else:
msg.showerror('INVALID',"Select Valid PDF")
except ValueError:
msg.showerror("Invalid","Select Valid PDF")
def mergeing():
try:
writer = PyPDF2.PdfFileWriter()
for i in range(reader1.numPages):
writer.addPage(reader1.getPage(i))
for j in range(reader2.numPages):
writer.addPage(reader2.getPage(j))
if len(name.get()) == 0:
msg.showerror("Invalid","Enter File Name")
else:
output = name.get()
if output[-4:] == '.pdf':
pass
else:
output = output+'.pdf'
output_file = open("{}{}".format(save_as,output),'wb')
writer.write(output_file)
msg.showinfo("Merge Successful","PDF Saved")
output_file.close()
except NameError:
msg.showerror("Invalid","Select PDF First")
except TypeError:
msg.showerror("File Unknown","Select Valid PDF first")
def main(screen):
screen.geometry("435x435+150+180")
screen.title("Merge Automate")
canva = tk.Canvas(screen,bg="yellow",bd=5)
canva.pack(expand=True, fill= "both")
screen.resizable(False,False) # in order to maintain fixed size of screen
head = tk.Label(screen,text="Merge Two PDF's",font=("arial",15,"bold"),fg="black",bg="white").place(x=125,y=5)
frame = tk.LabelFrame(screen,bg="red").place(x=50,y=90,width=330,height=290)
upload = tk.Button(screen,text="Upload PDF1",font=("arial",14,"bold"),bg="blue",fg="white",bd=5,command=upload_file).place(x=128,y=125,width=190,height=50)
upload1 = tk.Button(screen,text="Upload PDF2",font=("arial",14,"bold"),bg="blue",fg="white",bd=5,command=upload_file1).place(x=128,y=195,width=190,height=50)
lbl = tk.Label(screen,text="Save File As:",font=("arial",14,"bold"),fg="black",bg="white").place(x=65,y=270)
global name
name=tk.Entry(screen,bg="yellow",fg="black",font=('arial',13,"bold"),bd=5)
name.place(x=190,y=265,width=180,height=40)
save = tk.Button(screen,text="Complete Merge",font=("arial",14,"bold"),bg="blue",fg="white",bd=5,command=mergeing).place(x=105,y=320,width=230,height=40)
screen.mainloop()
if __name__ == '__main__':
main()