Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def write_merged_pdf(pdf1, pdf2, output_pdf, **kargs):
merger = PdfFileMerger()
num_pages1 = PdfFileReader(pdf1).getNumPages()
num_pages2 = PdfFileReader(pdf2).getNumPages()
reverse1 = kargs['reverse1'] or False
reverse2 = kargs['reverse2'] or False
# check file page difference
num_difference = num_pages1 - num_pages2
if num_difference < 0 or num_difference > 1:
raise Exception('Invalid page numbers: in1:%d in2:%d' % (num_pages1, num_pages2))
for index in range(num_pages1):
page1 = index
if reverse1:
page1 = num_pages1 - index - 1
print(page1)
merger.append(fileobj=pdf1, pages=(page1, page1 + 1))
# check if the merged document page is odd
if index <= num_pages2:
page2 = index
if reverse2:
page2 = num_pages2 - index - 1
merger.append(fileobj=pdf2, pages=(page2, page2 + 1))
merger.write(output_pdf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment