Python Forum

Full Version: Pdf merging
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to make my pdf appear as page 1 being the upper half of first page, and page 2 being the lower half of first page and so on.

Currently my code just moves page 2 onto page 1 completely overlaying what was on page 1 previously.

If anyone has any suggestions on how to achieve this I'd greatly appreciate it!

from PyPDF2 import PdfFileReader, PdfFileWriter


output = PdfFileWriter()

file_name = '81plots.pdf'

file1 = PdfFileReader(open(file_name, 'rb'))
file2 = PdfFileReader(open(file_name, 'rb'))

i = 0
for i in range(file1.getNumPages()-1):
    page = file1.getPage(i)
    page.mergePage(file2.getPage(i+1))
    output.addPage(page)

outfile = 'testfile.pdf'

with open(outfile, 'wb') as file:
    output.write(file)
Just guessing, would it be in the The Destination Class attributes?
see: http://pybrary.net/pyPdf/pythondoc-pyPdf.pdf.html
I know this is PyPdf and not PyPDF2, but PyPDF2's documentation points to it.
If anyone is wondering the solution. I figured out by using mergeTranslatedPage() function, and by setting the mediaBox() function large enough to fit both of my pdf pages.