This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRead_Timesheet.py
136 lines (107 loc) · 4.97 KB
/
Read_Timesheet.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
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
import os
import openpyxl
import json
from openpyxl.utils import get_column_letter
class FindFiles:
"""Finds the excell file with the titles "Timesheet - {username} - Week{number of week}" """
def __init__(self):
self._patch = "D:\\Desktop\\"
self._user = "Bastiaan Van Denabeele"
self._files = dict()
def get_folder_list(self):
"""
:return: returns a list of all the files in the selected folder
"""
return os.listdir(self._patch)
def find_excel(self):
"""returns and updates a dict with the als key the week and year of the file
as value the pacth of the located file
"""
for file in self.get_folder_list():
if "Timesheet - {} - Week".format(self._user)in file:
key = file.split(" - ")[2].split(".")[0]
self._files.update({key: self._patch+file})
return self._files
class ReadTimeSheets:
"""
Read the input of the found excel files
"""
def __init__(self):
self._file_list_dict = FindFiles().find_excel()
self._time_sheet_input = dict()
def get_files_dir(self):
return self._file_list_dict
def get_sheets(self):
"""returns a dict with the als key the week and year of the file
as value the sheet 'Timesheet' of the excel file
"""
returndict = dict()
for keys, value in self._file_list_dict.items():
returndict.update({keys: openpyxl.load_workbook(value).get_sheet_by_name('Timesheet')})
return returndict
def get_sheet_input(self):
"""get the sheet input from the timesheet
:return: a dict with al the input sorted by year and by timesheet
"""
# returns a dict with the as key the week and year of the file
# as value a dict
# In that dict the key are the coordinates of the cells en the value the values of the cells
sheet_inputs = dict()
for sheet_title, sheet_object in self.get_sheets().items():
sheet_year = sheet_title.split(" ")[2]
if sheet_year not in sheet_inputs.keys():
sheet_inputs[sheet_year] = {sheet_title: dict()}
else:
sheet_inputs[sheet_year].update({sheet_title: dict()})
for rowOfCellObjects in sheet_object['A5':self.get_last_entry_timesheet(sheet_object)]:
for cellObj in rowOfCellObjects:
if cellObj.value is not None:
sheet_inputs[sheet_year].get(sheet_title).update({cellObj.coordinate: cellObj.value})
return sheet_inputs
def get_last_entry_timesheet(self, sheet_object: openpyxl):
"""find the maximum range of data in the sheet for a timesheet
:param sheet_object: sheet timesheet
:return: the column letter and row number
"""
return self.get_last_entry_column(sheet_object, row=4) + str(self.get_last_entry_row(sheet_object, start_row=5))
@staticmethod
def get_last_entry_row(sheet_object: openpyxl, start_row=1, column=1):
# find the row number of the last entry in the given column
row = start_row
last_entry_row = row
while sheet_object.cell(column=column, row=row).value is not None:
last_entry_row = row
row += 1
return last_entry_row
@staticmethod
def get_last_entry_column(sheet_object: openpyxl, start_column=1, row=1):
# find the column number of the last entry the given row
column = start_column
last_entry_column = column
while sheet_object.cell(column=column, row=row).value is not None:
last_entry_column = column
column += 1
return get_column_letter(last_entry_column)
def get_type_of_activity_data(self, file_week=''):
""" updates a the json file with the type of activity"""
type_of_activity = dict()
# get the path of the gives file or get a random path in the dict self._file_list_dict
if file_week in self._file_list_dict.keys():
patch = self._file_list_dict.get(file_week)
else:
patch = list(self._file_list_dict.values())[0]
activity_sheet = openpyxl.load_workbook(patch).get_sheet_by_name('TypeOfActivity')
for i in range(2, self.get_last_entry_row(activity_sheet)):
column = 2
while activity_sheet.cell(column=column, row=i).value is not None \
and activity_sheet.cell(column=column, row=i).value != 1:
column += 1
if activity_sheet.cell(column=column, row=i).value == 1:
type_of_activity.update({activity_sheet.cell(column=1, row=i).value:
activity_sheet.cell(column=column, row=1).value})
return type_of_activity
def update_type_of_activity_json(self):
with open("type_of_activity.json", 'w') as outfile:
json.dump(self.get_type_of_activity_data(), outfile)
if __name__ == "__main__":
pass