-
Notifications
You must be signed in to change notification settings - Fork 0
/
ey3.py
executable file
·239 lines (199 loc) · 7.07 KB
/
ey3.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
#!/usr/bin/env python
# coding=UTF-8
#
from openpyxl import Workbook
from openpyxl.styles import Side, Font, PatternFill, Border, Alignment
from openpyxl.chart import Reference, BarChart
# import openpyxl
import glob
FOLDER_PREFIX_NAME = "EY"
def find_between(s, first, last):
try:
start = s.index(first) + len(first)
end = s.index(last, start)
return s[start:end]
except ValueError:
return ""
def find_between_r(s, first, last):
try:
start = s.rindex(first) + len(first)
end = s.rindex(last, start)
return s[start:end]
except ValueError:
return ""
def excel_create():
wb = Workbook()
ws = wb.active
# remove the sheet named "Sheet"
wb.remove_sheet(ws)
return wb
def excel_save(wb, filename):
# Save the file
wb.save(filename)
def set_allborder(ws, cell_range):
rows = list(ws.iter_rows(cell_range))
side = Side(border_style='thin', color="FF000000")
for pos_y, cells in enumerate(rows):
for pos_x, cell in enumerate(cells):
border = Border(
left=cell.border.left,
right=cell.border.right,
top=cell.border.top,
bottom=cell.border.bottom
)
border.left = side
border.right = side
border.top = side
border.bottom = side
cell.border = border
def set_border(ws, cell_range):
rows = list(ws.iter_rows(cell_range))
side = Side(border_style='thin', color="FF000000")
max_y = len(rows) - 1 # index of the last row
for pos_y, cells in enumerate(rows):
max_x = len(cells) - 1 # index of the last cell
for pos_x, cell in enumerate(cells):
border = Border(
left=cell.border.left,
right=cell.border.right,
top=cell.border.top,
bottom=cell.border.bottom
)
if pos_x == 0:
border.left = side
if pos_x == max_x:
border.right = side
if pos_y == 0:
border.top = side
if pos_y == max_y:
border.bottom = side
# set new border only if it's one of the edge cells
if pos_x == 0 or pos_x == max_x or pos_y == 0 or pos_y == max_y:
cell.border = border
def set_font(ws, cell_range):
rows = list(ws.iter_rows(cell_range))
font = Font(name='Calibri', size=11, bold=False, italic=False,
vertAlign=None, underline='none', strike=False,
color='FFFFFFFF')
for pos_y, cells in enumerate(rows):
for pos_x, cell in enumerate(cells):
cell.font = font
def set_alignment(ws, cell_range):
rows = list(ws.iter_rows(cell_range))
align_center = Alignment(horizontal='center')
for pos_y, cells in enumerate(rows):
for pos_x, cell in enumerate(cells):
cell.alignment = align_center
def set_background_color(ws, cell_range, color_string):
rows = list(ws.iter_rows(cell_range))
backgroundcolor = PatternFill(
fill_type="solid",
start_color='FF' + color_string,
end_color='FF' + color_string)
for pos_y, cells in enumerate(rows):
for pos_x, cell in enumerate(cells):
cell.fill = backgroundcolor
def mtp_cell_format(ws):
set_allborder(ws, "A1:B19")
set_font(ws, "A1:B1")
set_alignment(ws, "A1:B19")
color_string = '8b8989' # color hex string
set_background_color(ws, "A1:B1", color_string)
color_string = 'cdc9c9' # light gray
set_background_color(ws, "A2:B19", color_string)
def roi_mtp_dealwith(ws, folder):
ws['A1'] = "MTF"
ws['B1'] = "Value"
filefullpath = folder + "mtf/" + folder[0:6] + "-H-MTFOUT.txt"
with open(filefullpath, "r") as f:
for line in f:
roi, mtf = line.split('=')
index = find_between(roi, "ROI_", "_MTF")
roi_location = 'A' + str(int(index)+2)
ws[roi_location] = index
mtf_location = 'B' + str(int(index)+2)
ws[mtf_location] = (float(mtf))
filefullpath = folder + "mtf/" + folder[0:6] + "-V-MTFOUT.txt"
with open(filefullpath, "r") as f:
for line in f:
roi, mtf = line.split('=')
index = find_between(roi, "ROI_", "_MTF")
roi_location = 'A' + str(int(index)+2)
ws[roi_location] = index
mtf_location = 'B' + str(int(index)+2)
ws[mtf_location] = (float(mtf))
def sfr_cell_format(ws):
color_string = 'cdb79e' # light gray
set_font(ws, "D1:E1")
set_allborder(ws, "D1:E37")
set_alignment(ws, "D1:E37")
set_background_color(ws, "D1:E1", color_string)
color_string = 'ffdab9' # light gray
set_background_color(ws, "D2:E37", color_string)
def sfr_dealwith(ws, folder):
ws['D1'] = "SFR"
ws['E1'] = "Value"
filefullpath = folder + "/sfr/" + "SFROUT_shopfloor.txt"
with open(filefullpath, "r") as f:
for line in f:
roi, sfr = line.split('=')
index = find_between(roi, "ROI", "_SFR_RESULT")
roi_location = 'D' + str(int(index)+2)
ws[roi_location] = index
mtf_location = 'E' + str(int(index)+2)
ws[mtf_location] = float(sfr)
def excel_creatsheet(wb, ws_title):
ws = wb.create_sheet(title=ws_title)
return ws
def excel_mtf_barchart(ws):
chart1 = BarChart()
chart1.type = "col"
chart1.style = 10
chart1.title = "MTF Chart"
chart1.y_axis.title = 'MTF'
chart1.x_axis.title = 'ROI'
# Select all data include title
data = Reference(ws, min_col=2, min_row=1, max_row=19, max_col=2)
# Select data only
cats = Reference(ws, min_col=1, min_row=2, max_row=18)
chart1.add_data(data, titles_from_data=True)
chart1.set_categories(cats)
chart1.shape = 4
chart1.x_axis.scaling.min = 0
chart1.x_axis.scaling.max = 18
chart1.y_axis.scaling.min = 0
chart1.y_axis.scaling.max = 1
ws.add_chart(chart1, "G1")
def excel_sfr_barchart(ws):
chart1 = BarChart()
chart1.type = "col"
chart1.style = 12
chart1.title = "SFR Chart"
chart1.y_axis.title = 'SFR'
chart1.x_axis.title = 'ROI'
# Select all data include title
data = Reference(ws, min_col=5, min_row=1, max_row=37, max_col=5)
# Select data only
cats = Reference(ws, min_col=4, min_row=2, max_row=37)
chart1.add_data(data, titles_from_data=True)
chart1.set_categories(cats)
chart1.shape = 4
chart1.x_axis.scaling.min = 0
chart1.x_axis.scaling.max = 37
chart1.y_axis.scaling.min = 0
chart1.y_axis.scaling.max = 1
ws.add_chart(chart1, "G21")
def find_directoies_with_substring(ey):
return glob.glob(ey)
if __name__ == '__main__':
ey_folders = find_directoies_with_substring(FOLDER_PREFIX_NAME + "*/")
wb = excel_create()
for ey in ey_folders:
ws_sn = excel_creatsheet(wb, ey[:-1])
mtp_cell_format(ws_sn)
roi_mtp_dealwith(ws_sn, ey)
sfr_cell_format(ws_sn)
sfr_dealwith(ws_sn, ey)
excel_mtf_barchart(ws_sn)
excel_sfr_barchart(ws_sn)
excel_save(wb, "ey3.xlsx")