-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincident_initiation.py
More file actions
116 lines (89 loc) · 4.4 KB
/
incident_initiation.py
File metadata and controls
116 lines (89 loc) · 4.4 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
#!/usr/bin/env python3
# File name: Incident_Initiation.py
# Author: Kyle LaPolice
# Date created: 1 - Jul - 2022
# Date last modified: 20 - Jan - 2022
# Script Version: 1.3
from pathlib import Path
from typing import Any
import PySimpleGUI as sg # pip install PySimpleGUI
import pandas as pd # pip install pandas openpyxL
from docxtpl import DocxTemplate # pip install docxtpl
#remove data validation warnings from excel doc
import warnings
warnings.filterwarnings('ignore', category=UserWarning, module='openpyxl')
# Change name of window per project
window_name = 'Incident Initiation'
# Layout of window
layout = [
[sg.Text("Select Incident Log File:")],
[sg.Input(key = 'EXCEL', enable_events = True), sg.FileBrowse(key = 'eBROWSE')],
[sg.Text("Select Incidents to Initiate: (Hold CTRL to select multiple rows)")],
[sg.Table(values = [],
headings = ['Incident #', 'Event Date', 'Equipment #', ' EQ Description ', 'Assigned to?'],
key = 'TABLE',
expand_x = True,
justification = 'left',
enable_events = True,
select_mode=sg.TABLE_SELECT_MODE_EXTENDED)],
[sg.Text("Enter Name:")],
[sg.Input(key='NAME', enable_events = True, default_text = "")],
[sg.OK(key = 'GO'), sg.Cancel(key = 'Exit')]
]
# Creates window that user interacts with
window = sg.Window(window_name, layout)
# Run the Event Loop
while True:
event, values = window.read()
# Close window if events
if event == 'Exit' or event == sg.WIN_CLOSED:
break
# updates table with Excel sheet
if event == 'EXCEL':
# create dataframe with incident num colum, event date, eq num, eq desc, assigned
e_sheets = pd.read_excel(values.get('EXCEL'), sheet_name = "Equipment Incident Log")
e_dropna = e_sheets.dropna(subset=['Event Date']) # drop the rows with empty cells under Event Date
e_dropna['Event Date'] = e_dropna['Event Date'].dt.strftime('%d-%b-%Y') # reformat event date column to dd/mmm/yyyy
e_reverse = e_dropna.iloc[::-1] # reverse the order
e_index = e_reverse.reset_index(drop=True) # resets the index to new pruned and reversed dataframe
# create dataframe with columns used in table
table_values = e_index[['Equipment Incident #','Event Date', 'EQ #', 'EQ Description', 'Assigned to?']]
# convert dataframe to list and send to table
window['TABLE'].update(values = list(table_values.values.tolist()))
continue
# executes program
if event =='GO':
# converts from selected table row to specific eq incident num and adds to list
keep_list = values.get('TABLE')
joined_list = []
for items in values.get('TABLE'):
row = e_index['Equipment Incident #'][items]
joined_list.append(row)
# creates dataframe from rows that were selected in the TABLE
cleaned = e_index[e_index['Equipment Incident #'].isin(joined_list)]
#add user name input to dataframe
cleaned = cleaned.assign(Name = values.get('NAME'))
print(cleaned)
# removes from the column headers "spaces" and "#", and then removes "/" from the entire dataframe
cleaned.columns = cleaned.columns.str.replace(' ','_')
cleaned.columns = cleaned.columns.str.replace('#','')
# removes "/" and """ from the entire dataframe
cleaned = cleaned.replace("/","", regex=True)
cleaned = cleaned.replace("\"","", regex=True)
cleaned = cleaned.replace("\t","", regex=True)
# sets directories
base_dir = Path(__file__).parent
template_path = base_dir / '_Template' / 'FRM-1297_C Incident Form.docx'
# itterates over template to create word docs from templates
for record in cleaned.to_dict(orient="records"):
doc = DocxTemplate(template_path)
doc.render(record)
# dicrectory to new incident folder lableing it via incident, strips whitespace at ends
output_dir = base_dir / f"{record['Equipment_Incident_']} ~ {record['EQ_Description']}".strip()
# checks if file already exists, if it does not exist creates the folder and saves the docx in it
if not output_dir.is_dir():
output_dir.mkdir(exist_ok=True)
output_path = output_dir / 'FRM-1297_C Incident Form.docx'
doc.save(output_path)
continue
window.close()