Skip to content

Instantly share code, notes, and snippets.

@Dabsunter
Created May 29, 2020 16:21
Show Gist options
  • Save Dabsunter/3b007175e3e83157e70d8f0d223edae0 to your computer and use it in GitHub Desktop.
Save Dabsunter/3b007175e3e83157e70d8f0d223edae0 to your computer and use it in GitHub Desktop.
Rename every PDFs in the relative `rename` directory according to their metadatas
import os
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
print("Pretty PDF name")
for subdir, dirs, files in os.walk("rename"):
print("Dans", subdir)
for oldname in files:
oldpath = subdir + os.sep + oldname
if oldpath.endswith(".pdf"):
try:
fp = open(oldpath, 'rb')
parser = PDFParser(fp)
doc = PDFDocument(parser)
inf = doc.info[0]
title = str(inf['Title'])[2:-1]
authors = str(inf['Author'])[2:-1]
year = str(inf['CreationDate'])[4:8]
newname = authors.split(',')[0] + " " + year + " " + title + ".pdf"
newpath = subdir + os.sep + newname
fp.close()
os.rename(oldpath, newpath)
print(oldname, "renommé en", newname)
except:
print("ça a buggé sur" + oldname)
print("Game over")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment