Created
January 31, 2017 16:07
-
-
Save Geekfish/a4fe4efd59e158f55ca5c76479831c8d to your computer and use it in GitHub Desktop.
pyPDF2 merge 2 pdf pages into one
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 characters
from PyPDF2 import PdfFileReader, PdfFileWriter | |
from PyPDF2.pdf import PageObject | |
reader = PdfFileReader(open("invoice.pdf",'rb')) | |
invoice_page = reader.getPage(0) | |
sup_reader = PdfFileReader(open("supplement.pdf",'rb')) | |
sup_page = sup_reader.getPage(1) # We pick the second page here | |
translated_page = PageObject.createBlankPage(None, sup_page.mediaBox.getWidth(), sup_page.mediaBox.getHeight()) | |
translated_page.mergeScaledTranslatedPage(sup_page, 1, 0, -400) # -400 is approximate mid-page | |
translated_page.mergePage(invoice_page) | |
writer = PdfFileWriter() | |
writer.addPage(translated_page) | |
with open('out.pdf', 'wb') as f: | |
writer.write(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An extra simple way to merge unlimited pdf's: