Total Complexity | 56 |
Total Lines | 559 |
Duplicated Lines | 9.48 % |
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.metersubmetersbalance 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 | wb = Workbook() |
||
115 | ws = wb.active |
||
116 | |||
117 | # Row height |
||
118 | ws.row_dimensions[1].height = 102 |
||
119 | for i in range(2, 2000 + 1): |
||
120 | ws.row_dimensions[i].height = 42 |
||
121 | |||
122 | # Col width |
||
123 | ws.column_dimensions['A'].width = 1.5 |
||
124 | |||
125 | ws.column_dimensions['B'].width = 25.0 |
||
126 | |||
127 | for i in range(ord('C'), ord('L')): |
||
128 | ws.column_dimensions[chr(i)].width = 15.0 |
||
129 | |||
130 | # Font |
||
131 | name_font = Font(name='Constantia', size=15, bold=True) |
||
132 | title_font = Font(name='宋体', size=15, bold=True) |
||
133 | data_font = Font(name='Franklin Gothic Book', size=11) |
||
134 | |||
135 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
||
136 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
||
137 | right=Side(border_style='medium', color='00000000'), |
||
138 | bottom=Side(border_style='medium', color='00000000'), |
||
139 | top=Side(border_style='medium', color='00000000') |
||
140 | ) |
||
141 | b_border = Border( |
||
142 | bottom=Side(border_style='medium', color='00000000'), |
||
143 | ) |
||
144 | |||
145 | b_c_alignment = Alignment(vertical='bottom', |
||
146 | horizontal='center', |
||
147 | text_rotation=0, |
||
148 | wrap_text=True, |
||
149 | shrink_to_fit=False, |
||
150 | indent=0) |
||
151 | c_c_alignment = Alignment(vertical='center', |
||
152 | horizontal='center', |
||
153 | text_rotation=0, |
||
154 | wrap_text=True, |
||
155 | shrink_to_fit=False, |
||
156 | indent=0) |
||
157 | b_r_alignment = Alignment(vertical='bottom', |
||
158 | horizontal='right', |
||
159 | text_rotation=0, |
||
160 | wrap_text=True, |
||
161 | shrink_to_fit=False, |
||
162 | indent=0) |
||
163 | c_r_alignment = Alignment(vertical='bottom', |
||
164 | horizontal='center', |
||
165 | text_rotation=0, |
||
166 | wrap_text=True, |
||
167 | shrink_to_fit=False, |
||
168 | indent=0) |
||
169 | |||
170 | # Img |
||
171 | img = Image("excelexporters/myems.png") |
||
172 | img.width = img.width * 0.85 |
||
173 | img.height = img.height * 0.85 |
||
174 | # img = Image("myems.png") |
||
175 | ws.add_image(img, 'B1') |
||
176 | |||
177 | # Title |
||
178 | ws.row_dimensions[3].height = 60 |
||
179 | |||
180 | ws['B3'].font = name_font |
||
181 | ws['B3'].alignment = b_r_alignment |
||
182 | ws['B3'] = 'Name:' |
||
183 | ws['C3'].border = b_border |
||
184 | ws['C3'].alignment = b_c_alignment |
||
185 | ws['C3'].font = name_font |
||
186 | ws['C3'] = name |
||
187 | |||
188 | ws['D3'].font = name_font |
||
189 | ws['D3'].alignment = b_r_alignment |
||
190 | ws['D3'] = 'Period:' |
||
191 | ws['E3'].border = b_border |
||
192 | ws['E3'].alignment = b_c_alignment |
||
193 | ws['E3'].font = name_font |
||
194 | ws['E3'] = period_type |
||
195 | |||
196 | ws['F3'].font = name_font |
||
197 | ws['F3'].alignment = b_r_alignment |
||
198 | ws['F3'] = 'Date:' |
||
199 | ws['G3'].alignment = b_c_alignment |
||
200 | ws['G3'].font = name_font |
||
201 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
||
202 | ws.merge_cells("G3:H3") |
||
203 | |||
204 | if "reporting_period" not in report.keys() or \ |
||
205 | "difference_values" not in report['reporting_period'].keys() or \ |
||
206 | len(report['reporting_period']['difference_values']) == 0: |
||
207 | filename = str(uuid.uuid4()) + '.xlsx' |
||
208 | wb.save(filename) |
||
209 | |||
210 | return filename |
||
211 | ################################################# |
||
212 | |||
213 | has_difference_values_data_flag = True |
||
214 | if 'difference_values' not in report['reporting_period'].keys() or len( |
||
215 | report['reporting_period']['difference_values']) == 0: |
||
216 | has_difference_values_data_flag = False |
||
217 | |||
218 | current_row_number = 6 |
||
219 | |||
220 | if has_difference_values_data_flag: |
||
221 | reporting_period_data = report['reporting_period'] |
||
222 | category = report['meter']['energy_category_name'] |
||
223 | |||
224 | ws['B' + str(current_row_number)].font = title_font |
||
225 | ws['B' + str(current_row_number)] = name + ' 报告期' |
||
226 | |||
227 | current_row_number += 1 |
||
228 | |||
229 | ws.row_dimensions[current_row_number].height = 60 |
||
230 | |||
231 | ws['B' + str(current_row_number)].fill = table_fill |
||
232 | ws['B' + str(current_row_number)].border = f_border |
||
233 | if not isinstance(category, list): |
||
234 | ws['C' + str(current_row_number)].fill = table_fill |
||
235 | ws['C' + str(current_row_number)].font = name_font |
||
236 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
237 | ws['C' + str(current_row_number)].border = f_border |
||
238 | ws['C' + str(current_row_number)] = report['meter']['energy_category_name'] + " (" + report['meter'][ |
||
239 | 'unit_of_measure'] + ")" |
||
240 | |||
241 | current_row_number += 1 |
||
242 | |||
243 | ws['B' + str(current_row_number)].font = title_font |
||
244 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
245 | ws['B' + str(current_row_number)].border = f_border |
||
246 | ws['B' + str(current_row_number)] = '总表消耗' |
||
247 | |||
248 | ws['C' + str(current_row_number)].font = name_font |
||
249 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
250 | ws['C' + str(current_row_number)].border = f_border |
||
251 | ws['C' + str(current_row_number)] = round(reporting_period_data['master_meter_consumption_in_category'], 2) |
||
252 | |||
253 | current_row_number += 1 |
||
254 | |||
255 | ws['B' + str(current_row_number)].font = title_font |
||
256 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
257 | ws['B' + str(current_row_number)].border = f_border |
||
258 | ws['B' + str(current_row_number)] = '分表消耗' |
||
259 | |||
260 | ws['C' + str(current_row_number)].font = name_font |
||
261 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
262 | ws['C' + str(current_row_number)].border = f_border |
||
263 | ws['C' + str(current_row_number)] = round(reporting_period_data['submeters_consumption_in_category'], 2) |
||
264 | |||
265 | current_row_number += 1 |
||
266 | |||
267 | ws['B' + str(current_row_number)].font = title_font |
||
268 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
269 | ws['B' + str(current_row_number)].border = f_border |
||
270 | ws['B' + str(current_row_number)] = '差值' |
||
271 | |||
272 | ws['C' + str(current_row_number)].font = name_font |
||
273 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
274 | ws['C' + str(current_row_number)].border = f_border |
||
275 | ws['C' + str(current_row_number)] = round(reporting_period_data['difference_in_category'], 2) |
||
276 | |||
277 | current_row_number += 1 |
||
278 | |||
279 | ws['B' + str(current_row_number)].font = title_font |
||
280 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
281 | ws['B' + str(current_row_number)].border = f_border |
||
282 | ws['B' + str(current_row_number)] = '差值百分比' |
||
283 | |||
284 | ws['C' + str(current_row_number)].font = name_font |
||
285 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
286 | ws['C' + str(current_row_number)].border = f_border |
||
287 | ws['C' + str(current_row_number)] = str( |
||
288 | round(reporting_period_data['percentage_difference'] * 100, 2)) + '%' |
||
289 | |||
290 | current_row_number += 2 |
||
291 | |||
292 | time = reporting_period_data['timestamps'] |
||
293 | parameters_names_len = len(report['parameters']['names']) |
||
294 | has_time_data_flag = False |
||
295 | if time is not None and len(time) > 0: |
||
296 | has_time_data_flag = True |
||
297 | |||
298 | if has_time_data_flag: |
||
299 | |||
300 | ws['B' + str(current_row_number)].font = title_font |
||
301 | ws['B' + str(current_row_number)] = name + ' 详细数据' |
||
302 | |||
303 | current_row_number += 1 |
||
304 | chart_start_number = current_row_number |
||
305 | current_row_number = current_row_number + 7 + parameters_names_len * 6 |
||
306 | table_start_number = current_row_number |
||
307 | |||
308 | ws.row_dimensions[current_row_number].height = 60 |
||
309 | |||
310 | ws['B' + str(current_row_number)].fill = table_fill |
||
311 | ws['B' + str(current_row_number)].font = title_font |
||
312 | ws['B' + str(current_row_number)].border = f_border |
||
313 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
314 | ws['B' + str(current_row_number)] = '日期时间' |
||
315 | |||
316 | ws['C' + str(current_row_number)].fill = table_fill |
||
317 | ws['C' + str(current_row_number)].font = title_font |
||
318 | ws['C' + str(current_row_number)].border = f_border |
||
319 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
320 | ws['C' + str(current_row_number)] = report['meter']['energy_category_name'] + " (" + report['meter'][ |
||
321 | 'unit_of_measure'] + ")" |
||
322 | |||
323 | current_row_number += 1 |
||
324 | |||
325 | for i in range(0, len(time)): |
||
326 | ws['B' + str(current_row_number)].font = title_font |
||
327 | ws['B' + str(current_row_number)].border = f_border |
||
328 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
329 | ws['B' + str(current_row_number)] = time[i] |
||
330 | |||
331 | ws['C' + str(current_row_number)].font = title_font |
||
332 | ws['C' + str(current_row_number)].border = f_border |
||
333 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
334 | ws['C' + str(current_row_number)] = round(reporting_period_data['difference_values'][i], 2) |
||
335 | |||
336 | current_row_number += 1 |
||
337 | |||
338 | table_end_number = current_row_number - 1 |
||
339 | |||
340 | line = LineChart() |
||
341 | line.title = '报告期差值 - ' + report['meter']['energy_category_name'] + " (" + report['meter'][ |
||
342 | 'unit_of_measure'] + ")" |
||
343 | labels = Reference(ws, min_col=2, min_row=table_start_number + 1, max_row=table_end_number) |
||
344 | line_data = Reference(ws, min_col=3, min_row=table_start_number, max_row=table_end_number) |
||
345 | line.add_data(line_data, titles_from_data=True) |
||
346 | line.set_categories(labels) |
||
347 | line_data = line.series[0] |
||
348 | line_data.marker.symbol = "circle" |
||
349 | line_data.smooth = True |
||
350 | line.x_axis.crosses = 'min' |
||
351 | line.height = 8.25 |
||
352 | line.width = 24 |
||
353 | line.dLbls = DataLabelList() |
||
354 | line.dLbls.dLblPos = 't' |
||
355 | line.dLbls.showVal = True # 数量显示 |
||
356 | ws.add_chart(line, "B" + str(chart_start_number)) |
||
357 | |||
358 | ws['B' + str(current_row_number)].font = title_font |
||
359 | ws['B' + str(current_row_number)].border = f_border |
||
360 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
361 | ws['B' + str(current_row_number)] = '总计' |
||
362 | |||
363 | ws['C' + str(current_row_number)].font = title_font |
||
364 | ws['C' + str(current_row_number)].border = f_border |
||
365 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
366 | ws['C' + str(current_row_number)] = round(reporting_period_data['master_meter_consumption_in_category'] |
||
367 | , 2) |
||
368 | |||
369 | else: |
||
370 | pass |
||
371 | |||
372 | ########################################## |
||
373 | has_parameters_names_and_timestamps_and_values_data = True |
||
374 | # 12 is the starting line number of the last line chart in the report period |
||
375 | category = report['meter']['energy_category_name'] |
||
376 | ca_len = len(category) |
||
377 | time_len = len(report['reporting_period']['timestamps']) |
||
378 | current_sheet_parameters_row_number = 14 + ca_len * 6 |
||
379 | if 'parameters' not in report.keys() or \ |
||
380 | report['parameters'] is None or \ |
||
381 | 'names' not in report['parameters'].keys() or \ |
||
382 | report['parameters']['names'] is None or \ |
||
383 | len(report['parameters']['names']) == 0 or \ |
||
384 | 'timestamps' not in report['parameters'].keys() or \ |
||
385 | report['parameters']['timestamps'] is None or \ |
||
386 | len(report['parameters']['timestamps']) == 0 or \ |
||
387 | 'values' not in report['parameters'].keys() or \ |
||
388 | report['parameters']['values'] is None or \ |
||
389 | len(report['parameters']['values']) == 0 or \ |
||
390 | timestamps_data_all_equal_0(report['parameters']['timestamps']): |
||
391 | has_parameters_names_and_timestamps_and_values_data = False |
||
392 | if has_parameters_names_and_timestamps_and_values_data: |
||
393 | |||
394 | ############################### |
||
395 | # new worksheet |
||
396 | ############################### |
||
397 | |||
398 | parameters_data = report['parameters'] |
||
399 | |||
400 | parameters_names_len = len(parameters_data['names']) |
||
401 | |||
402 | parameters_ws = wb.create_sheet('相关参数') |
||
403 | |||
404 | parameters_timestamps_data_max_len = \ |
||
405 | get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
||
406 | |||
407 | # Row height |
||
408 | parameters_ws.row_dimensions[1].height = 102 |
||
409 | for i in range(2, 7 + 1): |
||
410 | parameters_ws.row_dimensions[i].height = 42 |
||
411 | |||
412 | for i in range(8, parameters_timestamps_data_max_len + 10): |
||
413 | parameters_ws.row_dimensions[i].height = 60 |
||
414 | |||
415 | # Col width |
||
416 | parameters_ws.column_dimensions['A'].width = 1.5 |
||
417 | |||
418 | parameters_ws.column_dimensions['B'].width = 25.0 |
||
419 | |||
420 | for i in range(3, 12 + parameters_names_len * 3): |
||
421 | parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
||
422 | |||
423 | # Img |
||
424 | img = Image("excelexporters/myems.png") |
||
425 | img.width = img.width * 0.85 |
||
426 | img.height = img.height * 0.85 |
||
427 | # img = Image("myems.png") |
||
428 | parameters_ws.add_image(img, 'B1') |
||
429 | |||
430 | # Title |
||
431 | parameters_ws.row_dimensions[3].height = 60 |
||
432 | |||
433 | parameters_ws['B3'].font = name_font |
||
434 | parameters_ws['B3'].alignment = b_r_alignment |
||
435 | parameters_ws['B3'] = 'Name:' |
||
436 | parameters_ws['C3'].border = b_border |
||
437 | parameters_ws['C3'].alignment = b_c_alignment |
||
438 | parameters_ws['C3'].font = name_font |
||
439 | parameters_ws['C3'] = name |
||
440 | |||
441 | parameters_ws['D3'].font = name_font |
||
442 | parameters_ws['D3'].alignment = b_r_alignment |
||
443 | parameters_ws['D3'] = 'Period:' |
||
444 | parameters_ws['E3'].border = b_border |
||
445 | parameters_ws['E3'].alignment = b_c_alignment |
||
446 | parameters_ws['E3'].font = name_font |
||
447 | parameters_ws['E3'] = period_type |
||
448 | |||
449 | parameters_ws['F3'].font = name_font |
||
450 | parameters_ws['F3'].alignment = b_r_alignment |
||
451 | parameters_ws['F3'] = 'Date:' |
||
452 | parameters_ws['G3'].border = b_border |
||
453 | parameters_ws['G3'].alignment = b_c_alignment |
||
454 | parameters_ws['G3'].font = name_font |
||
455 | parameters_ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
||
456 | parameters_ws.merge_cells("G3:H3") |
||
457 | |||
458 | parameters_ws_current_row_number = 6 |
||
459 | |||
460 | parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
||
461 | parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' 相关参数' |
||
462 | |||
463 | parameters_ws_current_row_number += 1 |
||
464 | |||
465 | parameters_table_start_row_number = parameters_ws_current_row_number |
||
466 | |||
467 | parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
||
468 | |||
469 | parameters_ws_current_row_number += 1 |
||
470 | |||
471 | table_current_col_number = 'B' |
||
472 | |||
473 | for i in range(0, parameters_names_len): |
||
474 | |||
475 | if len(parameters_data['timestamps'][i]) == 0: |
||
476 | continue |
||
477 | |||
478 | parameters_ws[table_current_col_number + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
479 | parameters_ws[table_current_col_number + str(parameters_ws_current_row_number - 1)].border = f_border |
||
480 | |||
481 | col = decimal_to_column(column_to_decimal(table_current_col_number) + 1) |
||
482 | |||
483 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
484 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
485 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = name_font |
||
486 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = c_c_alignment |
||
487 | parameters_ws[col + str(parameters_ws_current_row_number - 1)] = parameters_data['names'][i] |
||
488 | |||
489 | table_current_row_number = parameters_ws_current_row_number |
||
490 | |||
491 | for j, value in enumerate(list(parameters_data['timestamps'][i])): |
||
492 | col = table_current_col_number |
||
493 | |||
494 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
495 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
496 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
497 | parameters_ws[col + str(table_current_row_number)] = value |
||
498 | |||
499 | col = decimal_to_column(column_to_decimal(col) + 1) |
||
500 | |||
501 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
502 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
503 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
504 | parameters_ws[col + str(table_current_row_number)] = round(parameters_data['values'][i][j], 2) |
||
505 | |||
506 | table_current_row_number += 1 |
||
507 | |||
508 | table_current_col_number = decimal_to_column(column_to_decimal(table_current_col_number) + 3) |
||
509 | |||
510 | ######################################################## |
||
511 | # parameters chart and parameters table |
||
512 | ######################################################## |
||
513 | |||
514 | ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
||
515 | ws['B' + str(current_sheet_parameters_row_number)] = name + ' 相关参数' |
||
516 | |||
517 | current_sheet_parameters_row_number += 1 |
||
518 | |||
519 | chart_start_row_number = current_sheet_parameters_row_number |
||
520 | |||
521 | col_index = 0 |
||
522 | |||
523 | for i in range(0, parameters_names_len): |
||
524 | |||
525 | if len(parameters_data['timestamps'][i]) == 0: |
||
526 | continue |
||
527 | |||
528 | line = LineChart() |
||
529 | data_col = 3 + col_index * 3 |
||
530 | labels_col = 2 + col_index * 3 |
||
531 | col_index += 1 |
||
532 | line.title = '相关参数 - ' + \ |
||
533 | parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
||
534 | labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
||
535 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
536 | line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
||
537 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
538 | line.add_data(line_data, titles_from_data=True) |
||
539 | line.set_categories(labels) |
||
540 | line_data = line.series[0] |
||
541 | line_data.marker.symbol = "circle" |
||
542 | line_data.smooth = True |
||
543 | line.x_axis.crosses = 'min' |
||
544 | line.height = 8.25 |
||
545 | line.width = 24 |
||
546 | line.dLbls = DataLabelList() |
||
547 | line.dLbls.dLblPos = 't' |
||
548 | line.dLbls.showVal = False |
||
549 | line.dLbls.showPercent = False |
||
550 | chart_col = 'B' |
||
551 | chart_cell = chart_col + str(chart_start_row_number) |
||
552 | chart_start_row_number += 6 |
||
553 | ws.add_chart(line, chart_cell) |
||
554 | |||
555 | filename = str(uuid.uuid4()) + '.xlsx' |
||
556 | wb.save(filename) |
||
557 | |||
558 | return filename |
||
559 |