Skip to content

Commit 8b4c1ee

Browse files
authored
Merge pull request #354 from dishant26/Delete-pages-PDF
Added Delete pages pdf
2 parents 1c066ef + eeb9d52 commit 8b4c1ee

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,5 @@ Once you are done working on your script edit this `README.md` file and add the
9797
70| Plagiarism Checker| Find similarity/plagiarism score between 2 texts | [Find me Here](https://github.com/GDSC-RCCIIT/General-Purpose-Scripts/tree/main/scripts/Plagiarism-Checker)
9898
71| Water Reminder | Reminds you to drink water every 2 hours | [Find me Here](https://github.com/GDSC-RCCIIT/General-Purpose-Scripts/tree/main/scripts/Water%20Reminder)
9999
72| QR and Barcode Scanner | Scans both QR Codes and Barcodes | [Find me Here](https://github.com/GDSC-RCCIIT/General-Purpose-Scripts/tree/main/scripts/QR%20and%20Barcode%20Scanner) |
100-
### Good Luck and don't forget to have fun with Open Source 🚀
100+
73| PDF-Delete-Pages | Deletes pages from PDF as entered by the user | [Find me Here](https://github.com/GDSC-RCCIIT/General-Purpose-Scripts/tree/main/scripts/PDF-Delete-Pages) |
101+
### Good Luck and don't forget to have fun with Open Source 🚀
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from PyPDF2 import PdfFileWriter, PdfFileReader
2+
import os
3+
4+
user_path = input("\nEnter the relative/full path of the pdf: ").strip()
5+
path = os.path.normpath(user_path)
6+
pages_del = input("Enter the pages to be deleted separated by a comma: ")
7+
8+
pages_to_delete = pages_del.strip().split(",")
9+
pages_to_delete = [(int(i) - 1) for i in pages_to_delete]
10+
11+
with open(path, "rb") as pdf_file:
12+
pdf_reader = PdfFileReader(pdf_file)
13+
num_pages = pdf_reader.numPages
14+
15+
out_of_index_page = []
16+
for num in pages_to_delete:
17+
if num > num_pages:
18+
out_of_index_page.append(num)
19+
20+
if len(out_of_index_page) == 0:
21+
22+
infile = PdfFileReader(path, "rb")
23+
output = PdfFileWriter()
24+
25+
for i in range(infile.getNumPages()):
26+
if i not in pages_to_delete:
27+
p = infile.getPage(i)
28+
output.addPage(p)
29+
30+
inputfile_name = ((path.split("\\")[-1]).split(".pdf"))[0]
31+
32+
output_name = inputfile_name + "_deleted.pdf"
33+
34+
with open(output_name, "wb") as f:
35+
output.write(f)
36+
37+
print(f"\nThe output pdf is saved as: {output_name}\n")
38+
39+
else:
40+
print("\nPage number entered is greater than the No of Pages in PDF")
41+
print("Please Check & Re-Try\n")

scripts/PDF-Delete-Pages/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Delete Pages from PDF entered by the User
2+
3+
[![](https://img.shields.io/badge/Made_with-Python-red?style=for-the-badge&logo=python)](https://www.python.org/)
4+
[![](https://img.shields.io/badge/Made_with-PyPDF2-red?style=for-the-badge&logo=python)](https://github.com/mstamy2/PyPDF2)
5+
6+
### About
7+
A Python Script to Delete pages from a PDF as per page numbers entered by the user.
8+
9+
### Sample Output
10+
![image](https://user-images.githubusercontent.com/64226014/138313359-eae730b6-707e-4f26-a434-5cede171204a.png)
11+
12+
### Setup
13+
14+
* Install Python3 from [here](https://www.python.org/)
15+
* Open Windows Command Prompt.
16+
* Clone the repository
17+
```bash
18+
git clone https://github.com/GDSC-RCCIIT/General-Purpose-Scripts.git
19+
```
20+
* Navigate inside the ```scripts/PDF-Delete-Pages``` directory.
21+
* Run this command
22+
```bash
23+
pip install -r requirements.txt
24+
```
25+
* Now we are good to go.
26+
27+
Run using Python:
28+
```bash
29+
python Del_pages_pdf
30+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PyPDF2
195 KB
Binary file not shown.
192 KB
Binary file not shown.

0 commit comments

Comments
 (0)