-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshift_hours_generator.pyw
93 lines (69 loc) · 2.35 KB
/
shift_hours_generator.pyw
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
import os
import platform
import re
import subprocess
import typing
from pathlib import Path
import PySimpleGUI as sg
import pytesseract
from pyexcel_ods3 import save_data
sg.theme('SandyBeach')
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sept", "oct", "nov", "dec"]
class Shift:
def __init__(self, line: str):
split = re.split(r"\s+|-|(?<=pm):", line.strip().lower())
if len(split) != 4:
raise Exception("bad line: " + line)
self.month = to_month(split[0])
self.day = split[1]
self.start = split[2].lower()
self.end = split[3].lower()
def to_month(month: str):
matches = [m.capitalize() for m in months if month.lower().lstrip().startswith(m)]
if len(matches) > 0:
return matches[0]
return ""
def to_suffix(day):
if 4 <= day <= 20 or 24 <= day <= 30:
suffix = "th"
else:
suffix = ["st", "nd", "rd"][day % 10 - 1]
return suffix
def to_sheet(shifts: typing.List[Shift]):
sheet = []
for s in shifts:
num = re.match(r"^\d+", s.day).group()
day = num + to_suffix(int(num))
sheet.append([s.month + " " + day, s.start, s.end])
return sheet
def create_window():
layout = [
[sg.Text('Shifts Image'), sg.In(), sg.FileBrowse(key="file")],
[sg.Text(key='output_file')],
[sg.Button('Generate'), sg.Cancel()]
]
window = sg.Window("Shift Table Generator", layout)
return window
def main():
window = create_window()
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Cancel':
break
window['output_file'].Update("")
path = values['file']
res: str = pytesseract.image_to_string(path)
lines = [x for x in res.splitlines() if to_month(x) != ""]
shifts = [Shift(line) for line in lines]
sheet = {
"data": to_sheet(shifts)
}
output_path = generate_output_path(path)
save_data(output_path, sheet)
window['output_file'].Update(f"Data written to: {output_path}")
window.close()
def generate_output_path(path):
return str(Path(path).parent.absolute().joinpath(Path(path).stem + '.ods'))
if __name__ == '__main__':
main()