Created
May 29, 2020 16:21
-
-
Save Dabsunter/3b007175e3e83157e70d8f0d223edae0 to your computer and use it in GitHub Desktop.
Rename every PDFs in the relative `rename` directory according to their metadatas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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