-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOM_comparator.py
286 lines (207 loc) · 7.72 KB
/
BOM_comparator.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# -*- coding: utf-8 -*-
"""
Created on Tue May 2 16:09:14 2023
@author: Filip Poposki
"""
import os
from openpyxl import Workbook
import pandas as pd
def get_file_list():
'''
Returns
-------
list
list contains names of files found in folder. Alarms user if no files found.
'''
if len(os.listdir()) == 1:
print(f"Working directory: {os.getcwd()}")
print(f"Found the following files: {os.listdir()}\n")
else:
print(f"Working directory: {os.getcwd()}")
print(f"Found the following files: {os.listdir()}\n")
return os.listdir()
def check_count_of_files(list_files):
'''
Parameters
----------
list_files : list
list of files found in folder.
Returns
-------
bool
pass if number of files is ok to proceed further
'''
if len(list_files) < 3:
print('Not enough total files in folder')
return False
else:
return True
def find_xlsx_file(list_files_folder):
'''
Parameters
----------
list_files : list
list of files found in folder.
Returns
-------
list
returns list of files where .xlsx is found
'''
xlsx_files_list = []
for i in range(0,len(list_files_folder)):
if list_files_folder[i].endswith(".xlsx"):
xlsx_files_list.append(list_files_folder[i])
return xlsx_files_list
def check_count_xslx_files(list_xlsx_files):
'''
Parameters
----------
list_files : list
list of files with .xlsx found from find_xlsx_file()
Returns
-------
bool
pass if number of files is ok to proceed further
'''
if len(list_xlsx_files) == 2:
return True
else:
print("Number of .xlsx files is not adequate")
return False
def bom_to_grouped_df(bom_filename):
'''
Parameters
----------
bom_filename : string
DESCRIPTION.
Returns
-------
grouped_df : dataframe
DESCRIPTION.
'''
# Import file as Dataframe
df = pd.read_excel(f"{bom_filename}")
#Drop NaN rows
df = df.dropna(axis = 0)
#Drop first rows (not needed)
df = df.iloc[3:]
#Change collumn name
df.columns.values[0] = "ID"
#Leave only rows that dont contain the string "Level"
df = df.loc[~df['ID'].str.contains('Level')]
#Replace specific strings that are not needed
#keep in mind the number of empty spaces!!!
# count empty spaces in excel and then insert the exact number
df['ID'] = df['ID'].str.replace('LcsReleased ', '')
df['ID'] = df['ID'].str.replace('LcsWorking ', '')
df['ID'] = df['ID'].str.replace('LcsUpgrade ', '')
# Leave only rows that contain the string "PC"
df = df[df['ID'].str.contains('PC')]
# Split collumn ID into 2 collumns, first is Part number with split_length of chars, and the second is the rest
split_length = 8
df = pd.DataFrame({'Part_number': df['ID'].str[:split_length], 'Ref': df['ID'].str[split_length:]})
#Split Ref collumn to have only the designator (found after string PC)
df['Ref'] = df['Ref'].str.split('PC').str[-1]
# Remove empty spaces from ref column
df['Ref'] = df['Ref'].str.replace(" ","")
#Grouping of df by ref/designator (that is unique, can have multiple part numbers on a single designator)
# values are given in list
grouped_df = df.groupby('Ref').agg({'Part_number': list})
return grouped_df
def get_dict_from_processed_bom_df(processed_df_from_bom):
'''
Parameters
----------
processed_df_from_bom : Dataframe
DESCRIPTION.
Returns
-------
bom_dict : dict
DESCRIPTION.
'''
# Creates a dict with unique Ref,list
bom_dict = processed_df_from_bom.to_dict()['Part_number']
# Delete all values with no key (key = ''), meaning with no designator
del bom_dict[""]
return bom_dict
def create_bom_difference_dict(dict1,dict2,filename1,filename2):
'''
Parameters
----------
dict1 : dictionary
DESCRIPTION.
dict2 : dictionary
DESCRIPTION.
filename1 : string
DESCRIPTION.
filename2 : string
DESCRIPTION.
Returns
-------
None.
'''
differences = {}
for key in dict1:
if key not in dict2:
differences[key] = (dict1[key], "Not populated in this BOM")
elif key in dict2:
if dict1[key] != dict2[key]:
differences[key] = (dict1[key], dict2[key])
for key in dict2:
if key not in dict1:
differences[key] = ("Not populated in this BOM", dict2[key])
elif key in dict1:
if dict1[key] != dict2[key]:
differences[key] = (dict1[key], dict2[key])
return differences
def generate_bom_difference_report(dict1,dict2,dict_differences,bom_name1,bom_name2):
'''
Parameters
----------
dict1 : dictionary
dictionary contains ref designators and PNs from bom_name1.
dict2 : dictionary
dictionary contains ref designators and PNs from bom_name2.
dict_differences : dictionary
contains dictionary of differences.
bom_name1 : string
name of first file.
bom_name2 : string
name of second file.
Returns
-------
None.
'''
workbook = Workbook()
sheet1 = workbook.active
sheet1.title = "Comparison of BOMs"
sheet1.append(["Reference Designator", bom_name1, bom_name2])
#Populates the comparison sheet with the values from the differences list
for key in dict_differences:
sheet1.append([f"{key}", f"{dict_differences[key][0]}", f"{dict_differences[key][1]}"])
#Creates workbook to have overview of ref designators and PNs in bom_name1
sheet2 = workbook.create_sheet(f"{bom_name1}")
sheet2.append(["Reference Designator", bom_name1])
for key in dict1:
sheet2.append([f"{key}", f"{dict1[key]}"])
#Creates workbook to have overview of ref designators and PNs in bom_name2
sheet3 = workbook.create_sheet(f"{bom_name2}")
sheet3.append(["Reference Designator", bom_name2])
for key in dict2:
sheet3.append([f"{key}", f"{dict2[key]}"])
workbook.save(f"Comparison results for BOMs {bom_name1} and {bom_name2}.xlsx")
print("Report generated. Close program and check the excel file.")
if __name__ == "__main__":
files_in_folder = get_file_list()
if check_count_of_files(files_in_folder):
xlsx_files_in_folder = find_xlsx_file(files_in_folder)
if check_count_xslx_files(xlsx_files_in_folder):
bom_filename1 = xlsx_files_in_folder[0]
bom_filename2 = xlsx_files_in_folder[1] # Useful for further processing
bom_df1 = bom_to_grouped_df(bom_filename1)
bom_df2 = bom_to_grouped_df(bom_filename2)
bom_dict1 = get_dict_from_processed_bom_df(bom_df1)
bom_dict2 = get_dict_from_processed_bom_df(bom_df2)
bom_differences_dict = create_bom_difference_dict(bom_dict1, bom_dict2, bom_filename1, bom_filename2)
generate_bom_difference_report(bom_dict1, bom_dict2, bom_differences_dict, bom_filename1, bom_filename2)
os.system("pause")