|
| 1 | +# Import Libraries |
| 2 | +from PyPDF4 import PdfFileMerger |
| 3 | +import os |
| 4 | +import argparse |
| 5 | + |
| 6 | + |
| 7 | +def merge_pdfs(input_files: list, page_range: tuple, output_file: str, bookmark: bool = True): |
| 8 | + """ |
| 9 | + Merge a list of PDF files and save the combined result into the `output_file`. |
| 10 | + `page_range` to select a range of pages (behaving like Python's range() function) from the input files |
| 11 | + e.g (0,2) -> First 2 pages |
| 12 | + e.g (0,6,2) -> pages 1,3,5 |
| 13 | + bookmark -> add bookmarks to the output file to navigate directly to the input file section within the output file. |
| 14 | + """ |
| 15 | + # strict = False -> To ignore PdfReadError - Illegal Character error |
| 16 | + merger = PdfFileMerger(strict=False) |
| 17 | + for input_file in input_files: |
| 18 | + bookmark_name = os.path.splitext(os.path.basename(input_file))[0] if bookmark else None |
| 19 | + # pages To control which pages are appended from a particular file. |
| 20 | + merger.append(fileobj=open(input_file, 'rb'), pages=page_range, bookmark=bookmark_name) |
| 21 | + # Insert the pdf at specific page |
| 22 | + merger.write(fileobj=open(output_file, 'wb')) |
| 23 | + merger.close() |
| 24 | + |
| 25 | + |
| 26 | +def parse_args(): |
| 27 | + """Get user command line parameters""" |
| 28 | + parser = argparse.ArgumentParser(description="Available Options") |
| 29 | + parser.add_argument('-i', '--input_files', dest='input_files', nargs='*', |
| 30 | + type=str, required=True, help="Enter the path of the files to process") |
| 31 | + parser.add_argument('-p', '--page_range', dest='page_range', nargs='*', |
| 32 | + help="Enter the pages to consider e.g.: (0,2) -> First 2 pages") |
| 33 | + parser.add_argument('-o', '--output_file', dest='output_file', |
| 34 | + required=True, type=str, help="Enter a valid output file") |
| 35 | + parser.add_argument('-b', '--bookmark', dest='bookmark', default=True, type=lambda x: ( |
| 36 | + str(x).lower() in ['true', '1', 'yes']), help="Bookmark resulting file") |
| 37 | + # To Parse The Command Line Arguments |
| 38 | + args = vars(parser.parse_args()) |
| 39 | + # To Display The Command Line Arguments |
| 40 | + print("## Command Arguments #################################################") |
| 41 | + print("\n".join("{}:{}".format(i, j) for i, j in args.items())) |
| 42 | + print("######################################################################") |
| 43 | + return args |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == "__main__": |
| 47 | + # Parsing command line arguments entered by user |
| 48 | + args = parse_args() |
| 49 | + # convert a single str to a list |
| 50 | + input_files = [str(x) for x in args['input_files'][0].split(',')] |
| 51 | + page_range = None |
| 52 | + if args['page_range']: |
| 53 | + page_range = tuple(int(x) for x in args['page_range'][0].split(',')) |
| 54 | + # call the main function |
| 55 | + merge_pdfs( |
| 56 | + input_files=input_files, page_range=page_range, |
| 57 | + output_file=args['output_file'], bookmark=args['bookmark'] |
| 58 | + ) |
0 commit comments