| Conditions | 47 |
| Total Lines | 542 |
| Code Lines | 413 |
| Lines | 291 |
| Ratio | 53.69 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like excelexporters.tenantenergycategory.generate_excel() 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 |
||
| 63 | def generate_excel(report, |
||
| 64 | name, |
||
| 65 | reporting_start_datetime_local, |
||
| 66 | reporting_end_datetime_local, |
||
| 67 | period_type): |
||
| 68 | |||
| 69 | wb = Workbook() |
||
| 70 | ws = wb.active |
||
| 71 | |||
| 72 | # Row height |
||
| 73 | ws.row_dimensions[1].height = 118 |
||
| 74 | for i in range(2, 37 + 1): |
||
| 75 | ws.row_dimensions[i].height = 30 |
||
| 76 | |||
| 77 | for i in range(38, 90 + 1): |
||
| 78 | ws.row_dimensions[i].height = 30 |
||
| 79 | |||
| 80 | # Col width |
||
| 81 | ws.column_dimensions['A'].width = 1.5 |
||
| 82 | ws.column_dimensions['B'].width = 20.0 |
||
| 83 | ws.column_dimensions['C'].width = 35.0 |
||
| 84 | |||
| 85 | for i in range(ord('D'), ord('I')): |
||
| 86 | ws.column_dimensions[chr(i)].width = 15.0 |
||
| 87 | |||
| 88 | # Font |
||
| 89 | name_font = Font(name='Constantia', size=15, bold=True) |
||
| 90 | title_font = Font(name='宋体', size=15, bold=True) |
||
| 91 | data_font = Font(name='Franklin Gothic Book', size=11) |
||
| 92 | |||
| 93 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
||
| 94 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
||
| 95 | right=Side(border_style='medium', color='00000000'), |
||
| 96 | bottom=Side(border_style='medium', color='00000000'), |
||
| 97 | top=Side(border_style='medium', color='00000000') |
||
| 98 | ) |
||
| 99 | b_border = Border( |
||
| 100 | bottom=Side(border_style='medium', color='00000000'), |
||
| 101 | ) |
||
| 102 | |||
| 103 | b_c_alignment = Alignment(vertical='bottom', |
||
| 104 | horizontal='center', |
||
| 105 | text_rotation=0, |
||
| 106 | wrap_text=False, |
||
| 107 | shrink_to_fit=False, |
||
| 108 | indent=0) |
||
| 109 | c_c_alignment = Alignment(vertical='center', |
||
| 110 | horizontal='center', |
||
| 111 | text_rotation=0, |
||
| 112 | wrap_text=False, |
||
| 113 | shrink_to_fit=False, |
||
| 114 | indent=0) |
||
| 115 | b_r_alignment = Alignment(vertical='bottom', |
||
| 116 | horizontal='right', |
||
| 117 | text_rotation=0, |
||
| 118 | wrap_text=False, |
||
| 119 | shrink_to_fit=False, |
||
| 120 | indent=0) |
||
| 121 | c_r_alignment = Alignment(vertical='bottom', |
||
| 122 | horizontal='center', |
||
| 123 | text_rotation=0, |
||
| 124 | wrap_text=False, |
||
| 125 | shrink_to_fit=False, |
||
| 126 | indent=0) |
||
| 127 | # Img |
||
| 128 | img = Image("excelexporters/myems.png") |
||
| 129 | # img = Image("myems.png") |
||
| 130 | ws.add_image(img, 'B1') |
||
| 131 | |||
| 132 | # Title |
||
| 133 | ws['B3'].font = name_font |
||
| 134 | ws['B3'].alignment = b_r_alignment |
||
| 135 | ws['B3'] = 'Name:' |
||
| 136 | ws['C3'].border = b_border |
||
| 137 | ws['C3'].alignment = b_c_alignment |
||
| 138 | ws['C3'].font = name_font |
||
| 139 | ws['C3'] = name |
||
| 140 | |||
| 141 | ws['D3'].font = name_font |
||
| 142 | ws['D3'].alignment = b_r_alignment |
||
| 143 | ws['D3'] = 'Period:' |
||
| 144 | ws['E3'].border = b_border |
||
| 145 | ws['E3'].alignment = b_c_alignment |
||
| 146 | ws['E3'].font = name_font |
||
| 147 | ws['E3'] = period_type |
||
| 148 | |||
| 149 | ws['F3'].font = name_font |
||
| 150 | ws['F3'].alignment = b_r_alignment |
||
| 151 | ws['F3'] = 'Date:' |
||
| 152 | ws.merge_cells("G3:J3") |
||
| 153 | for i in range(ord('G'), ord('J') + 1): |
||
| 154 | ws[chr(i) + '3'].border = b_border |
||
| 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 | |||
| 160 | if "reporting_period" not in report.keys() or \ |
||
| 161 | "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
||
| 162 | filename = str(uuid.uuid4()) + '.xlsx' |
||
| 163 | wb.save(filename) |
||
| 164 | |||
| 165 | return filename |
||
| 166 | |||
| 167 | ################################################# |
||
| 168 | |||
| 169 | reporting_period_data = report['reporting_period'] |
||
| 170 | |||
| 171 | has_energy_data_flag = True |
||
| 172 | if "names" not in reporting_period_data.keys() or \ |
||
| 173 | reporting_period_data['names'] is None or \ |
||
| 174 | len(reporting_period_data['names']) == 0: |
||
| 175 | has_energy_data_flag = False |
||
| 176 | |||
| 177 | View Code Duplication | if has_energy_data_flag: |
|
| 178 | ws['B6'].font = title_font |
||
| 179 | ws['B6'] = name+' 报告期消耗' |
||
| 180 | |||
| 181 | category = reporting_period_data['names'] |
||
| 182 | ca_len = len(category) |
||
| 183 | |||
| 184 | ws['B7'].fill = table_fill |
||
| 185 | |||
| 186 | ws['B8'].font = title_font |
||
| 187 | ws['B8'].alignment = c_c_alignment |
||
| 188 | ws['B8'] = '能耗' |
||
| 189 | ws['B8'].border = f_border |
||
| 190 | |||
| 191 | ws['B9'].font = title_font |
||
| 192 | ws['B9'].alignment = c_c_alignment |
||
| 193 | ws['B9'] = '单位面积能耗' |
||
| 194 | ws['B9'].border = f_border |
||
| 195 | |||
| 196 | ws['B10'].font = title_font |
||
| 197 | ws['B10'].alignment = c_c_alignment |
||
| 198 | ws['B10'] = '环比' |
||
| 199 | ws['B10'].border = f_border |
||
| 200 | |||
| 201 | col = 'B' |
||
| 202 | |||
| 203 | for i in range(0, ca_len): |
||
| 204 | col = chr(ord('C') + i) |
||
| 205 | ws[col + '7'].fill = table_fill |
||
| 206 | ws[col + '7'].font = name_font |
||
| 207 | ws[col + '7'].alignment = c_c_alignment |
||
| 208 | ws[col + '7'] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
| 209 | ws[col + '7'].border = f_border |
||
| 210 | |||
| 211 | ws[col + '8'].font = name_font |
||
| 212 | ws[col + '8'].alignment = c_c_alignment |
||
| 213 | ws[col + '8'] = round(reporting_period_data['subtotals'][i], 0) |
||
| 214 | ws[col + '8'].border = f_border |
||
| 215 | |||
| 216 | ws[col + '9'].font = name_font |
||
| 217 | ws[col + '9'].alignment = c_c_alignment |
||
| 218 | ws[col + '9'] = round(reporting_period_data['subtotals_per_unit_area'][i], 2) |
||
| 219 | ws[col + '9'].border = f_border |
||
| 220 | |||
| 221 | ws[col + '10'].font = name_font |
||
| 222 | ws[col + '10'].alignment = c_c_alignment |
||
| 223 | ws[col + '10'] = str(round(reporting_period_data['increment_rates'][i] * 100, 2)) + "%" \ |
||
| 224 | if reporting_period_data['increment_rates'][i] is not None else "-" |
||
| 225 | ws[col + '10'].border = f_border |
||
| 226 | |||
| 227 | # TCE TCO2E |
||
| 228 | end_col = col |
||
| 229 | # TCE |
||
| 230 | tce_col = chr(ord(end_col) + 1) |
||
| 231 | ws[tce_col + '7'].fill = table_fill |
||
| 232 | ws[tce_col + '7'].font = name_font |
||
| 233 | ws[tce_col + '7'].alignment = c_c_alignment |
||
| 234 | ws[tce_col + '7'] = "TCE" |
||
| 235 | ws[tce_col + '7'].border = f_border |
||
| 236 | |||
| 237 | ws[tce_col + '8'].font = name_font |
||
| 238 | ws[tce_col + '8'].alignment = c_c_alignment |
||
| 239 | ws[tce_col + '8'] = round(reporting_period_data['total_in_kgce'], 0) |
||
| 240 | ws[tce_col + '8'].border = f_border |
||
| 241 | |||
| 242 | ws[tce_col + '9'].font = name_font |
||
| 243 | ws[tce_col + '9'].alignment = c_c_alignment |
||
| 244 | ws[tce_col + '9'] = round(reporting_period_data['total_in_kgce_per_unit_area'], 2) |
||
| 245 | ws[tce_col + '9'].border = f_border |
||
| 246 | |||
| 247 | ws[tce_col + '10'].font = name_font |
||
| 248 | ws[tce_col + '10'].alignment = c_c_alignment |
||
| 249 | ws[tce_col + '10'] = str(round(reporting_period_data['increment_rate_in_kgce'] * 100, 2)) + "%" \ |
||
| 250 | if reporting_period_data['increment_rate_in_kgce'] is not None else "-" |
||
| 251 | ws[tce_col + '10'].border = f_border |
||
| 252 | |||
| 253 | # TCO2E |
||
| 254 | tco2e_col = chr(ord(end_col) + 2) |
||
| 255 | ws[tco2e_col + '7'].fill = table_fill |
||
| 256 | ws[tco2e_col + '7'].font = name_font |
||
| 257 | ws[tco2e_col + '7'].alignment = c_c_alignment |
||
| 258 | ws[tco2e_col + '7'] = "TCO2E" |
||
| 259 | ws[tco2e_col + '7'].border = f_border |
||
| 260 | |||
| 261 | ws[tco2e_col + '8'].font = name_font |
||
| 262 | ws[tco2e_col + '8'].alignment = c_c_alignment |
||
| 263 | ws[tco2e_col + '8'] = round(reporting_period_data['total_in_kgco2e'], 0) |
||
| 264 | ws[tco2e_col + '8'].border = f_border |
||
| 265 | |||
| 266 | ws[tco2e_col + '9'].font = name_font |
||
| 267 | ws[tco2e_col + '9'].alignment = c_c_alignment |
||
| 268 | ws[tco2e_col + '9'] = round(reporting_period_data['total_in_kgco2e_per_unit_area'], 2) |
||
| 269 | ws[tco2e_col + '9'].border = f_border |
||
| 270 | |||
| 271 | ws[tco2e_col + '10'].font = name_font |
||
| 272 | ws[tco2e_col + '10'].alignment = c_c_alignment |
||
| 273 | ws[tco2e_col + '10'] = str(round(reporting_period_data['increment_rate_in_kgco2e'] * 100, 2)) + "%" \ |
||
| 274 | if reporting_period_data['increment_rate_in_kgco2e'] is not None else "-" |
||
| 275 | ws[tco2e_col + '10'].border = f_border |
||
| 276 | else: |
||
| 277 | for i in range(6, 10 + 1): |
||
| 278 | ws.row_dimensions[i].height = 0.1 |
||
| 279 | |||
| 280 | ################################################# |
||
| 281 | |||
| 282 | has_ele_peak_flag = True |
||
| 283 | if "toppeaks" not in reporting_period_data.keys() or \ |
||
| 284 | reporting_period_data['toppeaks'] is None or \ |
||
| 285 | len(reporting_period_data['toppeaks']) == 0: |
||
| 286 | has_ele_peak_flag = False |
||
| 287 | |||
| 288 | View Code Duplication | if has_ele_peak_flag: |
|
| 289 | ws['B12'].font = title_font |
||
| 290 | ws['B12'] = name+' 分时电耗' |
||
| 291 | |||
| 292 | ws['B13'].fill = table_fill |
||
| 293 | ws['B13'].font = name_font |
||
| 294 | ws['B13'].alignment = c_c_alignment |
||
| 295 | ws['B13'].border = f_border |
||
| 296 | |||
| 297 | ws['C13'].fill = table_fill |
||
| 298 | ws['C13'].font = name_font |
||
| 299 | ws['C13'].alignment = c_c_alignment |
||
| 300 | ws['C13'].border = f_border |
||
| 301 | ws['C13'] = '分时电耗' |
||
| 302 | |||
| 303 | ws['B14'].font = title_font |
||
| 304 | ws['B14'].alignment = c_c_alignment |
||
| 305 | ws['B14'] = '尖' |
||
| 306 | ws['B14'].border = f_border |
||
| 307 | |||
| 308 | ws['C14'].font = title_font |
||
| 309 | ws['C14'].alignment = c_c_alignment |
||
| 310 | ws['C14'].border = f_border |
||
| 311 | ws['C14'] = round(reporting_period_data['toppeaks'][0], 0) |
||
| 312 | |||
| 313 | ws['B15'].font = title_font |
||
| 314 | ws['B15'].alignment = c_c_alignment |
||
| 315 | ws['B15'] = '峰' |
||
| 316 | ws['B15'].border = f_border |
||
| 317 | |||
| 318 | ws['C15'].font = title_font |
||
| 319 | ws['C15'].alignment = c_c_alignment |
||
| 320 | ws['C15'].border = f_border |
||
| 321 | ws['C15'] = round(reporting_period_data['onpeaks'][0], 0) |
||
| 322 | |||
| 323 | ws['B16'].font = title_font |
||
| 324 | ws['B16'].alignment = c_c_alignment |
||
| 325 | ws['B16'] = '平' |
||
| 326 | ws['B16'].border = f_border |
||
| 327 | |||
| 328 | ws['C16'].font = title_font |
||
| 329 | ws['C16'].alignment = c_c_alignment |
||
| 330 | ws['C16'].border = f_border |
||
| 331 | ws['C16'] = round(reporting_period_data['midpeaks'][0], 0) |
||
| 332 | |||
| 333 | ws['B17'].font = title_font |
||
| 334 | ws['B17'].alignment = c_c_alignment |
||
| 335 | ws['B17'] = '谷' |
||
| 336 | ws['B17'].border = f_border |
||
| 337 | |||
| 338 | ws['C17'].font = title_font |
||
| 339 | ws['C17'].alignment = c_c_alignment |
||
| 340 | ws['C17'].border = f_border |
||
| 341 | ws['C17'] = round(reporting_period_data['offpeaks'][0], 0) |
||
| 342 | |||
| 343 | pie = PieChart() |
||
| 344 | labels = Reference(ws, min_col=2, min_row=14, max_row=17) |
||
| 345 | pie_data = Reference(ws, min_col=3, min_row=13, max_row=17) |
||
| 346 | pie.add_data(pie_data, titles_from_data=True) |
||
| 347 | pie.set_categories(labels) |
||
| 348 | pie.height = 5.25 |
||
| 349 | pie.width = 9 |
||
| 350 | s1 = pie.series[0] |
||
| 351 | s1.dLbls = DataLabelList() |
||
| 352 | s1.dLbls.showCatName = False |
||
| 353 | s1.dLbls.showVal = True |
||
| 354 | s1.dLbls.showPercent = True |
||
| 355 | ws.add_chart(pie, "D13") |
||
| 356 | |||
| 357 | else: |
||
| 358 | for i in range(12, 18 + 1): |
||
| 359 | ws.row_dimensions[i].height = 0.1 |
||
| 360 | |||
| 361 | ################################################ |
||
| 362 | |||
| 363 | current_row_number = 19 |
||
| 364 | |||
| 365 | has_kgce_data_flag = True |
||
| 366 | if "subtotals_in_kgce" not in reporting_period_data.keys() or \ |
||
| 367 | reporting_period_data['subtotals_in_kgce'] is None or \ |
||
| 368 | len(reporting_period_data['subtotals_in_kgce']) == 0: |
||
| 369 | has_kgce_data_flag = False |
||
| 370 | |||
| 371 | View Code Duplication | if has_kgce_data_flag: |
|
| 372 | ws['B' + str(current_row_number)].font = title_font |
||
| 373 | ws['B' + str(current_row_number)] = name + ' 吨标准煤 (TCE) 占比' |
||
| 374 | |||
| 375 | current_row_number += 1 |
||
| 376 | table_start_row_number = current_row_number |
||
| 377 | |||
| 378 | ws['B' + str(current_row_number)].fill = table_fill |
||
| 379 | ws['B' + str(current_row_number)].font = name_font |
||
| 380 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
| 381 | ws['B' + str(current_row_number)].border = f_border |
||
| 382 | |||
| 383 | ws['C' + str(current_row_number)].fill = table_fill |
||
| 384 | ws['C' + str(current_row_number)].font = name_font |
||
| 385 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
| 386 | ws['C' + str(current_row_number)].border = f_border |
||
| 387 | ws['C' + str(current_row_number)] = '吨标准煤 (TCE) 占比' |
||
| 388 | |||
| 389 | current_row_number += 1 |
||
| 390 | |||
| 391 | ca_len = len(reporting_period_data['names']) |
||
| 392 | |||
| 393 | for i in range(0, ca_len): |
||
| 394 | ws['B' + str(current_row_number)].font = title_font |
||
| 395 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
| 396 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
||
| 397 | ws['B' + str(current_row_number)].border = f_border |
||
| 398 | |||
| 399 | ws['C' + str(current_row_number)].font = title_font |
||
| 400 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
| 401 | ws['C' + str(current_row_number)].border = f_border |
||
| 402 | ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals_in_kgce'][i], 3) |
||
| 403 | |||
| 404 | current_row_number += 1 |
||
| 405 | |||
| 406 | table_end_row_number = current_row_number - 1 |
||
| 407 | |||
| 408 | pie = PieChart() |
||
| 409 | labels = Reference(ws, min_col=2, min_row=table_start_row_number+1, max_row=table_end_row_number) |
||
| 410 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
||
| 411 | pie.add_data(pie_data, titles_from_data=True) |
||
| 412 | pie.set_categories(labels) |
||
| 413 | pie.height = 5.25 |
||
| 414 | pie.width = 9 |
||
| 415 | s1 = pie.series[0] |
||
| 416 | s1.dLbls = DataLabelList() |
||
| 417 | s1.dLbls.showCatName = False |
||
| 418 | s1.dLbls.showVal = True |
||
| 419 | s1.dLbls.showPercent = True |
||
| 420 | table_cell = 'D' + str(table_start_row_number) |
||
| 421 | ws.add_chart(pie, table_cell) |
||
| 422 | |||
| 423 | if ca_len < 4: |
||
| 424 | current_row_number = current_row_number - ca_len + 4 |
||
| 425 | |||
| 426 | else: |
||
| 427 | for i in range(21, 29 + 1): |
||
| 428 | current_row_number = 30 |
||
| 429 | ws.row_dimensions[i].height = 0.1 |
||
| 430 | |||
| 431 | ##################################################### |
||
| 432 | |||
| 433 | current_row_number += 1 |
||
| 434 | has_kgco2e_data_flag = True |
||
| 435 | |||
| 436 | if "subtotals_in_kgco2e" not in reporting_period_data.keys() or \ |
||
| 437 | reporting_period_data['subtotals_in_kgco2e'] is None or \ |
||
| 438 | len(reporting_period_data['subtotals_in_kgco2e']) == 0: |
||
| 439 | has_kgco2e_data_flag = False |
||
| 440 | |||
| 441 | View Code Duplication | if has_kgco2e_data_flag: |
|
| 442 | ws['B' + str(current_row_number)].font = title_font |
||
| 443 | ws['B' + str(current_row_number)] = name + ' 吨二氧化碳排放 (TCO2E) 占比' |
||
| 444 | |||
| 445 | current_row_number += 1 |
||
| 446 | table_start_row_number = current_row_number |
||
| 447 | |||
| 448 | ws['B' + str(current_row_number)].fill = table_fill |
||
| 449 | ws['B' + str(current_row_number)].font = name_font |
||
| 450 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
| 451 | ws['B' + str(current_row_number)].border = f_border |
||
| 452 | |||
| 453 | ws['C' + str(current_row_number)].fill = table_fill |
||
| 454 | ws['C' + str(current_row_number)].font = name_font |
||
| 455 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
| 456 | ws['C' + str(current_row_number)].border = f_border |
||
| 457 | ws['C' + str(current_row_number)] = '吨二氧化碳排放 (TCO2E) 占比' |
||
| 458 | |||
| 459 | current_row_number += 1 |
||
| 460 | |||
| 461 | ca_len = len(reporting_period_data['names']) |
||
| 462 | |||
| 463 | for i in range(0, ca_len): |
||
| 464 | ws['B' + str(current_row_number)].font = title_font |
||
| 465 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
| 466 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
||
| 467 | ws['B' + str(current_row_number)].border = f_border |
||
| 468 | |||
| 469 | ws['C' + str(current_row_number)].font = title_font |
||
| 470 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
| 471 | ws['C' + str(current_row_number)].border = f_border |
||
| 472 | ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals_in_kgco2e'][i], 3) |
||
| 473 | current_row_number += 1 |
||
| 474 | |||
| 475 | table_end_row_number = current_row_number - 1 |
||
| 476 | |||
| 477 | pie = PieChart() |
||
| 478 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
| 479 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
||
| 480 | pie.add_data(pie_data, titles_from_data=True) |
||
| 481 | pie.set_categories(labels) |
||
| 482 | pie.height = 5.25 |
||
| 483 | pie.width = 9 |
||
| 484 | s1 = pie.series[0] |
||
| 485 | s1.dLbls = DataLabelList() |
||
| 486 | s1.dLbls.showCatName = False |
||
| 487 | s1.dLbls.showVal = True |
||
| 488 | s1.dLbls.showPercent = True |
||
| 489 | table_cell = 'D' + str(table_start_row_number) |
||
| 490 | ws.add_chart(pie, table_cell) |
||
| 491 | |||
| 492 | if ca_len < 4: |
||
| 493 | current_row_number = current_row_number - ca_len + 4 |
||
| 494 | |||
| 495 | else: |
||
| 496 | for i in range(30, 39 + 1): |
||
| 497 | current_row_number = 40 |
||
| 498 | ws.row_dimensions[i].height = 0.1 |
||
| 499 | |||
| 500 | ############################################### |
||
| 501 | current_row_number += 1 |
||
| 502 | |||
| 503 | has_detail_data_flag = True |
||
| 504 | |||
| 505 | table_start_draw_flag = current_row_number + 1 |
||
| 506 | |||
| 507 | if "timestamps" not in reporting_period_data.keys() or \ |
||
| 508 | reporting_period_data['timestamps'] is None or \ |
||
| 509 | len(reporting_period_data['timestamps']) == 0: |
||
| 510 | has_detail_data_flag = False |
||
| 511 | |||
| 512 | if has_detail_data_flag: |
||
| 513 | reporting_period_data = report['reporting_period'] |
||
| 514 | times = reporting_period_data['timestamps'] |
||
| 515 | ca_len = len(report['reporting_period']['names']) |
||
| 516 | |||
| 517 | ws['B' + str(current_row_number)].font = title_font |
||
| 518 | ws['B' + str(current_row_number)] = name+' 详细数据' |
||
| 519 | |||
| 520 | table_start_row_number = (current_row_number + 1) + ca_len * 5 |
||
| 521 | current_row_number = table_start_row_number |
||
| 522 | |||
| 523 | time = times[0] |
||
| 524 | has_data = False |
||
| 525 | |||
| 526 | if len(time) > 0: |
||
| 527 | has_data = True |
||
| 528 | |||
| 529 | if has_data: |
||
| 530 | |||
| 531 | ws['B' + str(current_row_number)].fill = table_fill |
||
| 532 | ws['B' + str(current_row_number)].font = title_font |
||
| 533 | ws['B' + str(current_row_number)].border = f_border |
||
| 534 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
| 535 | ws['B' + str(current_row_number)] = '日期时间' |
||
| 536 | |||
| 537 | for i in range(0, ca_len): |
||
| 538 | col = chr(ord('C') + i) |
||
| 539 | |||
| 540 | ws[col + str(current_row_number)].fill = table_fill |
||
| 541 | ws[col + str(current_row_number)].font = title_font |
||
| 542 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 543 | ws[col + str(current_row_number)] = reporting_period_data['names'][i] + \ |
||
| 544 | " (" + reporting_period_data['units'][i] + ")" |
||
| 545 | ws[col + str(current_row_number)].border = f_border |
||
| 546 | |||
| 547 | current_row_number += 1 |
||
| 548 | |||
| 549 | for i in range(0, len(time)): |
||
| 550 | ws['B' + str(current_row_number)].font = title_font |
||
| 551 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
| 552 | ws['B' + str(current_row_number)] = time[i] |
||
| 553 | ws['B' + str(current_row_number)].border = f_border |
||
| 554 | |||
| 555 | for j in range(0, ca_len): |
||
| 556 | col = chr(ord('C') + j) |
||
| 557 | |||
| 558 | ws[col + str(current_row_number)].font = title_font |
||
| 559 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 560 | ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 0) |
||
| 561 | ws[col + str(current_row_number)].border = f_border |
||
| 562 | |||
| 563 | current_row_number += 1 |
||
| 564 | |||
| 565 | table_end_row_number = current_row_number - 1 |
||
| 566 | |||
| 567 | ws['B' + str(current_row_number)].font = title_font |
||
| 568 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
| 569 | ws['B' + str(current_row_number)] = '小计' |
||
| 570 | ws['B' + str(current_row_number)].border = f_border |
||
| 571 | |||
| 572 | for i in range(0, ca_len): |
||
| 573 | col = chr(ord('C') + i) |
||
| 574 | ws[col + str(current_row_number)].font = title_font |
||
| 575 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 576 | ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 0) |
||
| 577 | ws[col + str(current_row_number)].border = f_border |
||
| 578 | |||
| 579 | # bar |
||
| 580 | bar = BarChart() |
||
| 581 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
| 582 | bar_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number) |
||
| 583 | bar.add_data(bar_data, titles_from_data=True) |
||
| 584 | bar.set_categories(labels) |
||
| 585 | bar.height = 5.25 |
||
| 586 | bar.width = len(time) |
||
| 587 | bar.dLbls = DataLabelList() |
||
| 588 | bar.dLbls.showVal = True |
||
| 589 | bar.dLbls.showPercent = True |
||
| 590 | chart_col = 'B' |
||
| 591 | chart_cell = chart_col + str(table_start_draw_flag + 5 * i) |
||
| 592 | ws.add_chart(bar, chart_cell) |
||
| 593 | |||
| 594 | current_row_number += 1 |
||
| 595 | |||
| 596 | else: |
||
| 597 | for i in range(40, 69 + 1): |
||
| 598 | current_row_number = 70 |
||
| 599 | ws.row_dimensions[i].height = 0.1 |
||
| 600 | |||
| 601 | filename = str(uuid.uuid4()) + '.xlsx' |
||
| 602 | wb.save(filename) |
||
| 603 | |||
| 604 | return filename |
||
| 605 |