-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractpdf.py
35 lines (26 loc) · 1.1 KB
/
extractpdf.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
33
34
35
import argparse
from PyPDF2 import PdfFileWriter, PdfFileReader
def split(inputFileName, outputFileName, startPage, endPage):
inputpdf = PdfFileReader(open(inputFileName, "rb"))
output = PdfFileWriter()
for i in range(startPage - 1, endPage):
output.addPage(
inputpdf.getPage(i)
)
with open(outputFileName, "wb") as outputStream:
output.write(outputStream)
def main():
parser = argparse.ArgumentParser(
prog="extract-pdf-pages",
description="Extract pages from a pdf file\npy extractpdf.py ./inputfile.pdf 1-20 ./outputfile.pdf",
)
parser.add_argument("inputFileName", help = "Input file");
parser.add_argument("pages", help = "Pages to extract, first page(inclusive)-last page(inclusive), eg: 1-20");
parser.add_argument("outputFileName", help = "Output file");
args = parser.parse_args()
# Parse pages
startPage = int(args.pages.split("-")[0])
endPage = int(args.pages.split("-")[1])
split(args.inputFileName, args.outputFileName, startPage, endPage)
if __name__ == "__main__":
main()