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.
On using your exact same code, I am able to get two
PDFas mergedPDFin 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 useopen()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.04withpython 2.7.Here is the code:
I hope this helps.
UPDATE:
Here is the code which is working for me and merging the two pdf's page as single page.
I have added a new code and now it is also rotating the final pdf. Output PDF that you need is
final.pdfAnd here is the Google Drive link to my drive for
PDFfiles. Also, I made slight changes intopdfnup.pyfor compatibility with my system forImmutablesetif you want to use the same file then, you can find it too in the drive link above.