Last active
December 6, 2022 15:35
Revisions
-
kokoro-aya renamed this gist
Dec 6, 2022 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
kokoro-aya created this gist
Dec 6, 2022 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()