Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@ Once you are done working on your script edit this `README.md` file and add the
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)
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)
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) |
### Good Luck and don't forget to have fun with Open Source 🚀
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) |
### Good Luck and don't forget to have fun with Open Source 🚀
41 changes: 41 additions & 0 deletions scripts/PDF-Delete-Pages/Del_pages_pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from PyPDF2 import PdfFileWriter, PdfFileReader
import os

user_path = input("\nEnter the relative/full path of the pdf: ").strip()
path = os.path.normpath(user_path)
pages_del = input("Enter the pages to be deleted separated by a comma: ")

pages_to_delete = pages_del.strip().split(",")
pages_to_delete = [(int(i) - 1) for i in pages_to_delete]

with open(path, "rb") as pdf_file:
pdf_reader = PdfFileReader(pdf_file)
num_pages = pdf_reader.numPages

out_of_index_page = []
for num in pages_to_delete:
if num > num_pages:
out_of_index_page.append(num)

if len(out_of_index_page) == 0:

infile = PdfFileReader(path, "rb")
output = PdfFileWriter()

for i in range(infile.getNumPages()):
if i not in pages_to_delete:
p = infile.getPage(i)
output.addPage(p)

inputfile_name = ((path.split("\\")[-1]).split(".pdf"))[0]

output_name = inputfile_name + "_deleted.pdf"

with open(output_name, "wb") as f:
output.write(f)

print(f"\nThe output pdf is saved as: {output_name}\n")

else:
print("\nPage number entered is greater than the No of Pages in PDF")
print("Please Check & Re-Try\n")
30 changes: 30 additions & 0 deletions scripts/PDF-Delete-Pages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Delete Pages from PDF entered by the User

[![](https://img.shields.io/badge/Made_with-Python-red?style=for-the-badge&logo=python)](https://www.python.org/)
[![](https://img.shields.io/badge/Made_with-PyPDF2-red?style=for-the-badge&logo=python)](https://github.com/mstamy2/PyPDF2)

### About
A Python Script to Delete pages from a PDF as per page numbers entered by the user.

### Sample Output
![image](https://user-images.githubusercontent.com/64226014/138313359-eae730b6-707e-4f26-a434-5cede171204a.png)

### Setup

* Install Python3 from [here](https://www.python.org/)
* Open Windows Command Prompt.
* Clone the repository
```bash
git clone https://github.com/GDSC-RCCIIT/General-Purpose-Scripts.git
```
* Navigate inside the ```scripts/PDF-Delete-Pages``` directory.
* Run this command
```bash
pip install -r requirements.txt
```
* Now we are good to go.

Run using Python:
```bash
python Del_pages_pdf
```
1 change: 1 addition & 0 deletions scripts/PDF-Delete-Pages/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PyPDF2
Binary file added scripts/PDF-Delete-Pages/testPDF.pdf
Binary file not shown.
Binary file added scripts/PDF-Delete-Pages/testPDF_deleted.pdf
Binary file not shown.