This repository was archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremove-pages
More file actions
executable file
·51 lines (40 loc) · 1.39 KB
/
remove-pages
File metadata and controls
executable file
·51 lines (40 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
import subprocess
import sys
def print_help():
print("""
Removes specified pages from a given PDF file.
### Usage
1. Ensure that you have `pip` installed.
2. Run the script with the PDF file to be processed and the page numbers
to be removed (comma separated, no spaces).
```
$ ./remove-pages doc.pdf 1,2,3
```
3. The modified file is saved as `new_doc.pdf`.
""")
print("Installing requirements...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "PyPDF2"])
from PyPDF2 import PdfFileWriter, PdfFileReader
try:
if len(sys.argv) > 2:
# PDF file to be processed
file = sys.argv[1]
remove_pages = list(map(int, sys.argv[2].split(',')))
print(f"File to be processed:\t{file}")
print(f"Pages to be removed:\t{remove_pages}")
writer = PdfFileWriter()
# add selected pages to the PDF file writer
pdf = PdfFileReader(open(file, 'rb'))
for page in range(pdf.getNumPages()):
# Note: PyPDF2 pages start at 0, specified pages start at 1
if (page + 1) not in remove_pages:
writer.addPage(pdf.getPage(page))
# save modified PDF
writer.write(open("new_doc.pdf", 'wb'))
print("\nPage removal complete! Saved as: new_doc.pdf")
else:
print_help()
except Exception as e:
print("\nError: File could not be processed!")
print(e)