SHARE
TWEET

Extract PDF title & rename all files on a directory

mai77 May 6th, 2011 866 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: cp1252 -*-
  2. # script to rename PDF files according to with title + name (unique)
  3. # pyPdf available at http://pybrary.net/pyPdf
  4. # runs as  python thisPy.py      in  a UNIX-shell (in windows "python" not required)
  5. # http://blog.isnotworking.com/2006/08/extract-pdf-title-from-all-files-on.html
  6.  
  7.  
  8. from pyPdf import PdfFileWriter, PdfFileReader
  9. import os
  10. trgtfilename = ""
  11.  
  12. for fileName in os.listdir('.'):
  13.   if fileName.lower()[-3:] != "pdf": continue
  14.   try:
  15.     actfile = file(fileName, "rb")
  16.     input1 = PdfFileReader(actfile)  
  17.     trgtfilename = input1.getDocumentInfo().title + "_" + fileName
  18.   except:
  19.     print "\n## ERROR ## %s Title could not be extracted. PDF file may be encrypted!" % fileName
  20.     continue
  21.  
  22.   del input1
  23.   actfile.close()
  24.  
  25.   print 'Trying to rename from:', fileName, ' to ', trgtfilename
  26.   try:
  27.     os.rename(fileName,trgtfilename)
  28.   except:
  29.     print fileName, ' could not be renamed!'
  30.     print '\n error: are prior names unique? Maybe the filename already exists or the document is already opened!'
RAW Paste Data
Want to get better at Python?
Learn to code Python in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top