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