Total Complexity | 66 |
Total Lines | 624 |
Duplicated Lines | 8.49 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like excelexporters.meterenergy often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import base64 |
||
2 | import uuid |
||
3 | import os |
||
4 | from openpyxl.chart import ( |
||
5 | BarChart, |
||
6 | LineChart, |
||
7 | Reference, |
||
8 | ) |
||
9 | from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font |
||
10 | from openpyxl.drawing.image import Image |
||
11 | from openpyxl import Workbook |
||
12 | from openpyxl.chart.label import DataLabelList |
||
13 | import openpyxl.utils.cell as format_cell |
||
14 | |||
15 | |||
16 | #################################################################################################################### |
||
17 | # PROCEDURES |
||
18 | # Step 1: Validate the report data |
||
19 | # Step 2: Generate excelexporters file |
||
20 | # Step 3: Encode the excelexporters file to Base64 |
||
21 | #################################################################################################################### |
||
22 | |||
23 | View Code Duplication | def export(result, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type): |
|
|
|||
24 | #################################################################################################################### |
||
25 | # Step 1: Validate the report data |
||
26 | #################################################################################################################### |
||
27 | if result is None: |
||
28 | return None |
||
29 | |||
30 | #################################################################################################################### |
||
31 | # Step 2: Generate excel file from the report data |
||
32 | #################################################################################################################### |
||
33 | filename = generate_excel(result, |
||
34 | name, |
||
35 | reporting_start_datetime_local, |
||
36 | reporting_end_datetime_local, |
||
37 | period_type) |
||
38 | #################################################################################################################### |
||
39 | # Step 3: Encode the excel file to Base64 |
||
40 | #################################################################################################################### |
||
41 | try: |
||
42 | with open(filename, 'rb') as binary_file: |
||
43 | binary_file_data = binary_file.read() |
||
44 | except IOError as ex: |
||
45 | pass |
||
46 | |||
47 | # Base64 encode the bytes |
||
48 | base64_encoded_data = base64.b64encode(binary_file_data) |
||
49 | # get the Base64 encoded data using human-readable characters. |
||
50 | base64_message = base64_encoded_data.decode('utf-8') |
||
51 | # delete the file from server |
||
52 | try: |
||
53 | os.remove(filename) |
||
54 | except NotImplementedError as ex: |
||
55 | pass |
||
56 | return base64_message |
||
57 | |||
58 | |||
59 | def timestamps_data_all_equal_0(lists): |
||
60 | for i, value in enumerate(list(lists)): |
||
61 | if len(value) > 0: |
||
62 | return False |
||
63 | |||
64 | return True |
||
65 | |||
66 | |||
67 | def get_parameters_timestamps_lists_max_len(parameters_timestamps_lists): |
||
68 | max_len = 0 |
||
69 | for i, value in enumerate(list(parameters_timestamps_lists)): |
||
70 | if len(value) > max_len: |
||
71 | max_len = len(value) |
||
72 | |||
73 | return max_len |
||
74 | |||
75 | |||
76 | def timestamps_data_not_equal_0(lists): |
||
77 | number = 0 |
||
78 | for i, value in enumerate(list(lists)): |
||
79 | if len(value) > 0: |
||
80 | number += 1 |
||
81 | return number |
||
82 | |||
83 | |||
84 | View Code Duplication | def decimal_to_column(num=65): |
|
85 | string = '' |
||
86 | num = num - 64 |
||
87 | # The column number is not greater than 90 |
||
88 | if num <= 26: |
||
89 | return chr(num+64) |
||
90 | # The column number is greater than 90 |
||
91 | while num // 26 > 0: |
||
92 | if num % 26 == 0: |
||
93 | string += 'Z' |
||
94 | num = num // 26 - 1 |
||
95 | else: |
||
96 | string += chr(num % 26 + 64) |
||
97 | num //= 26 |
||
98 | # Avoid conversion errors that might occur between 741 and 766 |
||
99 | if num > 0: |
||
100 | string += chr(num + 64) |
||
101 | |||
102 | return string[::-1] |
||
103 | |||
104 | |||
105 | def column_to_decimal(string='A'): |
||
106 | num = 0 |
||
107 | for index, key in enumerate(string[::-1]): |
||
108 | num += (ord(key) - 64) * (26 ** index) |
||
109 | |||
110 | return num + 64 |
||
111 | |||
112 | |||
113 | def generate_excel(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type): |
||
114 | |||
115 | wb = Workbook() |
||
116 | ws = wb.active |
||
117 | |||
118 | # Row height |
||
119 | ws.row_dimensions[1].height = 102 |
||
120 | for i in range(2, 2000 + 1): |
||
121 | ws.row_dimensions[i].height = 42 |
||
122 | |||
123 | # for i in range(2, 11 + 1): |
||
124 | # ws.row_dimensions[i].height = 30 |
||
125 | # |
||
126 | # for i in range(12, 43 + 1): |
||
127 | # ws.row_dimensions[i].height = 30 |
||
128 | |||
129 | # Col width |
||
130 | ws.column_dimensions['A'].width = 1.5 |
||
131 | |||
132 | ws.column_dimensions['B'].width = 25.0 |
||
133 | |||
134 | for i in range(ord('C'), ord('L')): |
||
135 | ws.column_dimensions[chr(i)].width = 15.0 |
||
136 | |||
137 | # Font |
||
138 | name_font = Font(name='Constantia', size=15, bold=True) |
||
139 | title_font = Font(name='宋体', size=15, bold=True) |
||
140 | data_font = Font(name='Franklin Gothic Book', size=11) |
||
141 | |||
142 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
||
143 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
||
144 | right=Side(border_style='medium', color='00000000'), |
||
145 | bottom=Side(border_style='medium', color='00000000'), |
||
146 | top=Side(border_style='medium', color='00000000') |
||
147 | ) |
||
148 | b_border = Border( |
||
149 | bottom=Side(border_style='medium', color='00000000'), |
||
150 | ) |
||
151 | |||
152 | b_c_alignment = Alignment(vertical='bottom', |
||
153 | horizontal='center', |
||
154 | text_rotation=0, |
||
155 | wrap_text=True, |
||
156 | shrink_to_fit=False, |
||
157 | indent=0) |
||
158 | c_c_alignment = Alignment(vertical='center', |
||
159 | horizontal='center', |
||
160 | text_rotation=0, |
||
161 | wrap_text=True, |
||
162 | shrink_to_fit=False, |
||
163 | indent=0) |
||
164 | b_r_alignment = Alignment(vertical='bottom', |
||
165 | horizontal='right', |
||
166 | text_rotation=0, |
||
167 | wrap_text=True, |
||
168 | shrink_to_fit=False, |
||
169 | indent=0) |
||
170 | c_r_alignment = Alignment(vertical='bottom', |
||
171 | horizontal='center', |
||
172 | text_rotation=0, |
||
173 | wrap_text=True, |
||
174 | shrink_to_fit=False, |
||
175 | indent=0) |
||
176 | |||
177 | # Img |
||
178 | img = Image("excelexporters/myems.png") |
||
179 | img.width = img.width * 0.85 |
||
180 | img.height = img.height * 0.85 |
||
181 | # img = Image("myems.png") |
||
182 | ws.add_image(img, 'B1') |
||
183 | |||
184 | # Title |
||
185 | ws.row_dimensions[3].height = 60 |
||
186 | |||
187 | ws['B3'].font = name_font |
||
188 | ws['B3'].alignment = b_r_alignment |
||
189 | ws['B3'] = 'Name:' |
||
190 | ws['C3'].border = b_border |
||
191 | ws['C3'].alignment = b_c_alignment |
||
192 | ws['C3'].font = name_font |
||
193 | ws['C3'] = name |
||
194 | |||
195 | ws['D3'].font = name_font |
||
196 | ws['D3'].alignment = b_r_alignment |
||
197 | ws['D3'] = 'Period:' |
||
198 | ws['E3'].border = b_border |
||
199 | ws['E3'].alignment = b_c_alignment |
||
200 | ws['E3'].font = name_font |
||
201 | ws['E3'] = period_type |
||
202 | |||
203 | ws['F3'].font = name_font |
||
204 | ws['F3'].alignment = b_r_alignment |
||
205 | ws['F3'] = 'Date:' |
||
206 | ws['G3'].border = b_border |
||
207 | ws['G3'].alignment = b_c_alignment |
||
208 | ws['G3'].font = name_font |
||
209 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
||
210 | ws.merge_cells("G3:H3") |
||
211 | |||
212 | if "reporting_period" not in report.keys() or \ |
||
213 | "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
||
214 | filename = str(uuid.uuid4()) + '.xlsx' |
||
215 | wb.save(filename) |
||
216 | |||
217 | return filename |
||
218 | ################################################# |
||
219 | # First: 能耗分析 |
||
220 | # 6: title |
||
221 | # 7: table title |
||
222 | # 8~9 table_data |
||
223 | ################################################# |
||
224 | has_energy_data_flag = True |
||
225 | |||
226 | if "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
||
227 | has_energy_data_flag = False |
||
228 | |||
229 | if has_energy_data_flag: |
||
230 | ws['B6'].font = title_font |
||
231 | ws['B6'] = name + '能耗分析' |
||
232 | |||
233 | reporting_period_data = report['reporting_period'] |
||
234 | # print(reporting_period_data) |
||
235 | category = report['meter']['energy_category_name'] |
||
236 | ca_len = len(category) |
||
237 | |||
238 | ws.row_dimensions[7].height = 60 |
||
239 | |||
240 | ws['B7'].fill = table_fill |
||
241 | ws['B7'].border = f_border |
||
242 | |||
243 | ws['B8'].font = title_font |
||
244 | ws['B8'].alignment = c_c_alignment |
||
245 | ws['B8'] = '能耗' |
||
246 | ws['B8'].border = f_border |
||
247 | |||
248 | ws['B9'].font = title_font |
||
249 | ws['B9'].alignment = c_c_alignment |
||
250 | ws['B9'] = '环比' |
||
251 | ws['B9'].border = f_border |
||
252 | |||
253 | col = '' |
||
254 | |||
255 | for i in range(0, ca_len): |
||
256 | col = chr(ord('C') + i) |
||
257 | row = '7' |
||
258 | cell = col + row |
||
259 | ws[col + '7'].fill = table_fill |
||
260 | ws[col + '7'].font = name_font |
||
261 | ws[col + '7'].alignment = c_c_alignment |
||
262 | ws[col + '7'] = report['meter']['energy_category_name'] + " (" + report['meter']['unit_of_measure'] + ")" |
||
263 | ws[col + '7'].border = f_border |
||
264 | |||
265 | ws[col + '8'].font = name_font |
||
266 | ws[col + '8'].alignment = c_c_alignment |
||
267 | ws[col + '8'] = round(reporting_period_data['total_in_category'], 2) |
||
268 | ws[col + '8'].border = f_border |
||
269 | |||
270 | ws[col + '9'].font = name_font |
||
271 | ws[col + '9'].alignment = c_c_alignment |
||
272 | ws[col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
||
273 | if reporting_period_data['increment_rate'] is not None else "-" |
||
274 | ws[col + '9'].border = f_border |
||
275 | |||
276 | # TCE TCO2E |
||
277 | end_col = col |
||
278 | # TCE |
||
279 | tce_col = chr(ord(end_col) + 1) |
||
280 | ws[tce_col + '7'].fill = table_fill |
||
281 | ws[tce_col + '7'].font = name_font |
||
282 | ws[tce_col + '7'].alignment = c_c_alignment |
||
283 | ws[tce_col + '7'] = "吨标准煤 (TCE)" |
||
284 | ws[tce_col + '7'].border = f_border |
||
285 | |||
286 | ws[tce_col + '8'].font = name_font |
||
287 | ws[tce_col + '8'].alignment = c_c_alignment |
||
288 | ws[tce_col + '8'] = round(reporting_period_data['total_in_kgce'] / 1000, 2) |
||
289 | ws[tce_col + '8'].border = f_border |
||
290 | |||
291 | ws[tce_col + '9'].font = name_font |
||
292 | ws[tce_col + '9'].alignment = c_c_alignment |
||
293 | ws[tce_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
||
294 | if reporting_period_data['increment_rate'] is not None else "-" |
||
295 | ws[tce_col + '9'].border = f_border |
||
296 | |||
297 | # TCO2E |
||
298 | tco2e_col = chr(ord(end_col) + 2) |
||
299 | ws[tco2e_col + '7'].fill = table_fill |
||
300 | ws[tco2e_col + '7'].font = name_font |
||
301 | ws[tco2e_col + '7'].alignment = c_c_alignment |
||
302 | ws[tco2e_col + '7'] = "吨二氧化碳排放 (TCO2E)" |
||
303 | ws[tco2e_col + '7'].border = f_border |
||
304 | |||
305 | ws[tco2e_col + '8'].font = name_font |
||
306 | ws[tco2e_col + '8'].alignment = c_c_alignment |
||
307 | ws[tco2e_col + '8'] = round(reporting_period_data['total_in_kgco2e'] / 1000, 2) |
||
308 | ws[tco2e_col + '8'].border = f_border |
||
309 | |||
310 | ws[tco2e_col + '9'].font = name_font |
||
311 | ws[tco2e_col + '9'].alignment = c_c_alignment |
||
312 | ws[tco2e_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
||
313 | if reporting_period_data['increment_rate'] is not None else "-" |
||
314 | ws[tco2e_col + '9'].border = f_border |
||
315 | else: |
||
316 | for i in range(6, 9 + 1): |
||
317 | ws.row_dimensions[i].height = 0.1 |
||
318 | ################################################# |
||
319 | # Second: 能耗详情 |
||
320 | # 11: title |
||
321 | # 12 ~ 16: chart |
||
322 | # 18: table title |
||
323 | # 19~43: table_data |
||
324 | ################################################# |
||
325 | has_energy_detail_flag = True |
||
326 | reporting_period_data = report['reporting_period'] |
||
327 | times = reporting_period_data['timestamps'] |
||
328 | |||
329 | if "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
||
330 | has_energy_detail_flag = False |
||
331 | |||
332 | if has_energy_detail_flag: |
||
333 | reporting_period_data = report['reporting_period'] |
||
334 | category = report['meter']['energy_category_name'] |
||
335 | ca_len = len(category) |
||
336 | parameters_names_len = len(report['parameters']['names']) |
||
337 | start_detail_data_row_num = 7 + (parameters_names_len + ca_len) * 6 |
||
338 | |||
339 | ws['B11'].font = title_font |
||
340 | ws['B11'] = name + '详细数据' |
||
341 | |||
342 | ws.row_dimensions[start_detail_data_row_num].height = 60 |
||
343 | |||
344 | ws['B' + str(start_detail_data_row_num)].fill = table_fill |
||
345 | ws['B' + str(start_detail_data_row_num)].font = title_font |
||
346 | ws['B' + str(start_detail_data_row_num)].border = f_border |
||
347 | ws['B' + str(start_detail_data_row_num)].alignment = c_c_alignment |
||
348 | ws['B' + str(start_detail_data_row_num)] = '日期时间' |
||
349 | time = times |
||
350 | has_data = False |
||
351 | max_row = 0 |
||
352 | if len(time) > 0: |
||
353 | has_data = True |
||
354 | max_row = start_detail_data_row_num + len(time) |
||
355 | |||
356 | if has_data: |
||
357 | for i in range(0, len(time)): |
||
358 | col = 'B' |
||
359 | row = str(start_detail_data_row_num + 1 + i) |
||
360 | # col = chr(ord('B') + i) |
||
361 | ws[col + row].font = title_font |
||
362 | ws[col + row].alignment = c_c_alignment |
||
363 | ws[col + row] = time[i] |
||
364 | ws[col + row].border = f_border |
||
365 | |||
366 | for i in range(0, ca_len): |
||
367 | # 12 title |
||
368 | col = chr(ord('C') + i) |
||
369 | |||
370 | ws[col + str(start_detail_data_row_num)].fill = table_fill |
||
371 | ws[col + str(start_detail_data_row_num)].font = title_font |
||
372 | ws[col + str(start_detail_data_row_num)].alignment = c_c_alignment |
||
373 | ws[col + str(start_detail_data_row_num)] = report['meter']['energy_category_name'] + \ |
||
374 | " (" + report['meter']['unit_of_measure'] + ")" |
||
375 | ws[col + str(start_detail_data_row_num)].border = f_border |
||
376 | |||
377 | # 13 data |
||
378 | time = times |
||
379 | time_len = len(time) |
||
380 | |||
381 | for j in range(0, time_len): |
||
382 | row = str(start_detail_data_row_num + 1 + j) |
||
383 | # col = chr(ord('B') + i) |
||
384 | ws[col + row].font = title_font |
||
385 | ws[col + row].alignment = c_c_alignment |
||
386 | ws[col + row] = round(reporting_period_data['values'][j], 2) |
||
387 | ws[col + row].border = f_border |
||
388 | # line |
||
389 | # 13~: line |
||
390 | line = LineChart() |
||
391 | line.title = '报告期消耗 - ' + report['meter']['energy_category_name'] + \ |
||
392 | " (" + report['meter']['unit_of_measure'] + ")" |
||
393 | labels = Reference(ws, min_col=2, min_row=start_detail_data_row_num + 1, max_row=max_row) |
||
394 | bar_data = Reference(ws, min_col=3, min_row=start_detail_data_row_num, max_row=max_row) |
||
395 | line.add_data(bar_data, titles_from_data=True) |
||
396 | line.set_categories(labels) |
||
397 | line_data = line.series[0] |
||
398 | line_data.marker.symbol = "circle" |
||
399 | line_data.smooth = True |
||
400 | line.x_axis.crosses = 'min' |
||
401 | # line_data.smooth = True |
||
402 | line.height = 8.25 # cm 1.05*5 1.05cm = 30 pt |
||
403 | line.width = 24 |
||
404 | # pie.title = "Pies sold by category" |
||
405 | line.dLbls = DataLabelList() |
||
406 | # line.dLbls.showCatName = True # 标签显示 |
||
407 | line.dLbls = DataLabelList() |
||
408 | line.dLbls.dLblPos = 't' |
||
409 | line.dLbls.showVal = True # 数量显示 |
||
410 | line.dLbls.showPercent = False # 百分比显示 |
||
411 | # s1 = CharacterProperties(sz=1800) # 图表中字体大小 *100 |
||
412 | ws.add_chart(line, "B12") |
||
413 | |||
414 | col = 'B' |
||
415 | row = str(start_detail_data_row_num + 1 + len(time)) |
||
416 | |||
417 | ws[col + row].font = title_font |
||
418 | ws[col + row].alignment = c_c_alignment |
||
419 | ws[col + row] = '总计' |
||
420 | ws[col + row].border = f_border |
||
421 | |||
422 | for i in range(0, ca_len): |
||
423 | col = chr(ord(col) + 1) |
||
424 | ws[col + row].font = title_font |
||
425 | ws[col + row].alignment = c_c_alignment |
||
426 | ws[col + row] = round(reporting_period_data['total_in_category'], 2) |
||
427 | ws[col + row].border = f_border |
||
428 | |||
429 | else: |
||
430 | for i in range(11, 43 + 1): |
||
431 | ws.row_dimensions[i].height = 0.0 |
||
432 | |||
433 | ########################################## |
||
434 | has_parameters_names_and_timestamps_and_values_data = True |
||
435 | # 12 is the starting line number of the last line chart in the report period |
||
436 | category = report['meter']['energy_category_name'] |
||
437 | time_len = len(reporting_period_data['timestamps']) |
||
438 | ca_len = len(category) |
||
439 | current_sheet_parameters_row_number = 12 + ca_len * 6 |
||
440 | if 'parameters' not in report.keys() or \ |
||
441 | report['parameters'] is None or \ |
||
442 | 'names' not in report['parameters'].keys() or \ |
||
443 | report['parameters']['names'] is None or \ |
||
444 | len(report['parameters']['names']) == 0 or \ |
||
445 | 'timestamps' not in report['parameters'].keys() or \ |
||
446 | report['parameters']['timestamps'] is None or \ |
||
447 | len(report['parameters']['timestamps']) == 0 or \ |
||
448 | 'values' not in report['parameters'].keys() or \ |
||
449 | report['parameters']['values'] is None or \ |
||
450 | len(report['parameters']['values']) == 0 or \ |
||
451 | timestamps_data_all_equal_0(report['parameters']['timestamps']): |
||
452 | has_parameters_names_and_timestamps_and_values_data = False |
||
453 | if has_parameters_names_and_timestamps_and_values_data: |
||
454 | |||
455 | ############################### |
||
456 | # new worksheet |
||
457 | ############################### |
||
458 | |||
459 | parameters_data = report['parameters'] |
||
460 | |||
461 | parameters_names_len = len(parameters_data['names']) |
||
462 | |||
463 | parameters_ws = wb.create_sheet('相关参数') |
||
464 | |||
465 | parameters_timestamps_data_max_len = \ |
||
466 | get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
||
467 | |||
468 | # Row height |
||
469 | parameters_ws.row_dimensions[1].height = 102 |
||
470 | for i in range(2, 7 + 1): |
||
471 | parameters_ws.row_dimensions[i].height = 42 |
||
472 | |||
473 | for i in range(8, parameters_timestamps_data_max_len + 10): |
||
474 | parameters_ws.row_dimensions[i].height = 60 |
||
475 | |||
476 | # Col width |
||
477 | parameters_ws.column_dimensions['A'].width = 1.5 |
||
478 | |||
479 | parameters_ws.column_dimensions['B'].width = 25.0 |
||
480 | |||
481 | for i in range(3, 12 + parameters_names_len * 3): |
||
482 | parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
||
483 | |||
484 | # Img |
||
485 | img = Image("excelexporters/myems.png") |
||
486 | img.width = img.width * 0.85 |
||
487 | img.height = img.height * 0.85 |
||
488 | # img = Image("myems.png") |
||
489 | parameters_ws.add_image(img, 'B1') |
||
490 | |||
491 | # Title |
||
492 | parameters_ws.row_dimensions[3].height = 60 |
||
493 | |||
494 | parameters_ws['B3'].font = name_font |
||
495 | parameters_ws['B3'].alignment = b_r_alignment |
||
496 | parameters_ws['B3'] = 'Name:' |
||
497 | parameters_ws['C3'].border = b_border |
||
498 | parameters_ws['C3'].alignment = b_c_alignment |
||
499 | parameters_ws['C3'].font = name_font |
||
500 | parameters_ws['C3'] = name |
||
501 | |||
502 | parameters_ws['D3'].font = name_font |
||
503 | parameters_ws['D3'].alignment = b_r_alignment |
||
504 | parameters_ws['D3'] = 'Period:' |
||
505 | parameters_ws['E3'].border = b_border |
||
506 | parameters_ws['E3'].alignment = b_c_alignment |
||
507 | parameters_ws['E3'].font = name_font |
||
508 | parameters_ws['E3'] = period_type |
||
509 | |||
510 | parameters_ws['F3'].font = name_font |
||
511 | parameters_ws['F3'].alignment = b_r_alignment |
||
512 | parameters_ws['F3'] = 'Date:' |
||
513 | parameters_ws['G3'].border = b_border |
||
514 | parameters_ws['G3'].alignment = b_c_alignment |
||
515 | parameters_ws['G3'].font = name_font |
||
516 | parameters_ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
||
517 | parameters_ws.merge_cells("G3:H3") |
||
518 | |||
519 | parameters_ws_current_row_number = 6 |
||
520 | |||
521 | parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
||
522 | parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' 相关参数' |
||
523 | |||
524 | parameters_ws_current_row_number += 1 |
||
525 | |||
526 | parameters_table_start_row_number = parameters_ws_current_row_number |
||
527 | |||
528 | parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
||
529 | |||
530 | parameters_ws_current_row_number += 1 |
||
531 | |||
532 | table_current_col_number = 'B' |
||
533 | |||
534 | for i in range(0, parameters_names_len): |
||
535 | |||
536 | if len(parameters_data['timestamps'][i]) == 0: |
||
537 | continue |
||
538 | |||
539 | parameters_ws[table_current_col_number + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
540 | parameters_ws[table_current_col_number + str(parameters_ws_current_row_number - 1)].border = f_border |
||
541 | |||
542 | col = decimal_to_column(column_to_decimal(table_current_col_number) + 1) |
||
543 | |||
544 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
545 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
546 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = name_font |
||
547 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = c_c_alignment |
||
548 | parameters_ws[col + str(parameters_ws_current_row_number - 1)] = parameters_data['names'][i] |
||
549 | |||
550 | table_current_row_number = parameters_ws_current_row_number |
||
551 | |||
552 | for j, value in enumerate(list(parameters_data['timestamps'][i])): |
||
553 | col = table_current_col_number |
||
554 | |||
555 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
556 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
557 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
558 | parameters_ws[col + str(table_current_row_number)] = value |
||
559 | |||
560 | col = decimal_to_column(column_to_decimal(col) + 1) |
||
561 | |||
562 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
563 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
564 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
565 | parameters_ws[col + str(table_current_row_number)] = round(parameters_data['values'][i][j], 2) |
||
566 | |||
567 | table_current_row_number += 1 |
||
568 | |||
569 | table_current_col_number = decimal_to_column(column_to_decimal(table_current_col_number) + 3) |
||
570 | |||
571 | ######################################################## |
||
572 | # parameters chart and parameters table |
||
573 | ######################################################## |
||
574 | |||
575 | ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
||
576 | ws['B' + str(current_sheet_parameters_row_number)] = name + ' 相关参数' |
||
577 | |||
578 | current_sheet_parameters_row_number += 1 |
||
579 | |||
580 | chart_start_row_number = current_sheet_parameters_row_number |
||
581 | |||
582 | col_index = 0 |
||
583 | |||
584 | for i in range(0, parameters_names_len): |
||
585 | |||
586 | if len(parameters_data['timestamps'][i]) == 0: |
||
587 | continue |
||
588 | |||
589 | line = LineChart() |
||
590 | data_col = 3 + col_index * 3 |
||
591 | labels_col = 2 + col_index * 3 |
||
592 | col_index += 1 |
||
593 | line.title = '相关参数 - ' + \ |
||
594 | parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
||
595 | labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
||
596 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
597 | line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
||
598 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
599 | line.add_data(line_data, titles_from_data=True) |
||
600 | line.set_categories(labels) |
||
601 | line_data = line.series[0] |
||
602 | line_data.marker.symbol = "circle" |
||
603 | line_data.smooth = True |
||
604 | line.x_axis.crosses = 'min' |
||
605 | line.height = 8.25 |
||
606 | line.width = 24 |
||
607 | line.dLbls = DataLabelList() |
||
608 | line.dLbls.dLblPos = 't' |
||
609 | line.dLbls.showVal = False |
||
610 | line.dLbls.showPercent = False |
||
611 | chart_col = 'B' |
||
612 | chart_cell = chart_col + str(chart_start_row_number) |
||
613 | chart_start_row_number += 6 |
||
614 | ws.add_chart(line, chart_cell) |
||
615 | |||
616 | current_sheet_parameters_row_number = chart_start_row_number |
||
617 | |||
618 | current_sheet_parameters_row_number += 1 |
||
619 | |||
620 | filename = str(uuid.uuid4()) + '.xlsx' |
||
621 | wb.save(filename) |
||
622 | |||
623 | return filename |
||
624 |