Tuesday, March 11, 2014

PDF merging without tears

I guess nobody needs a cumbersome Acrobat software to merge PDF files. And when things gets a little bit more complex, manually merging hundreds of PDFs can be a PIAS.

Use this:

from PyPDF2 import PdfFileMerger, PdfFileReader
import os


filenames = ["1.pdf", "2.pdf"]
merger = PdfFileMerger()
for filename in filenames:
    merger.append(PdfFileReader(file(os.path.join(os.getcwd(), filename), 'rb')))
merger.write(os.path.join(os.getcwd(), "output.pdf"))

It works like magic. Put this .py file to the folder where you would like to have some PDFs merged, change filenames, and modify this script. Life is instantly easier.

Written with StackEdit.

No comments:

Post a Comment