-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPDF_TOOL.py
278 lines (223 loc) · 10.2 KB
/
PDF_TOOL.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
from pypdf import *
from tkinter import *
from tkinter import ttk, messagebox
from tkinter.filedialog import askopenfilename
# to get the path and data of the file
def file_open():
global fileContents, filename
filePath = askopenfilename(
initialdir='C:/', title='Select a File', filetype=(("pdf File", ".pdf"), ("All Files", "*.*")))
with open(filePath, 'r+') as askedFile:
fileContents = askedFile.name
print(fileContents)
filename="" # to get the name of the file from the path
for i in fileContents[::-1]:
if i=="/":
break
filename+=i
filename=filename[::-1] # to get the name of the file from reversed
file_name.set(filename)
# to get the path and data of the file
def marge_file_open():
filePath = askopenfilename(
initialdir='C:/', title='Select a File', filetype=(("pdf File", ".pdf"), ("All Files", "*.*")))
with open(filePath, 'r+') as askedFile:
fileContents = askedFile.name
print(fileContents)
filename="" # to get the name of the file from the path
for i in fileContents[::-1]:
if i=="/":
break
filename+=i
filename=filename[::-1] # to get the name of the file from reversed
file_name_show.insert(0,filename+" , ")
pathlist.append(fileContents)
passw.set(str(len(pathlist)))
# function for reduce the size of the pdf
def reduce_size():
if fileContents==None:
messagebox.showerror("Error","Choose a file first")
return
if a.get()=="":
messagebox.showerror("Error","Enter the name of the file")
return
execute_name = a.get()
if execute_name.endswith(".pdf") == False: # to Sure the file name is a pdf
execute_name = execute_name+".pdf"
# print(execute_name)
pdf_reader = PdfReader(fileContents)
pdf_writer = PdfWriter()
for i in pdf_reader.pages:
pdf_writer.add_page(i)
pdf_writer.add_metadata(pdf_reader.metadata)
with open(execute_name, "wb") as write:
pdf_writer.write(write)
messagebox.showinfo('Success', "Successfully reduced size of the pdf you gave \nIt saved into your current directory or current path.")
pdf_writer.close()
# function for merging the pdfs
def merge_pdf():
if a.get()=="":
messagebox.showerror("Error","Enter the name of the file")
return
if len(pathlist) == 0:
messagebox.showerror("Error","Choose the pdfs correctly")
return
pdf_writer = PdfWriter()
for i in range(len(pathlist)): # to merge the pdfs
execute_name=a.get()
if execute_name.endswith(".pdf") == False: # to Sure the file name is a pdf
execute_name = execute_name+".pdf"
string = pathlist[i]
with open(string, "rb") as input_1: # to read the pdfs
pdf_writer.append(input_1)
with open(execute_name, "wb") as hello:
pdf_writer.write(hello)
pdf_writer.close()
messagebox.showinfo('Success', "Successfully merged the pdfs you gave \nIt saved into your current directory or current path.")
# function for encrypting the pdf
def encrypt_pdf():
if fileContents==None:
messagebox.showerror("Error","Choose a file first")
return
if a.get()=="":
messagebox.showerror("Error","Enter the name of the file")
return
if passw.get()=="":
messagebox.showerror("Error","Enter the password")
return
pdf_reader = PdfReader(fileContents)
pdf_writer = PdfWriter()
execute_name=a.get()
if not execute_name.endswith(".pdf"):
execute_name += ".pdf"
for fuck in pdf_reader.pages:
pdf_writer.add_page(fuck)
pdf_writer.encrypt(passw.get())
with open(execute_name, "wb") as helloworld:
pdf_writer.write(helloworld)
messagebox.showinfo('Success', "So the pdf is now successfully encrypted \nIt saved into your current directory or current path.")
pdf_writer.close()
# function for decrypting the pdf
def decrypt_pdf():
if fileContents==None:
messagebox.showerror("Error","Choose a file first")
return
if a.get()=="":
messagebox.showerror("Error","Enter the name of the file")
return
if passw.get()=="":
messagebox.showerror("Error","Enter the password")
return
pdf_reader = PdfReader(fileContents)
execute_name=a.get()
if not execute_name.endswith(".pdf"):
execute_name += ".pdf"
pdf_reader.decrypt(passw.get())
pdf_writer = PdfWriter()
for fuck in pdf_reader.pages:
pdf_writer.add_page(fuck)
with open(execute_name, "wb") as helloworld:
pdf_writer.write(helloworld)
messagebox.showinfo('Success', "So the pdf is now successfully decrypted \nIt saved into your current directory or current path.")
pdf_writer.close()
# function for Extract Image from the pdf
def Extract_img():
reader = PdfReader(fileContents)
page = reader.pages[0]
count = 0
for image_file_object in page.images:
with open(str(count) + image_file_object.name, "wb") as fp:
fp.write(image_file_object.data)
count += 1
messagebox.showinfo('Success', "The Images of the pdf is now successfully Extracted.\nIt saved into your current directory or current path.")
# funcion for choosing option and creating responding window
def choose():
global a,win,file_name,passw,pathlist,file_name_show
win=Tk()
win.geometry("440x250")
win.configure(bg="dark green")
fr=Frame(win,width=360,height=180,bg="aquamarine",bd=10,relief="ridge")
fr.place(anchor="center",relx=0.5,rely=0.5)
#win.resizable(0,0)
a=StringVar(win)
passw=StringVar(win)
file_name=StringVar(win)
pathlist=[]
if (drop.get()) =="Reduce size":
window.destroy()
win.title("Reduce size")
# lb1=Label(fr,text="Choose the file").pack()
file_name_show=Entry(fr,textvariable=file_name,width=40,bd=5,relief="sunken",state="disable")
file_name_show.pack()
file_name.set("Choose the file")
Button(fr,text="Open",command=file_open,bg="aqua",bd=5,relief="ridge").pack()
Label(fr,text="Enter the name of the executed pdf file",bg="aquamarine",bd=5,relief="sunken").pack()
Entry(fr,textvariable=a,bd=5,relief="sunken").pack()
Button(fr,text="Submit",command=reduce_size,bg="aqua",bd=5,relief="ridge").pack()
elif (drop.get()) =="Merge pdf":
window.destroy()
win.title("Merge pdf")
Entry(fr,textvariable=passw,bd=5,relief="sunken").pack()
Label(fr,text="Choose the pdfs one by one",bg="aquamarine",bd=5,relief="sunken").pack()
file_name_show=Entry(fr,width=65)
file_name_show.pack(padx=5,pady=5)
Button(fr,text="Open",command=marge_file_open,bg="aqua",bd=5,relief="ridge").pack()
Label(fr,text="Enter the name of the executed pdf file",bg="aquamarine",bd=5,relief="sunken").pack()
Entry(fr,textvariable=a,bd=5,relief="sunken").pack()
Button(fr,text="submit",command=merge_pdf,bg="aqua",bd=5,relief="ridge").pack()
elif (drop.get()) =="Encrypt pdf":
window.destroy()
win.title("Encrypt pdf")
# lb1=Label(fr,text="Choose the file").pack()
file_name_show=Entry(fr,textvariable=file_name,width=40,bd=5,relief="sunken",state="disable")
file_name_show.pack()
file_name.set("Choose the file")
Button(fr,text="Open",command=file_open,bg="aqua",bd=5,relief="ridge").pack()
Label(fr,text="Enter the name of the executed pdf file",bg="aquamarine",bd=5,relief="sunken").pack(padx=5,pady=5)
Entry(fr,textvariable=a,bd=5,relief="sunken").pack()
Label(fr,text="Enter the password",bg="aquamarine",bd=5,relief="sunken").pack()
Entry(fr,textvariable=passw,bd=5,relief="sunken").pack()
Button(fr,text="submit",command=encrypt_pdf,bg="aqua",bd=5,relief="ridge").pack()
elif (drop.get()) =="Decrypt pdf":
window.destroy()
win.title("Decrypt pdf")
# lb1=Label(fr,text="Choose the file").pack()
file_name_show=Entry(fr,width=40,textvariable=file_name,bd=5,relief="sunken",state="disable")
file_name_show.pack()
file_name.set("Choose the file")
Button(fr,text="Open",command=file_open,bg="aqua",bd=5,relief="ridge").pack()
Label(fr,text="Enter the name of the executed pdf file",bg="aquamarine",bd=5,relief="sunken").pack(padx=5,pady=5)
Entry(fr,textvariable=a,bd=5,relief="sunken").pack()
Label(fr,text="Enter the password",bg="aquamarine",bd=5,relief="sunken").pack()
Entry(fr,textvariable=passw,bd=5,relief="sunken").pack()
Button(fr,text="submit",command=decrypt_pdf,bg="aqua",bd=5,relief="ridge").pack()
elif (drop.get()) =="Extract Images":
window.destroy()
win.title("Extract Images")
file_name_show=Entry(fr,textvariable=file_name,width=40,bd=5,relief="sunken",state="disable")
file_name_show.pack()
file_name.set("Choose the file")
Button(fr,text="Open",command=file_open,bg="aqua",bd=5,relief="ridge").pack()
Button(fr,text="Submit",command=Extract_img,bg="aqua",bd=5,relief="ridge").pack(pady=5)
else:
win.destroy()
messagebox.showerror("Error","Choose correct option",master=window)
win.mainloop()
# main() function----------------------------------------------------
window=Tk()
window.title("All in one pdf tool")
window.geometry("440x200")
window.configure(bg="dark green")
window.iconbitmap("th.ico")
window.resizable(False,False)
# creating a frame for better UI
frame=Frame(window,width=360,height=180,bg="aquamarine",bd=10,relief="ridge")
frame.place(anchor="center",relx=0.5,rely=0.5)
Label(frame,text="Welcome to The Application",font=("arial",15,"bold"),bg="aquamarine",bd=5,relief="sunken").pack(padx=5,pady=5)
Label(frame,bg="aquamarine",text="Here You are able to ENCRYPT or DECRYPT PDFs\nAlso you can MARGE or RESIZE PDFs",font=("arial",12,"bold"),bd=5,relief="sunken").pack(padx=5,pady=5)
choices=["Reduce size","Merge pdf","Encrypt pdf","Decrypt pdf","Extract Images"]
drop=ttk.Combobox(frame,values=choices,state="readonly",width=20)
drop.set("Choose an option")
drop.pack()
Button(frame,text="Submit",command=choose,bg="aqua",bd=5,relief="ridge").pack(padx=5,pady=5)
window.mainloop()