Skip to content

Instantly share code, notes, and snippets.

@informationsea
Created May 2, 2012 05:52
Show Gist options
  • Save informationsea/2574216 to your computer and use it in GitHub Desktop.
Save informationsea/2574216 to your computer and use it in GitHub Desktop.
Rename PDF by meta data
#!/usr/bin/env python2.7
import argparse
import pyPdf
import os
import os.path
def _main():
parser = argparse.ArgumentParser(description='rename PDF files')
parser.add_argument('pdf', nargs='+', type=argparse.FileType('rb'))
parser.add_argument('--format', default='{date} - {title}.pdf', help='default:%(default)s')
options = parser.parse_args()
for f in options.pdf:
try:
pdfinfo = pyPdf.PdfFileReader(f).getDocumentInfo()
infodict = {'title': pdfinfo.title,
'author': pdfinfo.author,
'subject': pdfinfo.subject,
'date': pdfinfo['/ModDate'][2:8]}
# print infodict
newfilename = options.format.format(**infodict)
newfilename = newfilename.replace(':', ' ')
newfilename = newfilename.replace('/', ' ')
newfilename = newfilename.replace('\\', ' ')
newfilename = newfilename.replace('|', ' ')
newfilename = newfilename.replace('*', '+')
newfilename = newfilename.replace('?', '.')
newfilename = newfilename.replace('"', '\'')
newfilename = newfilename.replace('\n', ' ')
if f.name == newfilename:
print 'No rename', newfilename
continue
if os.path.exists(newfilename):
print 'ERROR', 'name conflict', newfilename, f.name
continue
os.rename(f.name, newfilename)
print 'Rename', newfilename
except:
print 'Some error'
if __name__ == '__main__':
_main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment