Code:
# -*- coding: cp1252 -*-
# script to rename PDF files according to with title + name (unique)
# pyPdf available at http://pybrary.net/pyPdf
# runs as python thisPy.py in a UNIX-shell (in windows "python" not required)
# http://blog.isnotworking.com/2006/08/extract-pdf-title-from-all-files-on.html
from pyPdf import PdfFileWriter, PdfFileReader
import os
trgtfilename = ""
for fileName in os.listdir('.'):
if fileName.lower()[-3:] != "pdf": continue
try:
actfile = file(fileName, "rb")
input1 = PdfFileReader(actfile)
trgtfilename = input1.getDocumentInfo().title + "_" + fileName
except:
print "\n## ERROR ## %s Title could not be extracted. PDF file may be encrypted!" % fileName
continue
del input1
actfile.close()
print 'Trying to rename from:', fileName, ' to ', trgtfilename
try:
os.rename(fileName,trgtfilename)
except:
print fileName, ' could not be renamed!'
print '\n error: are prior names unique? Maybe the filename already exists or the document is already opened!'
|