-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
28 lines (23 loc) · 790 Bytes
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# playground
import os
from pdfrw import PdfReader
import random
path = "testpaper/"
def renameFileToPDFTitle(path, fileName):
fullName = os.path.join(path, fileName)
print(fullName)
# Extract pdf title from pdf file
newName = PdfReader(fullName).Info.Title
if str(newName) == "()":
newName = str(random.randint(1, 1e5))
# Remove surrounding brackets that some pdf titles have
newName = newName.strip('()') + '.pdf'
print(newName)
newFullName = os.path.join(path, newName)
os.rename(fullName, newFullName)
for fileName in os.listdir(path):
# Rename only pdf files
fullName = os.path.join(path, fileName)
if (not os.path.isfile(fullName) or fileName[-4:] != '.pdf'):
continue
renameFileToPDFTitle(path, fileName)