Appending Pages to a PDF with Python

One of my customers created ~950 PDF’s of….something flood and letter related. I don’t know what exactly. Hey man, I just work the shovel.

Anyways, they decided to tack a disclaimer on each of them after the PDF’s were made, and rather than appending them by hand * 950 I told them I’d see if I could automate it for them. I nabbed PyPdf, wrote a little Python script, and tacked the disclaimer page on the front of all ~950 of them in about 2 minutes. I thought I’d post the code here in case anybody ran across a similar need.

Note PyPdf will raise a few deprication warnings if you’re on Python 2.6. You can safely ignore those unless you’re on or heading to 3.0.

# This takes a single PDF page from one PDF document and appends it to
# the front of an existing document, saving the new document as a new
# copy in another folder. Requires the pyPDF extension. You will also need to
# tell the script the location of the cover page, the folder that contains the
# PDF files to append it to, and an output folder for the modified files.

import os
from pyPdf import PdfFileWriter, PdfFileReader

print "Beginning conversion...."

# Set the cover to append to the front of the docs
inputcover = PdfFileReader(file("ELEVATION CERTIFICATE DISCLAIMER.pdf", "rb"))

# Loop through the source directory to get the PDF files and convert them
# Change the source directory here as needed
dirList=os.listdir("source")
for fname in dirList:
    if fname.endswith(".pdf"):
        print "Processing " + str(fname) + "...."

        # set up the pdfwriter and the input source
        output = PdfFileWriter()
        inputFile = "source/" + str(fname)
        input1 = PdfFileReader(file(inputFile, "rb"))

        # add the cover page to the PDF output
        output.addPage(inputcover.getPage(0))

        # add the rest of the pages to the PDf output
        for i in range (0, input1.getNumPages() ):
            output.addPage(input1.getPage(i))

        # write the output file - change output folder as needed
        outputFile = "output/" + str(fname)
        outputStream = file(outputFile, "wb")
        output.write(outputStream)
        outputStream.close()

print "Conversion complete."