Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9fb8c95

Browse files
Merge pull request avinashkranjan#874 from iamakkkhil/iamakkkhil-patch-1
avinashkranjan#868 PDF reoder added
2 parents 521e1c4 + 900ce45 commit 9fb8c95

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

‎PDF-Tools/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Add watermark
66
- Rotate clockwise and counterclocwise
77
- Encrypt PDFs
8+
- Reorder PDF pages
89

910
## Why is this tool required?
1011

‎PDF-Tools/main.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,74 @@ def rotate_pages():
9797
pdf_file_writer.write(fh)
9898

9999

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("\nNow 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("\nPDF 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+
100168
def menu():
101169
'''Menu for the various functionalities offered'''
102170

@@ -110,7 +178,7 @@ def menu():
110178
"\n Welcome to PDF-Tools \n Store the PDF's in the folder of the script \n Choose from the given options\n"
111179
)
112180
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"
114182
)
115183
# Call the necessary function according to the choice provided by the user
116184
z = int(input())
@@ -124,6 +192,8 @@ def menu():
124192
add_watermark()
125193
elif (z == 5):
126194
add_encryption()
195+
elif (z == 6):
196+
reorder_pages()
127197
else:
128198
print("Please select valid choice\n")
129199
menu()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /