| Conditions | 50 |
| Total Lines | 643 |
| Code Lines | 483 |
| Lines | 643 |
| Ratio | 100 % |
| 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.virtualmetercarbon.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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | """ |
||
| 91 | View Code Duplication | def generate_excel(report, |
|
| 92 | name, |
||
| 93 | base_period_start_datetime_local, |
||
| 94 | base_period_end_datetime_local, |
||
| 95 | reporting_start_datetime_local, |
||
| 96 | reporting_end_datetime_local, |
||
| 97 | period_type, |
||
| 98 | language): |
||
| 99 | |||
| 100 | trans = get_translation(language) |
||
| 101 | trans.install() |
||
| 102 | _ = trans.gettext |
||
| 103 | |||
| 104 | wb = Workbook() |
||
| 105 | |||
| 106 | ws = wb.active |
||
| 107 | ws.title = "VirtualMeterCarbon" |
||
| 108 | # Row height |
||
| 109 | ws.row_dimensions[1].height = 102 |
||
| 110 | for i in range(2, 2000 + 1): |
||
| 111 | ws.row_dimensions[i].height = 42 |
||
| 112 | |||
| 113 | # Col width |
||
| 114 | ws.column_dimensions['A'].width = 1.5 |
||
| 115 | |||
| 116 | ws.column_dimensions['B'].width = 25.0 |
||
| 117 | |||
| 118 | for i in range(ord('C'), ord('Z')): |
||
| 119 | ws.column_dimensions[chr(i)].width = 15.0 |
||
| 120 | |||
| 121 | # Font |
||
| 122 | name_font = Font(name='Arial', size=15, bold=True) |
||
| 123 | title_font = Font(name='Arial', size=15, bold=True) |
||
| 124 | |||
| 125 | table_fill = PatternFill(fill_type='solid', fgColor='90ee90') |
||
| 126 | f_border = Border(left=Side(border_style='medium'), |
||
| 127 | right=Side(border_style='medium'), |
||
| 128 | bottom=Side(border_style='medium'), |
||
| 129 | top=Side(border_style='medium') |
||
| 130 | ) |
||
| 131 | b_border = Border( |
||
| 132 | bottom=Side(border_style='medium'), |
||
| 133 | ) |
||
| 134 | |||
| 135 | b_c_alignment = Alignment(vertical='bottom', |
||
| 136 | horizontal='center', |
||
| 137 | text_rotation=0, |
||
| 138 | wrap_text=True, |
||
| 139 | shrink_to_fit=False, |
||
| 140 | indent=0) |
||
| 141 | c_c_alignment = Alignment(vertical='center', |
||
| 142 | horizontal='center', |
||
| 143 | text_rotation=0, |
||
| 144 | wrap_text=True, |
||
| 145 | shrink_to_fit=False, |
||
| 146 | indent=0) |
||
| 147 | b_r_alignment = Alignment(vertical='bottom', |
||
| 148 | horizontal='right', |
||
| 149 | text_rotation=0, |
||
| 150 | wrap_text=True, |
||
| 151 | shrink_to_fit=False, |
||
| 152 | indent=0) |
||
| 153 | |||
| 154 | # Img |
||
| 155 | img = Image("excelexporters/myems.png") |
||
| 156 | ws.add_image(img, 'A1') |
||
| 157 | |||
| 158 | # Title |
||
| 159 | ws['B3'].alignment = b_r_alignment |
||
| 160 | ws['B3'] = _('Name') + ':' |
||
| 161 | ws['C3'].border = b_border |
||
| 162 | ws['C3'].alignment = b_c_alignment |
||
| 163 | ws['C3'] = name |
||
| 164 | |||
| 165 | ws['D3'].alignment = b_r_alignment |
||
| 166 | ws['D3'] = _('Period Type') + ':' |
||
| 167 | ws['E3'].border = b_border |
||
| 168 | ws['E3'].alignment = b_c_alignment |
||
| 169 | ws['E3'] = period_type |
||
| 170 | |||
| 171 | ws['B4'].alignment = b_r_alignment |
||
| 172 | ws['B4'] = _('Reporting Start Datetime') + ':' |
||
| 173 | ws['C4'].border = b_border |
||
| 174 | ws['C4'].alignment = b_c_alignment |
||
| 175 | ws['C4'] = reporting_start_datetime_local |
||
| 176 | |||
| 177 | ws['D4'].alignment = b_r_alignment |
||
| 178 | ws['D4'] = _('Reporting End Datetime') + ':' |
||
| 179 | ws['E4'].border = b_border |
||
| 180 | ws['E4'].alignment = b_c_alignment |
||
| 181 | ws['E4'] = reporting_end_datetime_local |
||
| 182 | |||
| 183 | is_base_period_timestamp_exists_flag = is_base_period_timestamp_exists(report['base_period']) |
||
| 184 | |||
| 185 | if is_base_period_timestamp_exists_flag: |
||
| 186 | ws['B5'].alignment = b_r_alignment |
||
| 187 | ws['B5'] = _('Base Period Start Datetime') + ':' |
||
| 188 | ws['C5'].border = b_border |
||
| 189 | ws['C5'].alignment = b_c_alignment |
||
| 190 | ws['C5'] = base_period_start_datetime_local |
||
| 191 | |||
| 192 | ws['D5'].alignment = b_r_alignment |
||
| 193 | ws['D5'] = _('Base Period End Datetime') + ':' |
||
| 194 | ws['E5'].border = b_border |
||
| 195 | ws['E5'].alignment = b_c_alignment |
||
| 196 | ws['E5'] = base_period_end_datetime_local |
||
| 197 | |||
| 198 | if "reporting_period" not in report.keys() or \ |
||
| 199 | "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
||
| 200 | filename = str(uuid.uuid4()) + '.xlsx' |
||
| 201 | wb.save(filename) |
||
| 202 | |||
| 203 | return filename |
||
| 204 | |||
| 205 | #################################################################################################################### |
||
| 206 | if "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
||
| 207 | for i in range(7, 10 + 1): |
||
| 208 | ws.row_dimensions[i].height = 0.1 |
||
| 209 | else: |
||
| 210 | ws['B7'].font = title_font |
||
| 211 | ws['B7'] = name + _('Reporting Period Carbon Dioxide Emissions') |
||
| 212 | |||
| 213 | reporting_period_data = report['reporting_period'] |
||
| 214 | |||
| 215 | ws.row_dimensions[8].height = 60 |
||
| 216 | ws['B8'].fill = table_fill |
||
| 217 | ws['B8'].border = f_border |
||
| 218 | |||
| 219 | ws['B9'].font = title_font |
||
| 220 | ws['B9'].alignment = c_c_alignment |
||
| 221 | ws['B9'] = _('Carbon Dioxide Emissions') |
||
| 222 | ws['B9'].border = f_border |
||
| 223 | |||
| 224 | ws['B10'].font = title_font |
||
| 225 | ws['B10'].alignment = c_c_alignment |
||
| 226 | ws['B10'] = _('Increment Rate') |
||
| 227 | ws['B10'].border = f_border |
||
| 228 | |||
| 229 | ws['C8'].fill = table_fill |
||
| 230 | ws['C8'].font = name_font |
||
| 231 | ws['C8'].alignment = c_c_alignment |
||
| 232 | ws['C8'] = report['virtual_meter']['energy_category_name'] + \ |
||
| 233 | " (" + report['virtual_meter']['unit_of_measure'] + ")" |
||
| 234 | ws['C8'].border = f_border |
||
| 235 | |||
| 236 | ws['C9'].font = name_font |
||
| 237 | ws['C9'].alignment = c_c_alignment |
||
| 238 | ws['C9'] = round2(reporting_period_data['total_in_category'], 2) |
||
| 239 | ws['C9'].border = f_border |
||
| 240 | |||
| 241 | ws['C10'].font = name_font |
||
| 242 | ws['C10'].alignment = c_c_alignment |
||
| 243 | ws['C10'] = str(round2(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
||
| 244 | if reporting_period_data['increment_rate'] is not None else "-" |
||
| 245 | ws['C10'].border = f_border |
||
| 246 | |||
| 247 | # TCE |
||
| 248 | ws['D8'].fill = table_fill |
||
| 249 | ws['D8'].font = name_font |
||
| 250 | ws['D8'].alignment = c_c_alignment |
||
| 251 | ws['D8'] = _('Ton of Standard Coal') + '(TCE)' |
||
| 252 | ws['D8'].border = f_border |
||
| 253 | |||
| 254 | ws['D9'].font = name_font |
||
| 255 | ws['D9'].alignment = c_c_alignment |
||
| 256 | ws['D9'] = round2(reporting_period_data['total_in_kgce'] / 1000, 2) |
||
| 257 | ws['D9'].border = f_border |
||
| 258 | |||
| 259 | ws['D10'].font = name_font |
||
| 260 | ws['D10'].alignment = c_c_alignment |
||
| 261 | ws['D10'] = str(round2(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
||
| 262 | if reporting_period_data['increment_rate'] is not None else "-" |
||
| 263 | ws['D10'].border = f_border |
||
| 264 | |||
| 265 | # TCO2E |
||
| 266 | ws['E8'].fill = table_fill |
||
| 267 | ws['E8'].font = name_font |
||
| 268 | ws['E8'].alignment = c_c_alignment |
||
| 269 | ws['E8'] = _('Ton of Carbon Dioxide Emissions') + '(TCO2E)' |
||
| 270 | ws['E8'].border = f_border |
||
| 271 | |||
| 272 | ws['E9'].font = name_font |
||
| 273 | ws['E9'].alignment = c_c_alignment |
||
| 274 | ws['E9'] = round2(reporting_period_data['total_in_kgco2e'] / 1000, 2) |
||
| 275 | ws['E9'].border = f_border |
||
| 276 | |||
| 277 | ws['E10'].font = name_font |
||
| 278 | ws['E10'].alignment = c_c_alignment |
||
| 279 | ws['E10'] = str(round2(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
||
| 280 | if reporting_period_data['increment_rate'] is not None else "-" |
||
| 281 | ws['E10'].border = f_border |
||
| 282 | |||
| 283 | #################################################################################################################### |
||
| 284 | current_row_number = 12 |
||
| 285 | |||
| 286 | table_start_draw_flag = current_row_number + 1 |
||
| 287 | reporting_period_data = report['reporting_period'] |
||
| 288 | |||
| 289 | if "timestamps" not in reporting_period_data.keys() or \ |
||
| 290 | reporting_period_data['timestamps'] is None or \ |
||
| 291 | len(reporting_period_data['timestamps']) == 0: |
||
| 292 | pass |
||
| 293 | else: |
||
| 294 | if not is_base_period_timestamp_exists_flag: |
||
| 295 | reporting_period_data = report['reporting_period'] |
||
| 296 | time = reporting_period_data['timestamps'] |
||
| 297 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
||
| 298 | ws['B' + str(current_row_number)].font = title_font |
||
| 299 | ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data') |
||
| 300 | |||
| 301 | current_row_number += 1 |
||
| 302 | # 1: Stand for blank line 2: Stand for title |
||
| 303 | current_row_number += 1 * 6 + real_timestamps_len * 6 + 1 + 2 |
||
| 304 | table_start_row_number = current_row_number |
||
| 305 | |||
| 306 | has_data = False |
||
| 307 | |||
| 308 | if len(time) > 0: |
||
| 309 | has_data = True |
||
| 310 | |||
| 311 | if has_data: |
||
| 312 | |||
| 313 | ws.row_dimensions[current_row_number].height = 60 |
||
| 314 | current_col_number = 2 |
||
| 315 | col = format_cell.get_column_letter(current_col_number) |
||
| 316 | ws[col + str(current_row_number)].fill = table_fill |
||
| 317 | ws[col + str(current_row_number)].font = title_font |
||
| 318 | ws[col + str(current_row_number)].border = f_border |
||
| 319 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 320 | ws[col + str(current_row_number)] = _('Datetime') |
||
| 321 | |||
| 322 | current_col_number += 1 |
||
| 323 | col = format_cell.get_column_letter(current_col_number) |
||
| 324 | |||
| 325 | ws[col + str(current_row_number)].fill = table_fill |
||
| 326 | ws[col + str(current_row_number)].font = title_font |
||
| 327 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 328 | ws[col + str(current_row_number)] = report['virtual_meter']['energy_category_name'] + \ |
||
| 329 | " (" + report['virtual_meter']['unit_of_measure'] + ")" |
||
| 330 | ws[col + str(current_row_number)].border = f_border |
||
| 331 | |||
| 332 | current_row_number += 1 |
||
| 333 | |||
| 334 | for i in range(0, len(time)): |
||
| 335 | current_col_number = 2 |
||
| 336 | col = format_cell.get_column_letter(current_col_number) |
||
| 337 | |||
| 338 | ws[col + str(current_row_number)].font = title_font |
||
| 339 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 340 | ws[col + str(current_row_number)] = time[i] |
||
| 341 | ws[col + str(current_row_number)].border = f_border |
||
| 342 | |||
| 343 | current_col_number += 1 |
||
| 344 | col = format_cell.get_column_letter(current_col_number) |
||
| 345 | |||
| 346 | ws[col + str(current_row_number)].font = title_font |
||
| 347 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 348 | ws[col + str(current_row_number)] = round2(reporting_period_data['values'][i], 2) |
||
| 349 | ws[col + str(current_row_number)].border = f_border |
||
| 350 | |||
| 351 | current_row_number += 1 |
||
| 352 | |||
| 353 | table_end_row_number = current_row_number - 1 |
||
| 354 | |||
| 355 | current_col_number = 2 |
||
| 356 | col = format_cell.get_column_letter(current_col_number) |
||
| 357 | |||
| 358 | ws[col + str(current_row_number)].font = title_font |
||
| 359 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 360 | ws[col + str(current_row_number)] = _('Total') |
||
| 361 | ws[col + str(current_row_number)].border = f_border |
||
| 362 | |||
| 363 | current_col_number += 1 |
||
| 364 | col = format_cell.get_column_letter(current_col_number) |
||
| 365 | ws[col + str(current_row_number)].font = title_font |
||
| 366 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 367 | ws[col + str(current_row_number)] = round2(reporting_period_data['total_in_category'], 2) |
||
| 368 | ws[col + str(current_row_number)].border = f_border |
||
| 369 | |||
| 370 | # line |
||
| 371 | line = LineChart() |
||
| 372 | line.title = _('Reporting Period Carbon Dioxide Emissions') + ' - ' \ |
||
| 373 | + report['virtual_meter']['energy_category_name'] + \ |
||
| 374 | " (" + report['virtual_meter']['unit_of_measure'] + ")" |
||
| 375 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
| 376 | line_data = Reference(ws, min_col=3, min_row=table_start_row_number, |
||
| 377 | max_row=table_end_row_number) |
||
| 378 | line.add_data(line_data, titles_from_data=True) |
||
| 379 | line.set_categories(labels) |
||
| 380 | line_data = line.series[0] |
||
| 381 | line_data.marker.symbol = "auto" |
||
| 382 | line_data.smooth = True |
||
| 383 | line.x_axis.crosses = 'min' |
||
| 384 | line.height = 8.25 |
||
| 385 | line.width = 24 |
||
| 386 | chart_col = 'B' |
||
| 387 | chart_cell = chart_col + str(table_start_draw_flag) |
||
| 388 | ws.add_chart(line, chart_cell) |
||
| 389 | |||
| 390 | current_row_number += 2 |
||
| 391 | else: |
||
| 392 | base_period_data = report['base_period'] |
||
| 393 | reporting_period_data = report['reporting_period'] |
||
| 394 | base_period_timestamps = base_period_data['timestamps'] |
||
| 395 | reporting_period_timestamps = reporting_period_data['timestamps'] |
||
| 396 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
||
| 397 | ws['B' + str(current_row_number)].font = title_font |
||
| 398 | ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data') |
||
| 399 | |||
| 400 | current_row_number += 1 |
||
| 401 | # 1: Stand for blank line 2: Stand for title |
||
| 402 | current_row_number += 1 * 6 + real_timestamps_len * 6 + 1 + 2 |
||
| 403 | table_start_row_number = current_row_number |
||
| 404 | |||
| 405 | has_data = False |
||
| 406 | |||
| 407 | if len(base_period_timestamps[0]) or len(reporting_period_timestamps[0]) > 0: |
||
| 408 | has_data = True |
||
| 409 | |||
| 410 | if has_data: |
||
| 411 | ws.row_dimensions[current_row_number].height = 60 |
||
| 412 | current_col_number = 2 |
||
| 413 | col = format_cell.get_column_letter(current_col_number) |
||
| 414 | ws[col + str(current_row_number)].fill = table_fill |
||
| 415 | ws[col + str(current_row_number)].font = title_font |
||
| 416 | ws[col + str(current_row_number)].border = f_border |
||
| 417 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 418 | ws[col + str(current_row_number)] = _('Base Period') + " - " + _('Datetime') |
||
| 419 | |||
| 420 | current_col_number += 1 |
||
| 421 | col = format_cell.get_column_letter(current_col_number) |
||
| 422 | |||
| 423 | ws[col + str(current_row_number)].fill = table_fill |
||
| 424 | ws[col + str(current_row_number)].font = title_font |
||
| 425 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 426 | ws[col + str(current_row_number)] = _('Base Period') + " - " + \ |
||
| 427 | report['virtual_meter']['energy_category_name'] + \ |
||
| 428 | " (" + report['virtual_meter']['unit_of_measure'] + ")" |
||
| 429 | ws[col + str(current_row_number)].border = f_border |
||
| 430 | |||
| 431 | current_col_number += 1 |
||
| 432 | col = format_cell.get_column_letter(current_col_number) |
||
| 433 | |||
| 434 | ws[col + str(current_row_number)].fill = table_fill |
||
| 435 | ws[col + str(current_row_number)].font = title_font |
||
| 436 | ws[col + str(current_row_number)].border = f_border |
||
| 437 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 438 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " + _('Datetime') |
||
| 439 | |||
| 440 | current_col_number += 1 |
||
| 441 | col = format_cell.get_column_letter(current_col_number) |
||
| 442 | ws[col + str(current_row_number)].fill = table_fill |
||
| 443 | ws[col + str(current_row_number)].font = title_font |
||
| 444 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 445 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " \ |
||
| 446 | + report['virtual_meter']['energy_category_name'] + \ |
||
| 447 | " (" + report['virtual_meter']['unit_of_measure'] + ")" |
||
| 448 | ws[col + str(current_row_number)].border = f_border |
||
| 449 | |||
| 450 | current_row_number += 1 |
||
| 451 | |||
| 452 | max_timestamps_len = len(base_period_timestamps) \ |
||
| 453 | if len(base_period_timestamps) >= len(reporting_period_timestamps) \ |
||
| 454 | else len(reporting_period_timestamps) |
||
| 455 | |||
| 456 | for i in range(0, max_timestamps_len): |
||
| 457 | current_col_number = 2 |
||
| 458 | col = format_cell.get_column_letter(current_col_number) |
||
| 459 | ws[col + str(current_row_number)].font = title_font |
||
| 460 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 461 | ws[col + str(current_row_number)] = base_period_timestamps[i] \ |
||
| 462 | if i < len(base_period_timestamps) else None |
||
| 463 | ws[col + str(current_row_number)].border = f_border |
||
| 464 | |||
| 465 | current_col_number += 1 |
||
| 466 | col = format_cell.get_column_letter(current_col_number) |
||
| 467 | |||
| 468 | ws[col + str(current_row_number)].font = title_font |
||
| 469 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 470 | ws[col + str(current_row_number)] = round2(base_period_data['values'][i], 2) \ |
||
| 471 | if i < len(base_period_data['values']) else None |
||
| 472 | ws[col + str(current_row_number)].border = f_border |
||
| 473 | |||
| 474 | current_col_number += 1 |
||
| 475 | col = format_cell.get_column_letter(current_col_number) |
||
| 476 | |||
| 477 | ws[col + str(current_row_number)].font = title_font |
||
| 478 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 479 | ws[col + str(current_row_number)] = reporting_period_timestamps[i] \ |
||
| 480 | if i < len(reporting_period_timestamps) else None |
||
| 481 | ws[col + str(current_row_number)].border = f_border |
||
| 482 | |||
| 483 | current_col_number += 1 |
||
| 484 | col = format_cell.get_column_letter(current_col_number) |
||
| 485 | |||
| 486 | ws[col + str(current_row_number)].font = title_font |
||
| 487 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 488 | ws[col + str(current_row_number)] = round2(reporting_period_data['values'][i], 2) \ |
||
| 489 | if i < len(reporting_period_data['values']) else None |
||
| 490 | ws[col + str(current_row_number)].border = f_border |
||
| 491 | |||
| 492 | current_row_number += 1 |
||
| 493 | |||
| 494 | current_col_number = 2 |
||
| 495 | col = format_cell.get_column_letter(current_col_number) |
||
| 496 | ws[col + str(current_row_number)].font = title_font |
||
| 497 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 498 | ws[col + str(current_row_number)] = _('Total') |
||
| 499 | ws[col + str(current_row_number)].border = f_border |
||
| 500 | |||
| 501 | current_col_number += 1 |
||
| 502 | col = format_cell.get_column_letter(current_col_number) |
||
| 503 | ws[col + str(current_row_number)].font = title_font |
||
| 504 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 505 | ws[col + str(current_row_number)] = round2(base_period_data['total_in_category'], 2) |
||
| 506 | ws[col + str(current_row_number)].border = f_border |
||
| 507 | |||
| 508 | current_col_number += 1 |
||
| 509 | col = format_cell.get_column_letter(current_col_number) |
||
| 510 | |||
| 511 | ws[col + str(current_row_number)].font = title_font |
||
| 512 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 513 | ws[col + str(current_row_number)] = _('Total') |
||
| 514 | ws[col + str(current_row_number)].border = f_border |
||
| 515 | |||
| 516 | current_col_number += 1 |
||
| 517 | col = format_cell.get_column_letter(current_col_number) |
||
| 518 | ws[col + str(current_row_number)].font = title_font |
||
| 519 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 520 | ws[col + str(current_row_number)] = round2(reporting_period_data['total_in_category'], 2) |
||
| 521 | ws[col + str(current_row_number)].border = f_border |
||
| 522 | |||
| 523 | # line |
||
| 524 | line = LineChart() |
||
| 525 | line.title = _('Base Period Carbon Dioxide Emissions') + " / " \ |
||
| 526 | + _('Reporting Period Carbon Dioxide Emissions') + ' - ' \ |
||
| 527 | + report['virtual_meter']['energy_category_name'] + \ |
||
| 528 | " (" + report['virtual_meter']['unit_of_measure'] + ")" |
||
| 529 | labels = Reference(ws, min_col=4, |
||
| 530 | min_row=table_start_row_number + 1, |
||
| 531 | max_row=table_start_row_number + len(reporting_period_timestamps)) |
||
| 532 | base_line_data = Reference(ws, min_col=3, min_row=table_start_row_number, |
||
| 533 | max_row=table_start_row_number + len(reporting_period_timestamps)) |
||
| 534 | reporting_line_data = Reference(ws, min_col=5, |
||
| 535 | min_row=table_start_row_number, |
||
| 536 | max_row=table_start_row_number |
||
| 537 | + len(reporting_period_timestamps)) |
||
| 538 | line.add_data(base_line_data, titles_from_data=True) |
||
| 539 | line.add_data(reporting_line_data, titles_from_data=True) |
||
| 540 | line.set_categories(labels) |
||
| 541 | for j in range(len(line.series)): |
||
| 542 | line.series[j].marker.symbol = "auto" |
||
| 543 | line.series[j].smooth = True |
||
| 544 | line.x_axis.crosses = 'min' |
||
| 545 | line.height = 8.25 |
||
| 546 | line.width = 24 |
||
| 547 | chart_col = 'B' |
||
| 548 | chart_cell = chart_col + str(table_start_draw_flag) |
||
| 549 | ws.add_chart(line, chart_cell) |
||
| 550 | |||
| 551 | current_row_number += 2 |
||
| 552 | |||
| 553 | #################################################################################################################### |
||
| 554 | # table_start_draw_flag is the starting line number of the last line chart in the report period |
||
| 555 | current_sheet_parameters_row_number = table_start_draw_flag + 1 * 6 + 1 |
||
| 556 | if 'parameters' not in report.keys() or \ |
||
| 557 | report['parameters'] is None or \ |
||
| 558 | 'names' not in report['parameters'].keys() or \ |
||
| 559 | report['parameters']['names'] is None or \ |
||
| 560 | len(report['parameters']['names']) == 0 or \ |
||
| 561 | 'timestamps' not in report['parameters'].keys() or \ |
||
| 562 | report['parameters']['timestamps'] is None or \ |
||
| 563 | len(report['parameters']['timestamps']) == 0 or \ |
||
| 564 | 'values' not in report['parameters'].keys() or \ |
||
| 565 | report['parameters']['values'] is None or \ |
||
| 566 | len(report['parameters']['values']) == 0 or \ |
||
| 567 | timestamps_data_all_equal_0(report['parameters']['timestamps']): |
||
| 568 | pass |
||
| 569 | else: |
||
| 570 | |||
| 571 | ################################################################################################################ |
||
| 572 | # new worksheet |
||
| 573 | ################################################################################################################ |
||
| 574 | |||
| 575 | parameters_data = report['parameters'] |
||
| 576 | |||
| 577 | parameters_names_len = len(parameters_data['names']) |
||
| 578 | |||
| 579 | file_name = (re.sub(r'[^A-Z]', '', ws.title)) + '_' |
||
| 580 | parameters_ws = wb.create_sheet(file_name + _('Parameters')) |
||
| 581 | |||
| 582 | parameters_timestamps_data_max_len = \ |
||
| 583 | get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
||
| 584 | |||
| 585 | # Row height |
||
| 586 | parameters_ws.row_dimensions[1].height = 102 |
||
| 587 | for i in range(2, 7 + 1): |
||
| 588 | parameters_ws.row_dimensions[i].height = 42 |
||
| 589 | |||
| 590 | for i in range(8, parameters_timestamps_data_max_len + 10): |
||
| 591 | parameters_ws.row_dimensions[i].height = 60 |
||
| 592 | |||
| 593 | # Col width |
||
| 594 | parameters_ws.column_dimensions['A'].width = 1.5 |
||
| 595 | |||
| 596 | parameters_ws.column_dimensions['B'].width = 25.0 |
||
| 597 | |||
| 598 | for i in range(3, 12 + parameters_names_len * 3): |
||
| 599 | parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
||
| 600 | |||
| 601 | # Img |
||
| 602 | img = Image("excelexporters/myems.png") |
||
| 603 | parameters_ws.add_image(img, 'A1') |
||
| 604 | |||
| 605 | # Title |
||
| 606 | parameters_ws['B3'].alignment = b_r_alignment |
||
| 607 | parameters_ws['B3'] = _('Name') + ':' |
||
| 608 | parameters_ws['C3'].border = b_border |
||
| 609 | parameters_ws['C3'].alignment = b_c_alignment |
||
| 610 | parameters_ws['C3'] = name |
||
| 611 | |||
| 612 | parameters_ws['D3'].alignment = b_r_alignment |
||
| 613 | parameters_ws['D3'] = _('Period Type') + ':' |
||
| 614 | parameters_ws['E3'].border = b_border |
||
| 615 | parameters_ws['E3'].alignment = b_c_alignment |
||
| 616 | parameters_ws['E3'] = period_type |
||
| 617 | |||
| 618 | parameters_ws['B4'].alignment = b_r_alignment |
||
| 619 | parameters_ws['B4'] = _('Reporting Start Datetime') + ':' |
||
| 620 | parameters_ws['C4'].border = b_border |
||
| 621 | parameters_ws['C4'].alignment = b_c_alignment |
||
| 622 | parameters_ws['C4'] = reporting_start_datetime_local |
||
| 623 | |||
| 624 | parameters_ws['D4'].alignment = b_r_alignment |
||
| 625 | parameters_ws['D4'] = _('Reporting End Datetime') + ':' |
||
| 626 | parameters_ws['E4'].border = b_border |
||
| 627 | parameters_ws['E4'].alignment = b_c_alignment |
||
| 628 | parameters_ws['E4'] = reporting_end_datetime_local |
||
| 629 | |||
| 630 | parameters_ws_current_row_number = 6 |
||
| 631 | |||
| 632 | parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
||
| 633 | parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' ' + _('Parameters') |
||
| 634 | |||
| 635 | parameters_ws_current_row_number += 1 |
||
| 636 | |||
| 637 | parameters_table_start_row_number = parameters_ws_current_row_number |
||
| 638 | |||
| 639 | parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
||
| 640 | |||
| 641 | parameters_ws_current_row_number += 1 |
||
| 642 | |||
| 643 | table_current_col_number = 2 |
||
| 644 | |||
| 645 | for i in range(0, parameters_names_len): |
||
| 646 | |||
| 647 | if len(parameters_data['timestamps'][i]) == 0: |
||
| 648 | continue |
||
| 649 | |||
| 650 | col = format_cell.get_column_letter(table_current_col_number) |
||
| 651 | |||
| 652 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
| 653 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
| 654 | |||
| 655 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
| 656 | |||
| 657 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
| 658 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
| 659 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = name_font |
||
| 660 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = c_c_alignment |
||
| 661 | parameters_ws[col + str(parameters_ws_current_row_number - 1)] = parameters_data['names'][i] |
||
| 662 | |||
| 663 | table_current_row_number = parameters_ws_current_row_number |
||
| 664 | |||
| 665 | for j, value in enumerate(list(parameters_data['timestamps'][i])): |
||
| 666 | col = format_cell.get_column_letter(table_current_col_number) |
||
| 667 | |||
| 668 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
| 669 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
| 670 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
| 671 | parameters_ws[col + str(table_current_row_number)] = value |
||
| 672 | |||
| 673 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
| 674 | |||
| 675 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
| 676 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
| 677 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
| 678 | parameters_ws[col + str(table_current_row_number)] = round2(parameters_data['values'][i][j], 2) |
||
| 679 | |||
| 680 | table_current_row_number += 1 |
||
| 681 | |||
| 682 | table_current_col_number = table_current_col_number + 3 |
||
| 683 | |||
| 684 | ################################################################################################################ |
||
| 685 | # parameters chart and parameters table |
||
| 686 | ################################################################################################################ |
||
| 687 | |||
| 688 | ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
||
| 689 | ws['B' + str(current_sheet_parameters_row_number)] = name + ' ' + _('Parameters') |
||
| 690 | |||
| 691 | current_sheet_parameters_row_number += 1 |
||
| 692 | |||
| 693 | chart_start_row_number = current_sheet_parameters_row_number |
||
| 694 | |||
| 695 | col_index = 0 |
||
| 696 | |||
| 697 | for i in range(0, parameters_names_len): |
||
| 698 | |||
| 699 | if len(parameters_data['timestamps'][i]) == 0: |
||
| 700 | continue |
||
| 701 | |||
| 702 | line = LineChart() |
||
| 703 | data_col = 3 + col_index * 3 |
||
| 704 | labels_col = 2 + col_index * 3 |
||
| 705 | col_index += 1 |
||
| 706 | line.title = _('Parameters') + " - " + \ |
||
| 707 | parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
||
| 708 | labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
||
| 709 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
| 710 | line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
||
| 711 | max_row=(len( |
||
| 712 | parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
| 713 | line.add_data(line_data, titles_from_data=True) |
||
| 714 | line.set_categories(labels) |
||
| 715 | line_data = line.series[0] |
||
| 716 | line_data.marker.symbol = "auto" |
||
| 717 | line_data.smooth = True |
||
| 718 | line.x_axis.crosses = 'min' |
||
| 719 | line.height = 8.25 |
||
| 720 | line.width = 24 |
||
| 721 | chart_col = 'B' |
||
| 722 | chart_cell = chart_col + str(chart_start_row_number) |
||
| 723 | chart_start_row_number += 6 |
||
| 724 | ws.add_chart(line, chart_cell) |
||
| 725 | |||
| 726 | current_sheet_parameters_row_number = chart_start_row_number |
||
| 727 | |||
| 728 | current_sheet_parameters_row_number += 1 |
||
| 729 | |||
| 730 | filename = str(uuid.uuid4()) + '.xlsx' |
||
| 731 | wb.save(filename) |
||
| 732 | |||
| 733 | return filename |
||
| 734 | |||
| 772 |