-
Notifications
You must be signed in to change notification settings - Fork 4
/
Simple_Pdf_Page_Remover.py
32 lines (27 loc) · 1015 Bytes
/
Simple_Pdf_Page_Remover.py
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
# Simple PDF Page Remover in Python3
# Author: Sriharsha Kotcharlakot
# Date: Sun, 16 Feb 2020
import subprocess
def install(name):
subprocess.call(['pip3', 'install', name])
try:
from PyPDF4 import PdfFileReader, PdfFileWriter
except:
install("PyPDF4")
from PyPDF4 import PdfFileReader, PdfFileWriter
def remove_page(pdf_path, page_numbers):
pdf_writer = PdfFileWriter()
pdf_reader = PdfFileReader(pdf_path, 'rb')
i=0
for page in [pdf_reader.getPage(i) for i in range(pdf_reader.getNumPages())]:
if not i in page_numbers:
pdf_writer.addPage(page)
i+=1
with open('Resultant_PDF_After_Removal.pdf', 'wb') as fh:
pdf_writer.write(fh)
print("!! Welcome to Simple PDF Page Remover !!")
print("Enter path to the required PDF:")
path = input()
print("Enter the page numbers (starting from 0) that are required to be removed delimiting with spaces (For Example: 0 2 3 4 67 34):")
page = list(map(int, input().split()))
remove_page(path, page)