Skip to content

Instantly share code, notes, and snippets.

@kokoro-aya
Last active December 6, 2022 15:35

Revisions

  1. kokoro-aya renamed this gist Dec 6, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. kokoro-aya created this gist Dec 6, 2022.
    47 changes: 47 additions & 0 deletions main.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    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()