PyPDF2 - merging pages from two different PDF files is not working

8.6k views Asked by At

I'm trying to merge pages from two PDF files into a single PDF with a single page. So I tried the code below that uses PyPDF2:

from PyPDF2 import PdfFileReader,PdfFileWriter
import sys
f = sys.argv[1]
k = sys.argv[2]
print f,k
file1 = PdfFileReader(file(f, "rb"))
file2 = PdfFileReader(file(k, "rb"))
output = PdfFileWriter()
page = file1.getPage(0)
page.mergePage(file2.getPage(0))
output.addPage(page)
outputStream = file("join.pdf", "wb")
output.write(outputStream)
outputStream.close()

It produces a single file and single page with the contents of page 1 from file 1, but I don't find any data from page 1 of file2. Seems like it didn't get merged.

2

There are 2 answers

23
Shubham Namdeo On BEST ANSWER

On using your exact same code, I am able to get two PDF as merged PDF in one page with the second one overlapping the first one, I referred this link for detailed information.

And, instead of file() it is better to use open() as per this Python Documentation, so I did that.

Also, I made slight changes in your code but still, the working is same and correct on my machine. I am using Ubuntu 16.04 with python 2.7.

Here is the code:

from PyPDF2 import PdfFileReader,PdfFileWriter
import sys

f = sys.argv[1]
k = sys.argv[2]
print f, k
file1 = PdfFileReader(open(f, "rb"))
file2 = PdfFileReader(open(k, "rb"))
output = PdfFileWriter()
page = file1.getPage(0)
page.mergePage(file2.getPage(0))
output.addPage(page)

with open("join.pdf", "wb") as outputStream:
    output.write(outputStream)

I hope this helps.


UPDATE:

Here is the code which is working for me and merging the two pdf's page as single page.

from pyPdf import PdfFileWriter, PdfFileReader
from pdfnup import generateNup

initial_output = PdfFileWriter()
input1 = PdfFileReader(open("landscape1.pdf", "rb"))
input2 = PdfFileReader(open("landscape2.pdf", "rb"))

initial_output.addPage(input1.getPage(0))
initial_output.addPage(input2.getPage(0))

# creates a new pdf file with required pages as separate pages.
initial_output.write(file("final.pdf", "wb"))

# merges newly created pdf file pages as one.
generateNup("final.pdf", 2, "intermediate.pdf")

# overwrite and rotates the final.pdf
final_output = PdfFileWriter()
final_output.addPage(PdfFileReader(open("intermediate.pdf", "rb")).getPage(0).rotateClockwise(90)) 
final_output.write(open("final.pdf", "wb"))

I have added a new code and now it is also rotating the final pdf. Output PDF that you need is final.pdf

And here is the Google Drive link to my drive for PDF files. Also, I made slight changes into pdfnup.py for compatibility with my system for Immutableset if you want to use the same file then, you can find it too in the drive link above.

0
Rohit Behera On
def merge_page(self, output_pdf,*input_pdfs):
    a=len(input_pdfs)
    print (a)
    merge = PyPDF2.PdfFileMerger()
    outputStream = open(output_pdf, "wb")
    if a<2:
        raise Exception ("Need Atleast Two Pdf for Merging")

    else:
        for x in input_pdfs:
            merge.append(open(x,"rb"))
        merge.write(outputStream)
    outputStream.close()

For me this code is working in PyCharm and it can take n no of pdf files for merging into single pdf file but the no should be 2 or more less than that will give error.