Python and PDF

Here is an example of how to merge the first pages of a number of pdf pages into a single file.

I wrote this script, because I needed to print the first page of 200 individual files and I didn’t really want to open each one manually…

 

import os.path, pyPdf
from os import walk
 
output = pyPdf.PdfFileWriter()
original = "c:\\original\\folder\\"
f = []
 
for (dirpath, dirnames, filenames) in walk(original): f.extend(filenames)
 
for eachfile in filenames: 
	ffile = original + "\\" + eachfile
 
	if "pdf" in eachfile:
		pdf = pyPdf.PdfFileReader(open(ffile, "rb"))
		output.addPage(pdf.getPage(0))
	print ffile
 
outputStream = open("c:\\out.pdf", "wb")
output.write(outputStream)
outputStream.close()