Skip to content

Commit d1837bd

Browse files
script to delete pages from a given PDF file. (avinashkranjan#294)
* added README,script,test file and output images to the folder * Minor edit Co-authored-by: Abhinav Anand <manasgmishra@gmail.com>
1 parent c2cd214 commit d1837bd

File tree

6 files changed

+81
-0
lines changed

6 files changed

+81
-0
lines changed

delete-pages-pdf/MyPDF.pdf

3.43 KB
Binary file not shown.

delete-pages-pdf/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Delete pages in a PDF
2+
3+
- Using this script you can delete the pages you don't want with just one click.
4+
- I have added a test PDF with the script which you can run to test the script.
5+
6+
## Setup instructions
7+
8+
- We use **pdfrw** library for this script.
9+
- To install this library, type `pip install pdfrw` in your terminal.
10+
- To run the script run `python3 script.py` and then enter the information as prompted by the program.
11+
12+
13+
## Output
14+
15+
A short demo of how the script works
16+
17+
Here We have a pdf file MyPDF.pdf from which we have to delete pages.
18+
19+
![Image 1](img/1.png)
20+
21+
Running the script and entering path of the file and the page numbers to be deleted seperated by "," .
22+
23+
![Image 2](img/2.png)
24+
25+
The pages in the PDF file has been deleted
26+
27+
![Image 3](img/3.png)
28+
29+
30+
## Author
31+
32+
[Mohta Rahul Suresh](https://github.com/Rahul555-droid/)
33+
34+
## Disclaimers
35+
36+
**Once you delete the pages using this script they cannot be RETRIEVED so be CAREFUL!**

delete-pages-pdf/img/1.png

43.9 KB
Loading

delete-pages-pdf/img/2.png

8.39 KB
Loading

delete-pages-pdf/img/3.png

42.6 KB
Loading

delete-pages-pdf/script.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from pdfrw import PdfWriter, PdfReader
2+
import os
3+
4+
5+
def delete(path, del_page):
6+
"""
7+
The function delete takes two arguments path and del_page
8+
path is the path of the source pdf file.
9+
This function deletes the pages from the pdf file.
10+
11+
Parameters:
12+
path : Path of the pdf file.
13+
del_page : A list of pages to be deleted.
14+
15+
Returns:
16+
None
17+
"""
18+
# create a pdf object using PdfReader that could be read
19+
pdf_obj = PdfReader(path)
20+
# pdf_obj.pages attribute gives the length of the pages in pdf
21+
total_pages = len(pdf_obj.pages)
22+
print("Total Pages in PDF are:", total_pages)
23+
# Initialising the writer object using the PdfWriter class
24+
writer = PdfWriter()
25+
26+
# Adding only those pages that we need to this list excluding del_page
27+
page_list = [page for page in range(
28+
1, total_pages+1) if page not in del_page]
29+
30+
# Index of pdf_obj.pages starts from 0.
31+
for page in page_list:
32+
writer.addpage(pdf_obj.pages[page-1])
33+
34+
# removing the original pdf
35+
os.remove(path)
36+
# writing the modified file to the memory
37+
writer.write(path)
38+
39+
40+
path = input("enter the path(full or relative) of the pdf file:")
41+
del_page = input(
42+
"Enter the pages to be deleted seperated by comma(,):").strip().split(",")
43+
del_page = [int(i) for i in del_page]
44+
delete(path, del_page)
45+
print("\n\t pages", *del_page, "have been deleted successfully!!!")

0 commit comments

Comments
 (0)