Want more features on Pastebin? Sign Up, it's FREE!
Guest

Extract PDF title & rename all files on a directory

By: mai77 on May 6th, 2011  |  syntax: Python  |  size: 1.04 KB  |  views: 815  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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!'
clone this paste RAW Paste Data