Skip to content

Instantly share code, notes, and snippets.

@PhotonQuantum
Last active February 9, 2020 09:12
Show Gist options
  • Save PhotonQuantum/69eb0d061192824c48048dbad961aa14 to your computer and use it in GitHub Desktop.
Save PhotonQuantum/69eb0d061192824c48048dbad961aa14 to your computer and use it in GitHub Desktop.
from PyPDF2 import PdfFileReader
import sys
from os import listdir, rename
from os.path import join
scan_dir = sys.argv[1]
files = [join(scan_dir, fn) for fn in listdir(scan_dir)]
for fn in listdir(scan_dir):
if fn[-4:] == ".pdf":
with open(join(scan_dir, fn), mode="rb") as f:
pdfparser = PdfFileReader(f)
doc_info = pdfparser.getDocumentInfo()
title = doc_info.get("/Title", None)
if title:
new_fn = title + ".pdf"
print(fn + " -> " + new_fn)
# rename(join(scan_dir, fn), join(scan_dir, new_fn))
else:
print(fn + " FAIL")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment