-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprefix_filename_date_ocr.py
More file actions
executable file
·167 lines (130 loc) · 4.29 KB
/
Copy pathprefix_filename_date_ocr.py
File metadata and controls
executable file
·167 lines (130 loc) · 4.29 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#! /Users/kaeff/.bin/.venv/bin/python3
from argparse import ArgumentParser, BooleanOptionalAction
from datetime import datetime
import os
import glob
import re
from pdfminer.high_level import extract_text
from dateutil import parser
import ocrmypdf
# Map German month names to English
MONTH_GERMAN_TO_ENGLISH = {
"Januar": "January",
"Februar": "February",
"März": "March",
"April": "April",
"Mai": "May",
"Juni": "June",
"Juli": "July",
"August": "August",
"September": "September",
"Oktober": "October",
"November": "November",
"Dezember": "December"
}
def translate_month_names(text):
# Replace German month names with English
for month in MONTH_GERMAN_TO_ENGLISH:
text = text.replace(month, MONTH_GERMAN_TO_ENGLISH[month])
return text
def filter_for_date(text):
date_text = ""
pattern_german_names = '([0-9]{1,2})[_\\.-] (' + \
'|'.join(MONTH_GERMAN_TO_ENGLISH.keys()) + \
') ([0-9]{2,4})'
pattern = '([0-9]{2})[_\\.-]([0-9]{2})[_\\.-]([0-9]{2,4})'
pattern_long = '([0-9]{2})\\. ?([a-zA-Z]+)[ \\.]([0-9]{4})'
# If written within the text, takes prescedence
match = re.search(pattern_german_names, text)
if match:
date_text = match.group(0)
else:
match = re.search(pattern, text)
if match:
date_text = match.group(0)
else:
match = re.search(pattern_long, text)
if match:
date_text = match.group(0)
return date_text
def extract_date(text):
text = filter_for_date(text)
text = translate_month_names(text)
# Parse date using dateutil with fuzzy=True
try:
return parser.parse(text, fuzzy=True, dayfirst=True, yearfirst=True)
except ValueError:
return None
def perform_ocr(pdf_file):
ocrmypdf.configure_logging(verbosity=-1)
print("Performing OCR for: " + pdf_file)
dirname, filename = os.path.split(pdf_file)
basename, ext = os.path.splitext(filename)
tmp_pdf_ocred = os.path.join(dirname, basename + ".ocr" + ext)
if os.path.exists(tmp_pdf_ocred):
os.remove(tmp_pdf_ocred)
ocrmypdf.ocr(pdf_file, tmp_pdf_ocred, force_ocr=True)
text = extract_text(tmp_pdf_ocred)
os.remove(tmp_pdf_ocred)
return text
def get_new_filename(pdf_file):
if re.search('^\\d{4}-\\d{2}-\\d{2}_', os.path.basename(pdf_file)):
return pdf_file
# Check if filename contains a date
date = extract_date(pdf_file)
# Otherwise, extract text from PDF
if not date:
text = extract_text(pdf_file)
date = extract_date(text)
# Otherwise, try OCR
if not date:
text = perform_ocr(pdf_file)
date = extract_date(text)
# Otherwise, use mtime
if not date:
# Use last modified timestamp as date
timestamp = os.path.getmtime(pdf_file)
date = datetime.fromtimestamp(timestamp).date()
# Rename file with prefix and date
if date:
formatted_date = date.strftime("%Y-%m-%d")
new_file_name = os.path.join(
os.path.dirname(pdf_file),
f"{formatted_date}_{os.path.basename(pdf_file)}")
else:
new_file_name = pdf_file
return new_file_name
def prefix_filename_date_ocr(force, files):
for pdf_file in files:
# Get new file name
new_file_name = get_new_filename(pdf_file)
# Rename file
if force:
os.rename(pdf_file, new_file_name)
print("Renamed: " + new_file_name)
else:
print(pdf_file + " -> " + new_file_name)
def main():
arg_parser = ArgumentParser()
arg_parser.add_argument(
"-f", "--force",
action=BooleanOptionalAction,
help="Actually rename files")
arg_parser.add_argument(
"-t", "--print-text",
action=BooleanOptionalAction,
help="Print OCR text for debugging")
arg_parser.add_argument(
"files",
nargs="*",
default=glob.glob("*.PDF") + glob.glob("*.pdf"),
help="List of files to prefix"
)
args = arg_parser.parse_args()
if not args.force:
print("DRY RUN - no files will be renamed. " +
"Call script with argument --force to rename files")
print()
prefix_filename_date_ocr(args.force, args.files)
if __name__ == "__main__":
main()