| Total Complexity | 73 |
| Total Lines | 608 |
| Duplicated Lines | 6.41 % |
| 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.spaceefficiency 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 | PieChart, |
||
| 6 | LineChart, |
||
| 7 | BarChart, |
||
| 8 | Reference, |
||
| 9 | ) |
||
| 10 | from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
||
| 11 | from openpyxl.drawing.image import Image |
||
| 12 | from openpyxl import Workbook |
||
| 13 | from openpyxl.chart.label import DataLabelList |
||
| 14 | import openpyxl.utils.cell as format_cell |
||
| 15 | |||
| 16 | |||
| 17 | #################################################################################################################### |
||
| 18 | # PROCEDURES |
||
| 19 | # Step 1: Validate the report data |
||
| 20 | # Step 2: Generate excel file |
||
| 21 | # Step 3: Encode the excel file bytes to Base64 |
||
| 22 | #################################################################################################################### |
||
| 23 | |||
| 24 | |||
| 25 | View Code Duplication | def export(report, |
|
|
|
|||
| 26 | name, |
||
| 27 | reporting_start_datetime_local, |
||
| 28 | reporting_end_datetime_local, |
||
| 29 | period_type): |
||
| 30 | #################################################################################################################### |
||
| 31 | # Step 1: Validate the report data |
||
| 32 | #################################################################################################################### |
||
| 33 | if report is None: |
||
| 34 | return None |
||
| 35 | print(report) |
||
| 36 | |||
| 37 | #################################################################################################################### |
||
| 38 | # Step 2: Generate excel file from the report data |
||
| 39 | #################################################################################################################### |
||
| 40 | filename = generate_excel(report, |
||
| 41 | name, |
||
| 42 | reporting_start_datetime_local, |
||
| 43 | reporting_end_datetime_local, |
||
| 44 | period_type) |
||
| 45 | #################################################################################################################### |
||
| 46 | # Step 3: Encode the excel file to Base64 |
||
| 47 | #################################################################################################################### |
||
| 48 | try: |
||
| 49 | with open(filename, 'rb') as binary_file: |
||
| 50 | binary_file_data = binary_file.read() |
||
| 51 | except IOError as ex: |
||
| 52 | pass |
||
| 53 | |||
| 54 | # Base64 encode the bytes |
||
| 55 | base64_encoded_data = base64.b64encode(binary_file_data) |
||
| 56 | # get the Base64 encoded data using human-readable characters. |
||
| 57 | base64_message = base64_encoded_data.decode('utf-8') |
||
| 58 | # delete the file from server |
||
| 59 | try: |
||
| 60 | os.remove(filename) |
||
| 61 | except NotImplementedError as ex: |
||
| 62 | pass |
||
| 63 | return base64_message |
||
| 64 | |||
| 65 | |||
| 66 | def generate_excel(report, |
||
| 67 | name, |
||
| 68 | reporting_start_datetime_local, |
||
| 69 | reporting_end_datetime_local, |
||
| 70 | period_type): |
||
| 71 | wb = Workbook() |
||
| 72 | ws = wb.active |
||
| 73 | |||
| 74 | # Row height |
||
| 75 | ws.row_dimensions[1].height = 102 |
||
| 76 | for i in range(2, 2000 + 1): |
||
| 77 | ws.row_dimensions[i].height = 42 |
||
| 78 | |||
| 79 | # Col width |
||
| 80 | ws.column_dimensions['A'].width = 1.5 |
||
| 81 | |||
| 82 | ws.column_dimensions['B'].width = 25.0 |
||
| 83 | |||
| 84 | for i in range(ord('C'), ord('L')): |
||
| 85 | ws.column_dimensions[chr(i)].width = 15.0 |
||
| 86 | |||
| 87 | # Font |
||
| 88 | name_font = Font(name='Constantia', size=15, bold=True) |
||
| 89 | title_font = Font(name='宋体', size=15, bold=True) |
||
| 90 | data_font = Font(name='Franklin Gothic Book', size=11) |
||
| 91 | |||
| 92 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
||
| 93 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
||
| 94 | right=Side(border_style='medium', color='00000000'), |
||
| 95 | bottom=Side(border_style='medium', color='00000000'), |
||
| 96 | top=Side(border_style='medium', color='00000000') |
||
| 97 | ) |
||
| 98 | b_border = Border( |
||
| 99 | bottom=Side(border_style='medium', color='00000000'), |
||
| 100 | ) |
||
| 101 | |||
| 102 | b_c_alignment = Alignment(vertical='bottom', |
||
| 103 | horizontal='center', |
||
| 104 | text_rotation=0, |
||
| 105 | wrap_text=True, |
||
| 106 | shrink_to_fit=False, |
||
| 107 | indent=0) |
||
| 108 | c_c_alignment = Alignment(vertical='center', |
||
| 109 | horizontal='center', |
||
| 110 | text_rotation=0, |
||
| 111 | wrap_text=True, |
||
| 112 | shrink_to_fit=False, |
||
| 113 | indent=0) |
||
| 114 | b_r_alignment = Alignment(vertical='bottom', |
||
| 115 | horizontal='right', |
||
| 116 | text_rotation=0, |
||
| 117 | wrap_text=True, |
||
| 118 | shrink_to_fit=False, |
||
| 119 | indent=0) |
||
| 120 | c_r_alignment = Alignment(vertical='bottom', |
||
| 121 | horizontal='center', |
||
| 122 | text_rotation=0, |
||
| 123 | wrap_text=True, |
||
| 124 | shrink_to_fit=False, |
||
| 125 | indent=0) |
||
| 126 | # Img |
||
| 127 | img = Image("excelexporters/myems.png") |
||
| 128 | img.width = img.width * 0.85 |
||
| 129 | img.height = img.height * 0.85 |
||
| 130 | # img = Image("myems.png") |
||
| 131 | ws.add_image(img, 'B1') |
||
| 132 | |||
| 133 | # Title |
||
| 134 | ws.row_dimensions[3].height = 60 |
||
| 135 | |||
| 136 | ws['B3'].font = name_font |
||
| 137 | ws['B3'].alignment = b_r_alignment |
||
| 138 | ws['B3'] = 'Name:' |
||
| 139 | ws['C3'].border = b_border |
||
| 140 | ws['C3'].alignment = b_c_alignment |
||
| 141 | ws['C3'].font = name_font |
||
| 142 | ws['C3'] = name |
||
| 143 | |||
| 144 | ws['D3'].font = name_font |
||
| 145 | ws['D3'].alignment = b_r_alignment |
||
| 146 | ws['D3'] = 'Period:' |
||
| 147 | ws['E3'].border = b_border |
||
| 148 | ws['E3'].alignment = b_c_alignment |
||
| 149 | ws['E3'].font = name_font |
||
| 150 | ws['E3'] = period_type |
||
| 151 | |||
| 152 | ws['F3'].font = name_font |
||
| 153 | ws['F3'].alignment = b_r_alignment |
||
| 154 | ws['F3'] = 'Date:' |
||
| 155 | ws['G3'].border = b_border |
||
| 156 | ws['G3'].alignment = b_c_alignment |
||
| 157 | ws['G3'].font = name_font |
||
| 158 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
||
| 159 | ws.merge_cells("G3:H3") |
||
| 160 | |||
| 161 | if "reporting_period_efficiency" not in report.keys() or \ |
||
| 162 | "names" not in report['reporting_period_efficiency'].keys() or len( |
||
| 163 | report['reporting_period_efficiency']['names']) == 0: |
||
| 164 | filename = str(uuid.uuid4()) + '.xlsx' |
||
| 165 | wb.save(filename) |
||
| 166 | |||
| 167 | return filename |
||
| 168 | |||
| 169 | ################################## |
||
| 170 | |||
| 171 | current_row_number = 6 |
||
| 172 | |||
| 173 | reporting_period_data = report['reporting_period_efficiency'] |
||
| 174 | |||
| 175 | has_names_data_flag = True |
||
| 176 | |||
| 177 | if "names" not in reporting_period_data.keys() or \ |
||
| 178 | reporting_period_data['names'] is None or \ |
||
| 179 | len(reporting_period_data['names']) == 0: |
||
| 180 | has_names_data_flag = False |
||
| 181 | |||
| 182 | if has_names_data_flag: |
||
| 183 | ws['B' + str(current_row_number)].font = title_font |
||
| 184 | ws['B' + str(current_row_number)] = name + ' 报告期累积效率' |
||
| 185 | |||
| 186 | current_row_number += 1 |
||
| 187 | |||
| 188 | category = reporting_period_data['names'] |
||
| 189 | ca_len = len(category) |
||
| 190 | |||
| 191 | ws.row_dimensions[current_row_number].height = 80 |
||
| 192 | ws['B' + str(current_row_number)].fill = table_fill |
||
| 193 | ws['B' + str(current_row_number)].border = f_border |
||
| 194 | |||
| 195 | col = 'C' |
||
| 196 | |||
| 197 | for i in range(0, ca_len): |
||
| 198 | ws[col + str(current_row_number)].fill = table_fill |
||
| 199 | ws[col + str(current_row_number)].font = name_font |
||
| 200 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 201 | ws[col + str(current_row_number)].border = f_border |
||
| 202 | ws[col + str(current_row_number)] = \ |
||
| 203 | reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
| 204 | |||
| 205 | col = chr(ord(col) + 1) |
||
| 206 | |||
| 207 | current_row_number += 1 |
||
| 208 | |||
| 209 | ws['B' + str(current_row_number)].font = title_font |
||
| 210 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
| 211 | ws['B' + str(current_row_number)].border = f_border |
||
| 212 | ws['B' + str(current_row_number)] = '累积效率' |
||
| 213 | |||
| 214 | col = 'C' |
||
| 215 | |||
| 216 | for i in range(0, ca_len): |
||
| 217 | ws[col + str(current_row_number)].font = name_font |
||
| 218 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 219 | ws[col + str(current_row_number)].border = f_border |
||
| 220 | ws[col + str(current_row_number)] = round(reporting_period_data['cumulations'][i], 2) \ |
||
| 221 | if reporting_period_data['cumulations'][i] is not None else 0 |
||
| 222 | |||
| 223 | col = chr(ord(col) + 1) |
||
| 224 | |||
| 225 | current_row_number += 1 |
||
| 226 | |||
| 227 | ws['B' + str(current_row_number)].font = title_font |
||
| 228 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
| 229 | ws['B' + str(current_row_number)].border = f_border |
||
| 230 | ws['B' + str(current_row_number)] = '环比' |
||
| 231 | |||
| 232 | col = 'C' |
||
| 233 | |||
| 234 | for i in range(0, ca_len): |
||
| 235 | ws[col + str(current_row_number)].font = name_font |
||
| 236 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 237 | ws[col + str(current_row_number)].border = f_border |
||
| 238 | ws[col + str(current_row_number)] = str( |
||
| 239 | round(reporting_period_data['increment_rates'][i] * 100, 2)) + '%' \ |
||
| 240 | if reporting_period_data['increment_rates'][i] is not None else '-' |
||
| 241 | |||
| 242 | col = chr(ord(col) + 1) |
||
| 243 | |||
| 244 | current_row_number += 2 |
||
| 245 | |||
| 246 | ##################################### |
||
| 247 | |||
| 248 | has_parameters_names_and_timestamps_and_values_data = True |
||
| 249 | current_sheet_parameters_row_number = current_row_number |
||
| 250 | |||
| 251 | if 'parameters' not in report.keys() or \ |
||
| 252 | report['parameters'] is None or \ |
||
| 253 | 'names' not in report['parameters'].keys() or \ |
||
| 254 | report['parameters']['names'] is None or \ |
||
| 255 | len(report['parameters']['names']) == 0 or \ |
||
| 256 | 'timestamps' not in report['parameters'].keys() or \ |
||
| 257 | report['parameters']['timestamps'] is None or \ |
||
| 258 | len(report['parameters']['timestamps']) == 0 or \ |
||
| 259 | 'values' not in report['parameters'].keys() or \ |
||
| 260 | report['parameters']['values'] is None or \ |
||
| 261 | len(report['parameters']['values']) == 0 or \ |
||
| 262 | timestamps_data_all_equal_0(report['parameters']['timestamps']): |
||
| 263 | |||
| 264 | has_parameters_names_and_timestamps_and_values_data = False |
||
| 265 | |||
| 266 | ##################################### |
||
| 267 | |||
| 268 | has_values_data = True |
||
| 269 | has_timestamps_data = True |
||
| 270 | |||
| 271 | if 'values' not in reporting_period_data.keys() or \ |
||
| 272 | reporting_period_data['values'] is None or \ |
||
| 273 | len(reporting_period_data['values']) == 0: |
||
| 274 | has_values_data = False |
||
| 275 | |||
| 276 | if 'timestamps' not in reporting_period_data.keys() or \ |
||
| 277 | reporting_period_data['timestamps'] is None or \ |
||
| 278 | len(reporting_period_data['timestamps']) == 0 or \ |
||
| 279 | len(reporting_period_data['timestamps'][0]) == 0: |
||
| 280 | has_timestamps_data = False |
||
| 281 | |||
| 282 | if has_values_data and has_timestamps_data: |
||
| 283 | ca_len = len(reporting_period_data['names']) |
||
| 284 | time = reporting_period_data['timestamps'][0] |
||
| 285 | |||
| 286 | ws['B' + str(current_row_number)].font = title_font |
||
| 287 | ws['B' + str(current_row_number)] = name + ' 报告期累积效率' |
||
| 288 | |||
| 289 | current_row_number += 1 |
||
| 290 | |||
| 291 | chart_start_row_number = current_row_number |
||
| 292 | |||
| 293 | current_row_number += ca_len * 6 + 1 |
||
| 294 | |||
| 295 | if has_parameters_names_and_timestamps_and_values_data: |
||
| 296 | current_sheet_parameters_row_number = current_row_number |
||
| 297 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
||
| 298 | current_row_number += 6*real_timestamps_len + 2 |
||
| 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 | |||
| 305 | table_start_row_number = current_row_number |
||
| 306 | |||
| 307 | ws.row_dimensions[current_row_number].height = 85 |
||
| 308 | ws['B' + str(current_row_number)].fill = table_fill |
||
| 309 | ws['B' + str(current_row_number)].font = title_font |
||
| 310 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
| 311 | ws['B' + str(current_row_number)].border = f_border |
||
| 312 | ws['B' + str(current_row_number)] = '日期时间' |
||
| 313 | |||
| 314 | col = 'C' |
||
| 315 | |||
| 316 | for i in range(0, ca_len): |
||
| 317 | ws[col + str(current_row_number)].fill = table_fill |
||
| 318 | ws[col + str(current_row_number)].font = title_font |
||
| 319 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 320 | ws[col + str(current_row_number)].border = f_border |
||
| 321 | ws[col + str(current_row_number)] = \ |
||
| 322 | reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
| 323 | col = chr(ord(col) + 1) |
||
| 324 | |||
| 325 | current_row_number += 1 |
||
| 326 | |||
| 327 | for i in range(0, len(time)): |
||
| 328 | ws['B' + str(current_row_number)].font = title_font |
||
| 329 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
| 330 | ws['B' + str(current_row_number)].border = f_border |
||
| 331 | ws['B' + str(current_row_number)] = time[i] |
||
| 332 | |||
| 333 | col = 'C' |
||
| 334 | for j in range(0, ca_len): |
||
| 335 | ws[col + str(current_row_number)].font = title_font |
||
| 336 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 337 | ws[col + str(current_row_number)].border = f_border |
||
| 338 | ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \ |
||
| 339 | if reporting_period_data['values'][j][i] is not None else 0.00 |
||
| 340 | col = chr(ord(col) + 1) |
||
| 341 | |||
| 342 | current_row_number += 1 |
||
| 343 | |||
| 344 | table_end_row_number = current_row_number - 1 |
||
| 345 | |||
| 346 | ws['B' + str(current_row_number)].font = title_font |
||
| 347 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
| 348 | ws['B' + str(current_row_number)].border = f_border |
||
| 349 | ws['B' + str(current_row_number)] = '小计' |
||
| 350 | |||
| 351 | col = 'C' |
||
| 352 | |||
| 353 | for i in range(0, ca_len): |
||
| 354 | ws[col + str(current_row_number)].font = title_font |
||
| 355 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 356 | ws[col + str(current_row_number)].border = f_border |
||
| 357 | ws[col + str(current_row_number)] = round(reporting_period_data['cumulations'][i], 2) \ |
||
| 358 | if reporting_period_data['cumulations'][i] is not None else 0.00 |
||
| 359 | col = chr(ord(col) + 1) |
||
| 360 | |||
| 361 | current_row_number += 2 |
||
| 362 | |||
| 363 | format_time_width_number = 1.0 |
||
| 364 | min_len_number = 1.0 |
||
| 365 | min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0 |
||
| 366 | |||
| 367 | if period_type == 'hourly': |
||
| 368 | format_time_width_number = 4.0 |
||
| 369 | min_len_number = 2 |
||
| 370 | min_width_number = 12.0 |
||
| 371 | elif period_type == 'daily': |
||
| 372 | format_time_width_number = 2.5 |
||
| 373 | min_len_number = 4 |
||
| 374 | min_width_number = 14.0 |
||
| 375 | elif period_type == 'monthly': |
||
| 376 | format_time_width_number = 2.1 |
||
| 377 | min_len_number = 4 |
||
| 378 | min_width_number = 12.4 |
||
| 379 | elif period_type == 'yearly': |
||
| 380 | format_time_width_number = 1.5 |
||
| 381 | min_len_number = 5 |
||
| 382 | min_width_number = 11.5 |
||
| 383 | |||
| 384 | for i in range(0, ca_len): |
||
| 385 | line = LineChart() |
||
| 386 | line.title = '报告期累积效率 - ' + \ |
||
| 387 | reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
| 388 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
| 389 | line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number) |
||
| 390 | line.add_data(line_data, titles_from_data=True) |
||
| 391 | line.set_categories(labels) |
||
| 392 | line_data = line.series[0] |
||
| 393 | line_data.marker.symbol = "circle" |
||
| 394 | line_data.smooth = True |
||
| 395 | line.x_axis.crosses = 'min' |
||
| 396 | line.height = 8.25 |
||
| 397 | line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number |
||
| 398 | if line.width > 24: |
||
| 399 | line.width = 24 |
||
| 400 | line.dLbls = DataLabelList() |
||
| 401 | line.dLbls.dLblPos = 't' |
||
| 402 | line.dLbls.showVal = True |
||
| 403 | line.dLbls.showPercent = False |
||
| 404 | chart_col = 'B' |
||
| 405 | chart_cell = chart_col + str(chart_start_row_number) |
||
| 406 | chart_start_row_number += 6 |
||
| 407 | ws.add_chart(line, chart_cell) |
||
| 408 | |||
| 409 | ##################################################################### |
||
| 410 | |||
| 411 | if has_parameters_names_and_timestamps_and_values_data: |
||
| 412 | |||
| 413 | ############################### |
||
| 414 | # new worksheet |
||
| 415 | ############################### |
||
| 416 | |||
| 417 | parameters_data = report['parameters'] |
||
| 418 | parameters_names_len = len(parameters_data['names']) |
||
| 419 | |||
| 420 | parameters_ws = wb.create_sheet('相关参数') |
||
| 421 | |||
| 422 | parameters_timestamps_data_max_len = \ |
||
| 423 | get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
||
| 424 | |||
| 425 | # Row height |
||
| 426 | parameters_ws.row_dimensions[1].height = 102 |
||
| 427 | for i in range(2, 7 + 1): |
||
| 428 | parameters_ws.row_dimensions[i].height = 42 |
||
| 429 | |||
| 430 | for i in range(8, parameters_timestamps_data_max_len + 10): |
||
| 431 | parameters_ws.row_dimensions[i].height = 60 |
||
| 432 | |||
| 433 | # Col width |
||
| 434 | parameters_ws.column_dimensions['A'].width = 1.5 |
||
| 435 | |||
| 436 | parameters_ws.column_dimensions['B'].width = 25.0 |
||
| 437 | |||
| 438 | for i in range(3, 12+parameters_names_len*3): |
||
| 439 | parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
||
| 440 | |||
| 441 | # Img |
||
| 442 | img = Image("excelexporters/myems.png") |
||
| 443 | img.width = img.width * 0.85 |
||
| 444 | img.height = img.height * 0.85 |
||
| 445 | # img = Image("myems.png") |
||
| 446 | parameters_ws.add_image(img, 'B1') |
||
| 447 | |||
| 448 | # Title |
||
| 449 | parameters_ws.row_dimensions[3].height = 60 |
||
| 450 | |||
| 451 | parameters_ws['B3'].font = name_font |
||
| 452 | parameters_ws['B3'].alignment = b_r_alignment |
||
| 453 | parameters_ws['B3'] = 'Name:' |
||
| 454 | parameters_ws['C3'].border = b_border |
||
| 455 | parameters_ws['C3'].alignment = b_c_alignment |
||
| 456 | parameters_ws['C3'].font = name_font |
||
| 457 | parameters_ws['C3'] = name |
||
| 458 | |||
| 459 | parameters_ws['D3'].font = name_font |
||
| 460 | parameters_ws['D3'].alignment = b_r_alignment |
||
| 461 | parameters_ws['D3'] = 'Period:' |
||
| 462 | parameters_ws['E3'].border = b_border |
||
| 463 | parameters_ws['E3'].alignment = b_c_alignment |
||
| 464 | parameters_ws['E3'].font = name_font |
||
| 465 | parameters_ws['E3'] = period_type |
||
| 466 | |||
| 467 | parameters_ws['F3'].font = name_font |
||
| 468 | parameters_ws['F3'].alignment = b_r_alignment |
||
| 469 | parameters_ws['F3'] = 'Date:' |
||
| 470 | parameters_ws['G3'].border = b_border |
||
| 471 | parameters_ws['G3'].alignment = b_c_alignment |
||
| 472 | parameters_ws['G3'].font = name_font |
||
| 473 | parameters_ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
||
| 474 | parameters_ws.merge_cells("G3:H3") |
||
| 475 | |||
| 476 | parameters_ws_current_row_number = 6 |
||
| 477 | |||
| 478 | parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
||
| 479 | parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' 相关参数' |
||
| 480 | |||
| 481 | parameters_ws_current_row_number += 1 |
||
| 482 | |||
| 483 | parameters_table_start_row_number = parameters_ws_current_row_number |
||
| 484 | |||
| 485 | parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
||
| 486 | |||
| 487 | parameters_ws_current_row_number += 1 |
||
| 488 | |||
| 489 | table_current_col_number = 2 |
||
| 490 | |||
| 491 | for i in range(0, parameters_names_len): |
||
| 492 | |||
| 493 | if len(parameters_data['timestamps'][i]) == 0: |
||
| 494 | continue |
||
| 495 | |||
| 496 | col = format_cell.get_column_letter(table_current_col_number) |
||
| 497 | |||
| 498 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
| 499 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
| 500 | |||
| 501 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
| 502 | |||
| 503 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
| 504 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
| 505 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = name_font |
||
| 506 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = c_c_alignment |
||
| 507 | parameters_ws[col + str(parameters_ws_current_row_number - 1)] = parameters_data['names'][i] |
||
| 508 | |||
| 509 | table_current_row_number = parameters_ws_current_row_number |
||
| 510 | |||
| 511 | for j, value in enumerate(list(parameters_data['timestamps'][i])): |
||
| 512 | col = format_cell.get_column_letter(table_current_col_number) |
||
| 513 | |||
| 514 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
| 515 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
| 516 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
| 517 | parameters_ws[col + str(table_current_row_number)] = value |
||
| 518 | |||
| 519 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
| 520 | |||
| 521 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
| 522 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
| 523 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
| 524 | parameters_ws[col + str(table_current_row_number)] = round(parameters_data['values'][i][j], 2) |
||
| 525 | |||
| 526 | table_current_row_number += 1 |
||
| 527 | |||
| 528 | table_current_col_number = table_current_col_number + 3 |
||
| 529 | |||
| 530 | ######################################################## |
||
| 531 | # parameters chart and parameters table |
||
| 532 | ######################################################## |
||
| 533 | |||
| 534 | ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
||
| 535 | ws['B' + str(current_sheet_parameters_row_number)] = name + ' 相关参数' |
||
| 536 | |||
| 537 | current_sheet_parameters_row_number += 1 |
||
| 538 | |||
| 539 | chart_start_row_number = current_sheet_parameters_row_number |
||
| 540 | |||
| 541 | col_index = 0 |
||
| 542 | |||
| 543 | for i in range(0, parameters_names_len): |
||
| 544 | |||
| 545 | if len(parameters_data['timestamps'][i]) == 0: |
||
| 546 | continue |
||
| 547 | |||
| 548 | line = LineChart() |
||
| 549 | data_col = 3+col_index*3 |
||
| 550 | labels_col = 2+col_index*3 |
||
| 551 | col_index += 1 |
||
| 552 | line.title = '相关参数 - ' + \ |
||
| 553 | parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
||
| 554 | labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
||
| 555 | max_row=(len(parameters_data['timestamps'][i])+parameters_table_start_row_number)) |
||
| 556 | line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
||
| 557 | max_row=(len(parameters_data['timestamps'][i])+parameters_table_start_row_number)) |
||
| 558 | line.add_data(line_data, titles_from_data=True) |
||
| 559 | line.set_categories(labels) |
||
| 560 | line_data = line.series[0] |
||
| 561 | line_data.marker.symbol = "circle" |
||
| 562 | line_data.smooth = True |
||
| 563 | line.x_axis.crosses = 'min' |
||
| 564 | line.height = 8.25 |
||
| 565 | line.width = 24 |
||
| 566 | line.dLbls = DataLabelList() |
||
| 567 | line.dLbls.dLblPos = 't' |
||
| 568 | line.dLbls.showVal = False |
||
| 569 | line.dLbls.showPercent = False |
||
| 570 | chart_col = 'B' |
||
| 571 | chart_cell = chart_col + str(chart_start_row_number) |
||
| 572 | chart_start_row_number += 6 |
||
| 573 | ws.add_chart(line, chart_cell) |
||
| 574 | |||
| 575 | current_sheet_parameters_row_number = chart_start_row_number |
||
| 576 | |||
| 577 | current_sheet_parameters_row_number += 1 |
||
| 578 | |||
| 579 | filename = str(uuid.uuid4()) + '.xlsx' |
||
| 580 | wb.save(filename) |
||
| 581 | |||
| 582 | return filename |
||
| 583 | |||
| 584 | |||
| 585 | def get_parameters_timestamps_lists_max_len(parameters_timestamps_lists): |
||
| 586 | max_len = 0 |
||
| 587 | for i, value in enumerate(list(parameters_timestamps_lists)): |
||
| 588 | if len(value) > max_len: |
||
| 589 | max_len = len(value) |
||
| 590 | |||
| 591 | return max_len |
||
| 592 | |||
| 593 | |||
| 594 | def timestamps_data_all_equal_0(lists): |
||
| 595 | for i, value in enumerate(list(lists)): |
||
| 596 | if len(value) > 0: |
||
| 597 | return False |
||
| 598 | |||
| 599 | return True |
||
| 600 | |||
| 601 | |||
| 602 | def timestamps_data_not_equal_0(lists): |
||
| 603 | number = 0 |
||
| 604 | for i, value in enumerate(list(lists)): |
||
| 605 | if len(value) > 0: |
||
| 606 | number += 1 |
||
| 607 | return number |
||
| 608 |