Last active
May 15, 2019 13:08
-
-
Save dreua/04d69e3f3f8c9ac3fc91063bf0fe4309 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 characters
#!/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