4
4
import os
5
5
from PyPDF2 import PdfFileReader , PdfFileWriter
6
6
7
+ ## Helper Functions
7
8
9
+ ### Get PDF details in the window
8
10
def extract_information (pdf_path ):
9
11
new_window = tk .Toplevel (root )
10
12
new_window .title (pdf_path )
@@ -27,57 +29,80 @@ def extract_information(pdf_path):
27
29
new_label = tk .Label (new_window , text = txt )
28
30
new_label .place (x = 10 , y = 10 )
29
31
30
-
32
+ # Read a pdf then create a new pdf with each page rotated
31
33
def rotate_pages (pdf_path ):
34
+
35
+ # Output file object
32
36
pdf_writer = PdfFileWriter ()
33
37
pdf_reader = PdfFileReader (pdf_path )
38
+
39
+ # Copy each page to the output file object
34
40
for i in range (pdf_reader .getNumPages ()):
35
41
pdf_writer .addPage (pdf_reader .getPage (i ).rotateClockwise (90 ))
36
42
43
+ # Export the object in a pdf file
37
44
with open ("./rotate_pages.pdf" , "wb" ) as fh :
38
45
pdf_writer .write (fh )
39
46
40
-
47
+ # Read two pdfs and merge them into one
41
48
def merge_pdfs (pdfA , pdfB ):
49
+
50
+ # Reading file objects of both pdfs
42
51
pdfA_obj = PdfFileReader (pdfA , strict = False )
43
52
pdfB_obj = PdfFileReader (pdfB , strict = False )
53
+
54
+ # Object to store merged pdf
44
55
pdf_merge = PdfFileWriter ()
45
56
57
+ # Get all the pages from pdfA to writer object
46
58
for pgNum in range (pdfA_obj .numPages ):
47
59
pg = pdfA_obj .getPage (pgNum )
48
60
pdf_merge .addPage (pg )
49
61
62
+ # Get all the pages from pdfB to writer object
50
63
for pgNum in range (pdfB_obj .numPages ):
51
64
pg = pdfB_obj .getPage (pgNum )
52
65
pdf_merge .addPage (pg )
53
66
54
67
# Write out the merged PDF
55
-
56
68
fileObj = open (".\merged_pdf.pdf" , "wb" )
57
69
pdf_merge .write (fileObj )
70
+
71
+ #Display confirmation message
58
72
info_label .configure (text = f"File Saved at : { os .getcwd ()} \merged_pdf.pdf" )
59
73
60
74
75
+ # Split one pdf in two different pdfs
61
76
def pdf_split (path , ranges ):
77
+
78
+ # Read original pdf
62
79
pdf = PdfFileReader (path , strict = False )
80
+
81
+ # Writer obeject for pdf
63
82
pdf_writer = PdfFileWriter ()
64
83
84
+ # Get all the pages upto the provided page number
65
85
for page in range (int (ranges )):
66
86
pdf_writer .addPage (pdf .getPage (page ))
87
+
88
+ # Export the object to pdf file for the first part of the splitted pdf
67
89
with open ("./SplitA.pdf" , "wb" ) as output_pdf :
68
90
pdf_writer .write (output_pdf )
69
91
92
+ # Same steps for the second part of the pdf.
70
93
pdf_writer = PdfFileWriter ()
71
94
for page in range (int (ranges ), pdf .getNumPages ()):
72
95
pdf_writer .addPage (pdf .getPage (page ))
73
96
with open ("./SplitB.pdf" , "wb" ) as output_pdf :
74
97
pdf_writer .write (output_pdf )
75
98
76
-
99
+ # Overlay water mark on a pdf (watermark should also be a pdf)
77
100
def create_watermark (input_pdf , watermark ):
101
+
102
+ # Get watermark page after reading the pdf
78
103
watermark_obj = PdfFileReader (watermark , strict = False )
79
104
watermark_page = watermark_obj .getPage (0 )
80
-
105
+
81
106
pdf_reader = PdfFileReader (input_pdf , strict = False )
82
107
pdf_writer = PdfFileWriter ()
83
108
@@ -86,46 +111,59 @@ def create_watermark(input_pdf, watermark):
86
111
page = pdf_reader .getPage (page )
87
112
page .mergePage (watermark_page )
88
113
pdf_writer .addPage (page )
89
-
114
+
115
+ # Export the writer object to a pdf
90
116
with open ("./Watermarked.pdf" , "wb" ) as out :
91
117
pdf_writer .write (out )
92
118
info_label .configure (text = f"Watermark set\n Check : { os .getcwd ()} " )
93
119
94
-
120
+ # Make a pdf password protected
95
121
def add_encryption (input_pdf , password ):
122
+
123
+ # Reader and writer objects
96
124
pdf_writer = PdfFileWriter ()
97
125
pdf_reader = PdfFileReader (input_pdf , strict = False )
98
126
127
+ # Copy all the pages from the pdf to the writer object
99
128
for page in range (pdf_reader .getNumPages ()):
100
129
pdf_writer .addPage (pdf_reader .getPage (page ))
101
-
130
+
131
+ # add encryption to a writer object
102
132
pdf_writer .encrypt (user_pwd = password , owner_pwd = None , use_128bit = True )
133
+
134
+ # export the encrypted pdf
103
135
f = open ("./Encrypted.pdf" , "wb" )
104
136
pdf_writer .write (f )
105
137
info_label .configure (text = f"Password Set\n Check : { os .getcwd ()} " )
106
138
107
139
140
+ ## Selection Functions
141
+
142
+ # Select file and call extract_information()
108
143
def info ():
109
144
path = askopenfilename (title = "Select File" , filetypes = [("PDF Document" , "*pdf" )])
110
- extract_information (path )
145
+ if path :
146
+ extract_information (path )
111
147
112
148
149
+ # Select file and call rotate_pages()
113
150
def rotate ():
114
151
path = askopenfilename (title = "Select File" , filetypes = [("PDF Document" , "*pdf" )])
115
- rotate_pages ( path )
116
-
152
+ if path :
153
+ rotate_pages ( path )
117
154
155
+ # Select two files and call merge_pdfs()
118
156
def merge ():
119
157
path_A = askopenfilename (
120
158
title = "Select First File" , filetypes = [("PDF Document" , "*pdf" )]
121
159
)
122
160
path_B = askopenfilename (
123
161
title = "Select Second File" , filetypes = [("PDF Document" , "*pdf" )]
124
162
)
163
+ if all (path_A , path_B ):
164
+ merge_pdfs (path_A , path_B )
125
165
126
- merge_pdfs (path_A , path_B )
127
-
128
-
166
+ #Select file and get range and then call pdf_split
129
167
def split ():
130
168
path = askopenfilename (title = "Select File" , filetypes = [("PDF Document" , "*pdf" )])
131
169
@@ -135,6 +173,8 @@ def assign():
135
173
newWindow .destroy ()
136
174
137
175
if path :
176
+ # Small windo basic tkinter setup
177
+ # this window is responsible of creating a popup and ask user the required informationn
138
178
newWindow = tk .Toplevel (root )
139
179
newWindow .title ("Enter Split Ranges" )
140
180
newWindow .geometry ("400x200" )
@@ -154,6 +194,7 @@ def assign():
154
194
container .grid (row = 2 , column = 0 )
155
195
156
196
197
+ # Select two files and call creat_watermark()
157
198
def watermark ():
158
199
path = askopenfilename (title = "Select File" , filetypes = [("PDF Document" , "*pdf" )])
159
200
pathW = askopenfilename (
@@ -172,6 +213,9 @@ def assign():
172
213
newWindow .destroy ()
173
214
174
215
if path :
216
+
217
+ # Small windo basic tkinter setup
218
+ # this window is responsible of creating a popup and ask user the required informationn
175
219
newWindow = tk .Toplevel (root )
176
220
newWindow .title ("Create Password" )
177
221
newWindow .geometry ("400x200" )
@@ -191,32 +235,36 @@ def assign():
191
235
container .grid (row = 1 , column = 0 )
192
236
193
237
238
+ # Basic root tkinter setup, geometry, title
194
239
root = tk .Tk ()
195
240
root .geometry ("800x600" )
196
241
root .wm_title ("PDF Utility Tool" )
197
242
root .resizable (False , False )
198
243
fontStyle = tkFont .Font (size = 42 )
199
244
btnText = tkFont .Font (size = 12 )
200
245
201
-
246
+ # Creating two rows and one column in the window
202
247
root .columnconfigure (0 , minsize = 800 )
203
248
root .rowconfigure (0 , minsize = 200 )
204
249
root .rowconfigure (2 , minsize = 400 )
205
250
251
+ # Title label
206
252
label1 = tk .Label (
207
253
text = "PDF Utility Tool" , font = fontStyle , borderwidth = 2 , relief = "groove"
208
254
)
209
- info_label = tk .Label (text = "" , font = btnText )
210
-
211
255
256
+ # Information label shows the confirmation messages
257
+ info_label = tk .Label (text = "" , font = btnText )
212
258
label1 .grid (row = 0 , column = 0 )
213
259
info_label .place (x = 200 , y = 150 )
214
260
261
+ # Container to store all the function buttons
215
262
container = tk .Frame (root )
216
263
217
264
container .columnconfigure ([0 , 1 ], minsize = 300 )
218
265
container .rowconfigure ([0 , 1 , 2 ], minsize = 100 )
219
266
267
+ # Button for evry function and custom command attribute to call the functions
220
268
btn_merge = tk .Button (
221
269
container ,
222
270
text = "Merge" ,
@@ -279,6 +327,7 @@ def assign():
279
327
)
280
328
281
329
330
+ # Placing all the buttons in the grid setup
282
331
btn_merge .grid (row = 0 , column = 0 )
283
332
btn_rotate .grid (row = 0 , column = 1 )
284
333
btn_split .grid (row = 1 , column = 0 )
0 commit comments