Extract PDF title & rename all files on a directory
mai77 May 6th, 2011 898 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- # -*- 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
- actfile = file(fileName, "rb")
- input1 = PdfFileReader(actfile)
- trgtfilename = input1.getDocumentInfo().title + "_" + fileName
- print "\n## ERROR ## %s Title could not be extracted. PDF file may be encrypted!" % fileName
- del input1
- print 'Trying to rename from:', fileName, ' to ', trgtfilename
- print fileName, ' could not be renamed!'
- print '\n error: are prior names unique? Maybe the filename already exists or the document is already opened!'
RAW Paste Data