Python PDF Merger

This post is to show how Python can be used to merge PDF files together. I was using a scanner recently that couldn’t combine multiple pages into a single document, so I had to save each page individually. I found a script at http://stackoverflow.com/questions/3444645/merge-pdf-files that was a life saver, so I am reproducing it here:

# Loading the pyPdf Library
from pyPdf import PdfFileWriter, PdfFileReader

# Creating a routine that appends files to the output file
def append_pdf(input,output):
 [output.addPage(input.getPage(page_num)) for page_num in range(input.numPages)]

# Creating an object where pdf pages are appended to
output = PdfFileWriter()

# Appending three pdf-pages from three different files
append_pdf(PdfFileReader(open("page_1.pdf","rb")),output)
append_pdf(PdfFileReader(open("page_2.pdf","rb")),output)
append_pdf(PdfFileReader(open("page_3.pdf","rb")),output)
append_pdf(PdfFileReader(open("page_4.pdf","rb")),output)
append_pdf(PdfFileReader(open("page_5.pdf","rb")),output)

# Writing all the collected pages to a file
output.write(open("combined_pages.pdf","wb"))

Leave a Reply

Your email address will not be published. Required fields are marked *