@@ -97,6 +97,74 @@ def rotate_pages():
97
97
pdf_file_writer .write (fh )
98
98
99
99
100
+ def ifPageExists (total_pages , page_no ):
101
+ """
102
+ This function checks whether the given page number is in the specified range
103
+ of total pages.
104
+ """
105
+ if page_no <= total_pages :
106
+ return False
107
+ return True
108
+
109
+
110
+ def reorder_pages ():
111
+ input_pdf = input (r"Enter I/P PDF path " )
112
+
113
+ pdf_writer = PdfFileWriter ()
114
+ pdf_reader = PdfFileReader (input_pdf )
115
+
116
+ # get total no.of pages ie length of PDF
117
+ total_pages = pdf_reader .getNumPages ()
118
+ # creates a list of of total pages in ascending order
119
+ ordered_pages = [i + 1 for i in range (total_pages )]
120
+
121
+ # Taking input that how many pages want to reorder
122
+ n = int (input ("Enter the Total Number of pages which you want to reorder:" ))
123
+
124
+ # Taking user INPUT of page no and location you want to move that page
125
+ print ("\n Now enter the Page no which you want to reorder with the expected location" )
126
+
127
+ # Running a loop to take input
128
+ for i in range (n ):
129
+ ans_1 = True
130
+ while ans_1 :
131
+ page_no = int (input ("Enter the Page No. you want to reorder: " ))
132
+ ans_1 = ifPageExists (total_pages , page_no )
133
+ if ans_1 : # if the no. is invalid
134
+ print ("Invalid Page No. " )
135
+ print (f"Enter a number below { total_pages } " )
136
+
137
+ ans_2 = True
138
+ while ans_2 :
139
+ expected_location = int (input ("Enter the location you want to reorder: " ))
140
+ ans_2 = ifPageExists (total_pages , expected_location )
141
+ if ans_2 : # if location is in invalid
142
+ print ("Invalid Page No. " )
143
+ print (f"Enter a number below { total_pages } " )
144
+
145
+ # removing the pages from the initial list so that we can
146
+ # move it to the specified location
147
+ ordered_pages .remove (page_no )
148
+ # inserting the page no at the specified location
149
+ ordered_pages .insert (expected_location - 1 , page_no )
150
+
151
+ print ("Pages are going to be in these order: " , end = "" )
152
+ print (ordered_pages , "\n " )
153
+
154
+ # if ordered pages are ready in a list then passing it further into write function
155
+ print ("\n PDF being prepared !" )
156
+ for page in ordered_pages :
157
+ # adding pages in write function page by page
158
+ pdf_writer .addPage (pdf_reader .getPage (page - 1 ))
159
+
160
+ # Saving the PDF with the specified name
161
+ output_file = input ("Enter the filename in which you want to save (without .pdf extension): " ) + '.pdf'
162
+ with open (output_file , 'wb' ) as fh :
163
+ pdf_writer .write (fh )
164
+
165
+ print (f"Great Success!!! Check your directory for { output_file } file!" )
166
+
167
+
100
168
def menu ():
101
169
'''Menu for the various functionalities offered'''
102
170
@@ -110,7 +178,7 @@ def menu():
110
178
"\n Welcome to PDF-Tools \n Store the PDF's in the folder of the script \n Choose from the given options\n "
111
179
)
112
180
print (
113
- " 1.Merge PDF\n 2.Split PDF\n 3.Rotate PDF\n 4.Add Watermark\n 5.Encrypt PDF\n "
181
+ " 1.Merge PDF\n 2.Split PDF\n 3.Rotate PDF\n 4.Add Watermark\n 5.Encrypt PDF\n 6.Reorder PDF Pages \n "
114
182
)
115
183
# Call the necessary function according to the choice provided by the user
116
184
z = int (input ())
@@ -124,6 +192,8 @@ def menu():
124
192
add_watermark ()
125
193
elif (z == 5 ):
126
194
add_encryption ()
195
+ elif (z == 6 ):
196
+ reorder_pages ()
127
197
else :
128
198
print ("Please select valid choice\n " )
129
199
menu ()
0 commit comments