Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/python3
from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2.pdf import PageObject
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('first', help='First file (first page becomes first page)')
parser.add_argument('second', help='Second file (first page becomes second page)')
parser.add_argument('output', nargs='?', help='Output file name (optional)')
args = parser.parse_args()
if args.output:
output_file = args.output
else:
output_file = args.first[:-4] + '_merged.pdf'
reader = PdfFileReader(open(args.first,'rb'))
sup_reader = PdfFileReader(open(args.second,'rb'))
writer = PdfFileWriter()
for pageNo in range(reader.getNumPages() + sup_reader.getNumPages()):
if (pageNo < reader.getNumPages()):
writer.addPage(reader.getPage(pageNo))
if (pageNo < sup_reader.getNumPages()):
writer.addPage(sup_reader.getPage(pageNo))
with open(output_file, 'wb') as f:
writer.write(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment