jhan11 jhan11 - 6 months ago 14
Python Question

Allowing for possible unknown file names to be processed in a python script

I am using a script that overlays an input pdf onto another that is essentially a letterhead. However, I am not sure how to allow for the process to be automated to allow for many files to be processed one at a time, without previously knowing what the file will be named. I am using python 2.7.

from pyPdf import PdfFileWriter, PdfFileReader


output = PdfFileWriter()
input1 = PdfFileReader(file("example.pdf", "rb"))

# add page 1 from input1 to output document, unchanged
output.addPage(input1.getPage(0))

# add page 2 from input1, but first add a watermark from another pdf:
page2 = input1.getPage(0)
watermark = PdfFileReader(file("template.pdf", "rb"))
page2.mergePage(watermark.getPage(0))

output.addPage(page2)


# finally, write "output" to document-output.pdf
outputStream = file("example.pdf", "wb")
output.write(outputStream)
outputStream.close()

Answer

Thank you for all of the contributions. This is what I ended up using:

from pyPdf import PdfFileWriter, PdfFileReader
import os, sys, glob, shutil

path = glob.glob ('p:\letter2pdf\processing\*.pdf')
template = 'p:/letter2pdf/processing/template/template.pdf'
dest1 = 'p:\letter2pdf\output'
dest2 = 'p:\letter2pdf\processing'

for files in sorted(path):

    output = PdfFileWriter()
    input1 = PdfFileReader(file(files,"rb"))

    print "title = %s" % (input1.getDocumentInfo().title)

# add page 1 from input1 to output document, unchanged
    output.addPage(input1.getPage(0))

#add a watermark from another pdf:
    page2 = input1.getPage(0)
    watermark = PdfFileReader(file (template, "rb"))
    page2.mergePage(watermark.getPage(0))



# finally, write "output" to document-output.pdf
    outputStream = file(input1.getDocumentInfo().title, "wb")
    output.write(outputStream)
    outputStream.close()
Comments