-
Notifications
You must be signed in to change notification settings - Fork 0
/
CFD_ITR_Lookup_Tool.py
137 lines (106 loc) · 5.45 KB
/
CFD_ITR_Lookup_Tool.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
136
137
# In-service training record tool, born out of Illinois State audit of random records
# We moved ITRs to Vector Solutions but have 10+ years of data we may have to access in the future
# legacy data is located on a share point list
# that file is located in private install files or can be downloaded from the source
import pandas as pd
import tkinter as tk
from tkinter import filedialog, simpledialog
import os
import re
# Lets Go! ITR tracker v3 2/8/24 [email protected]
#clean and convert duration strings to numbers
def clean_and_convert_duration(duration):
# Convert the duration to a string
duration_str = str(duration)
numeric_part = re.search(r'\d+\.*\d*', duration_str)
if numeric_part:
return float(numeric_part.group())
return 0.0
# prompt user to select a directory for saving the output Excel file
def select_output_directory():
output_directory = filedialog.askdirectory(title="Select Output Directory")
return output_directory
# search and destroy
def search_and_write_to_excel():
file_number = simpledialog.askstring("Enter File Number", "Please enter the File Number:")
if file_number is not None:
# Filter the DataFrame based on file number
filtered_df = df[
df[['Officer\'s File #', 'Member 2 File', 'Member 3 File', 'Member 4 File', 'Member 5 File']].eq(
file_number).any(axis=1)]
if not filtered_df.empty:
# store class hours
class_hours = {}
# store class hours for each year
class_hours_by_year = {}
# Iterate through each row in the df
for index, row in filtered_df.iterrows():
year = row['Date'].year
# Iterate through each drill column and extract drill names and durations
for i in range(1, 5):
drill_column = f"Drill #{i}"
duration_column = f"Drill #{i} Duration"
drill = row[drill_column]
duration = row[duration_column]
if not pd.isnull(drill): # Check if drill exists derp
if drill in class_hours:
class_hours[drill] += clean_and_convert_duration(duration)
else:
class_hours[drill] = clean_and_convert_duration(duration)
if year in class_hours_by_year:
if drill in class_hours_by_year[year]:
class_hours_by_year[year][drill] += clean_and_convert_duration(duration)
else:
class_hours_by_year[year][drill] = clean_and_convert_duration(duration)
else:
class_hours_by_year[year] = {drill: clean_and_convert_duration(duration)}
# dictionary to DataFrame
class_summary_df = pd.DataFrame(list(class_hours.items()), columns=['Class', 'Total Hours'])
# Sort the DataFrame
class_summary_df = class_summary_df.sort_values(by='Total Hours', ascending=False)
# output file path
output_directory = select_output_directory()
if output_directory:
output_file_path = os.path.join(output_directory, f"{file_number}_ITR_Records.xlsx")
with pd.ExcelWriter(output_file_path, engine='xlsxwriter') as writer:
# class summary to a new sheet
class_summary_df.to_excel(writer, sheet_name='Class Summary', index=False)
# class details to the main sheet
filtered_df.drop(columns=['Item Type', 'Path'], errors='ignore').to_excel(writer, sheet_name='ITRs', index=False)
# separate sheets for each year
years = filtered_df['Date'].dt.year.unique()
for year in years:
year_df = pd.DataFrame(list(class_hours_by_year[year].items()),
columns=['Class', 'Total Hours'])
year_df = year_df.sort_values(by='Total Hours', ascending=False)
year_sheet_name = str(year)
year_df.to_excel(writer, sheet_name=year_sheet_name, index=False)
print(f"Class details saved to {output_file_path}")
else:
print(f"No training records found for File Number: {file_number}")
# GUI window for selecting the Excel file
root = tk.Tk()
root.withdraw()
# select the Excel file
selected_file = filedialog.askopenfilename(filetypes=[("Excel Files", "*.xlsx")])
if selected_file:
df = pd.read_excel(selected_file, sheet_name='ITRs') # Assuming the sheet name is 'ITRs'
# GUI window
root = tk.Tk()
root.title("Training Record Lookup")
# Center the window
window_width = 400
window_height = 150
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_position = (screen_width - window_width) // 2
y_position = (screen_height - window_height) // 2
root.geometry(f"{window_width}x{window_height}+{x_position}+{y_position}")
# button to trigger the search and write to Excel
search_button = tk.Button(root, text="Search and Write to Excel", command=search_and_write_to_excel)
search_button.pack(pady=20)
# Run the GUI loop
root.mainloop()
else:
print("No Excel file selected. Exiting program.")
# End