-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckList_Generator.py
More file actions
128 lines (109 loc) · 4.7 KB
/
Copy pathCheckList_Generator.py
File metadata and controls
128 lines (109 loc) · 4.7 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
from docx import Document
from datetime import datetime
import os, sys
# Gets the correct path to the template file when running as an .exe or as a python file
def resource_path(relative_path):
# Get the path to the resource (file) bundled with the executable
try:
# PyInstaller creates a temp folder and stores the path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
# Save the modified document to a new file
def save_document(doc, name):
doc.save(f'{name}.docx')
return f"{name}.docx"
# Replaces words in the body of the document
def replace_document_words(hashmap, doc):
placeholder_mapping = {
'DEVICE_PH': 'deviceType',
'CUSTOMER_PH': 'customer',
'SERIAL_PH': 'serial',
'MID_PH': 'mid',
'MODEM_PH': 'modem',
'IMEI_PH': 'imei',
'PI_PH': 'pi_version',
'TESTFIXTURE_PH': 'testFixer',
'VOLTAGETESTER_PH': 'voltageTester',
'BP_PH': 'batteryPack',
'USB_PH': 'usb',
'CAM_PH': 'cameraType',
'CAMSN_PH': 'cameraSerial',
'USER_PH': 'cameraUsername',
'PASS_PH': 'cameraPass',
'IP_PH': 'cameraIP',
}
# Iterate through each paragraph in the document
for paragraph in doc.paragraphs:
# Iterate through each run (a part of the paragraph with the same style/formatting)
for run in paragraph.runs:
for placeholder, key in placeholder_mapping.items():
if placeholder in run.text:
run.text = run.text.replace(placeholder, hashmap[key])
# replaces words in tables
def replace_table_words(hashmap, doc):
table_mapping = {
'MODEM_PH': 'modem',
'SD_PH': 'sd_size',
'IMEI_PH': 'imei',
'PI_PH': 'pi_version',
'COMPRESSION_TYPE_PH': 'compression_type',
'TESTFIXTURE_PH': 'testFixer',
'VOLTAGETESTER_PH': 'voltageTester',
'BP_PH': 'batteryPack',
'USB_PH': 'usb',
'CAM_PH': 'cameraType',
'CAMSN_PH': 'cameraSerial',
'USER_PH': 'cameraUsername',
'PASS_PH': 'cameraPass',
'IP_PH': 'cameraIP',
}
# Iterate through tables in the document
for table in doc.tables:
# Iterate through each row in table
for row in table.rows:
# Iterate through each column in the row
for cell in row.cells:
# Iterate through each paragraph in the cell (cells contain paragraphs)
for paragraph in cell.paragraphs:
# Iterate through each word in the paragraph
for run in paragraph.runs:
for placeholder, key in table_mapping.items():
if placeholder in run.text:
run.text = run.text.replace(placeholder, hashmap[key])
# In the created document this section replaces words in the header
def replace_header_words(hashmap, doc):
for section in doc.sections:
header = section.header
for paragraph in header.paragraphs:
for run in paragraph.runs:
if 'DATE_PH' in run.text:
current_date = datetime.now().strftime("%m/%d/%Y")
run.text = run.text.replace('DATE_PH', current_date)
continue
def generate_checklist_document(hashmap):
documentLocation = None
deviceType = hashmap["deviceType"]
# Load the template document
if deviceType == "LHC/LHG":
doc = Document(resource_path('templates/LHCTemplate.docx')) # Use resource_path to get the bundled file
else:
doc = Document(resource_path(f'templates/{deviceType}Template.docx'))
replace_document_words(hashmap, doc)
replace_table_words(hashmap, doc)
# Save the document and return the location
documentLocation = save_document(doc, hashmap["serial"])
return documentLocation
def generate_customer_info_document(hashmap):
documentLocation = None
deviceType = hashmap["deviceType"]
# Load the template document
if deviceType == "LHC/LHG":
doc = Document(resource_path('customerInfoTemplates/LHCCustomerTemplate.docx')) # Use resource_path to get the bundled file
else:
doc = Document(resource_path(f'customerInfoTemplates/{deviceType}CustomerTemplate.docx'))
replace_header_words(hashmap, doc)
replace_document_words(hashmap, doc)
documentLocation = save_document(doc, f'{hashmap["serial"]}_customer')
return documentLocation