1

I am trying to use this class to append a page from one PDF to another by specifying the page position.

Does anyone have experience with this? I couldn't find any example of using PdfFileMerger.merge over internet

with open(orig_pdf, 'rb') as orig, open(amend_pdf, 'rb') as new:

pdf = PdfFileMerger()
pdf.merge(2, new)
pdf.write('.pdf')
Royal
  • 218
  • 2
  • 17

1 Answers1

1

Consider using merge and passing the position, which is the page number you wish to add the pdf file

There are (probably) many ways of achieving the same results. Heres a basic working example:

from PyPDF2 import PdfFileMerger, PdfFileReader

orig_pdf = r'C:\temp\old.pdf'
amend_pdf = r'C:\temp\new.pdf'

with open(orig_pdf, 'rb') as orig, open(amend_pdf, 'rb') as new:
    merger = PdfFileMerger()
    merger.append(PdfFileReader(orig_pdf))
    
    # Add amend_pdf after page 2
    merger.merge(2, PdfFileReader(amend_pdf))
    merger.write("results.pdf")

For more info, have a look at the official documentation https://pythonhosted.org/PyPDF2/PdfFileMerger.html

Greg
  • 4,468
  • 3
  • 16
  • 26