Skip to content

Instantly share code, notes, and snippets.

@kokoro-aya
Last active December 6, 2022 15:35
Show Gist options
  • Save kokoro-aya/5ef4cc61c243dbe1de71fdcfb965a07b to your computer and use it in GitHub Desktop.
Save kokoro-aya/5ef4cc61c243dbe1de71fdcfb965a07b to your computer and use it in GitHub Desktop.
Extract PDF pages from a single PDF file with range
from argparse import ArgumentParser
from PyPDF2 import PdfFileReader, PdfWriter
def main():
parser = ArgumentParser(
prog="A simple PDF page extractor",
description="A simple PDF page extractor",
epilog="Text at the bottom for help"
)
parser.add_argument("input")
parser.add_argument("output")
parser.add_argument("start")
parser.add_argument("to")
args = parser.parse_args()
input_file = args.input
output_file = args.output
start = int(args.start)
to = int(args.to)
if start < 0 or to < 0:
raise "Starting and ending page must be a natural number."
if start > to:
raise "Starting page must be smaller than ending page."
with open(input_file, 'rb') as reader:
pdf = PdfFileReader(reader)
info = pdf.getDocumentInfo()
page_num = pdf.getNumPages()
if start > page_num or to > page_num:
raise f"Either the starting page or ending page is out of range of the file pages. " \
f"The file has {page_num} pages."
pdf_out = PdfWriter()
for i in range(start, to):
pdf_out.addPage(pdf.getPage(i))
pdf_out.write(output_file)
print("Extraction finished.")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment