Skip to content

Instantly share code, notes, and snippets.

@dreua
Forked from Geekfish/merge_pdfs.py
Last active Sep 19, 2022
Embed
What would you like to do?
pyPDF2 merge 2 pdf pages into one
#!/bin/python3
from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2 import PageObject
# Theses files are just for testing, no point in merging these
reader = PdfFileReader(open("Nextcloud Manual.pdf",'rb'))
# this defines the output page format (relevant if not the same)
sup_reader = PdfFileReader(open("Cplusplus.pdf",'rb'))
writer = PdfFileWriter()
for pageNo in range(min(reader.getNumPages(), sup_reader.getNumPages())):
print("Merging page:", pageNo)
invoice_page = reader.getPage(pageNo)
sup_page = sup_reader.getPage(pageNo)
translated_page = PageObject.createBlankPage(None, sup_page.mediaBox.getWidth(), sup_page.mediaBox.getHeight())
translated_page.mergeScaledTranslatedPage(sup_page, 1, 0, 0)
translated_page.mergePage(invoice_page)
writer.addPage(translated_page)
with open('out.pdf', 'wb') as f:
writer.write(f)
@dreua
Copy link
Author

dreua commented Sep 16, 2022

@fomightez Thank you, I just edited this Gist accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment