| Conditions | 23 |
| Total Lines | 301 |
| Code Lines | 225 |
| Lines | 301 |
| 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.metercost.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 |
||
| 62 | View Code Duplication | def generate_excel(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type): |
|
| 63 | wb = Workbook() |
||
| 64 | |||
| 65 | # todo |
||
| 66 | ws = wb.active |
||
| 67 | |||
| 68 | # Row height |
||
| 69 | ws.row_dimensions[1].height = 102 |
||
| 70 | for i in range(2, 2000 + 1): |
||
| 71 | ws.row_dimensions[i].height = 42 |
||
| 72 | # for i in range(2, 11 + 1): |
||
| 73 | # ws.row_dimensions[i].height = 30 |
||
| 74 | # |
||
| 75 | # for i in range(12, 43 + 1): |
||
| 76 | # ws.row_dimensions[i].height = 30 |
||
| 77 | |||
| 78 | # Col width |
||
| 79 | ws.column_dimensions['A'].width = 1.5 |
||
| 80 | |||
| 81 | ws.column_dimensions['B'].width = 25.0 |
||
| 82 | |||
| 83 | for i in range(ord('C'), ord('L')): |
||
| 84 | ws.column_dimensions[chr(i)].width = 15.0 |
||
| 85 | |||
| 86 | # Font |
||
| 87 | name_font = Font(name='Constantia', size=15, bold=True) |
||
| 88 | title_font = Font(name='宋体', size=15, bold=True) |
||
| 89 | data_font = Font(name='Franklin Gothic Book', size=11) |
||
| 90 | |||
| 91 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
||
| 92 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
||
| 93 | right=Side(border_style='medium', color='00000000'), |
||
| 94 | bottom=Side(border_style='medium', color='00000000'), |
||
| 95 | top=Side(border_style='medium', color='00000000') |
||
| 96 | ) |
||
| 97 | b_border = Border( |
||
| 98 | bottom=Side(border_style='medium', color='00000000'), |
||
| 99 | ) |
||
| 100 | |||
| 101 | b_c_alignment = Alignment(vertical='bottom', |
||
| 102 | horizontal='center', |
||
| 103 | text_rotation=0, |
||
| 104 | wrap_text=True, |
||
| 105 | shrink_to_fit=False, |
||
| 106 | indent=0) |
||
| 107 | c_c_alignment = Alignment(vertical='center', |
||
| 108 | horizontal='center', |
||
| 109 | text_rotation=0, |
||
| 110 | wrap_text=True, |
||
| 111 | shrink_to_fit=False, |
||
| 112 | indent=0) |
||
| 113 | b_r_alignment = Alignment(vertical='bottom', |
||
| 114 | horizontal='right', |
||
| 115 | text_rotation=0, |
||
| 116 | wrap_text=True, |
||
| 117 | shrink_to_fit=False, |
||
| 118 | indent=0) |
||
| 119 | c_r_alignment = Alignment(vertical='bottom', |
||
| 120 | horizontal='center', |
||
| 121 | text_rotation=0, |
||
| 122 | wrap_text=True, |
||
| 123 | shrink_to_fit=False, |
||
| 124 | indent=0) |
||
| 125 | |||
| 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" not in report.keys() or \ |
||
| 162 | "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
||
| 163 | filename = str(uuid.uuid4()) + '.xlsx' |
||
| 164 | wb.save(filename) |
||
| 165 | |||
| 166 | return filename |
||
| 167 | |||
| 168 | ############################### |
||
| 169 | |||
| 170 | has_cost_data_flag = True |
||
| 171 | |||
| 172 | if "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
||
| 173 | has_cost_data_flag = False |
||
| 174 | |||
| 175 | if has_cost_data_flag: |
||
| 176 | ws['B6'].font = title_font |
||
| 177 | ws['B6'] = name + '报告期成本' |
||
| 178 | |||
| 179 | reporting_period_data = report['reporting_period'] |
||
| 180 | category = report['meter']['energy_category_name'] |
||
| 181 | ca_len = len(category) |
||
| 182 | |||
| 183 | ws.row_dimensions[7].height = 60 |
||
| 184 | ws['B7'].fill = table_fill |
||
| 185 | ws['B7'].border = f_border |
||
| 186 | |||
| 187 | ws['B8'].font = title_font |
||
| 188 | ws['B8'].alignment = c_c_alignment |
||
| 189 | ws['B8'] = '成本' |
||
| 190 | ws['B8'].border = f_border |
||
| 191 | |||
| 192 | ws['B9'].font = title_font |
||
| 193 | ws['B9'].alignment = c_c_alignment |
||
| 194 | ws['B9'] = '环比' |
||
| 195 | ws['B9'].border = f_border |
||
| 196 | |||
| 197 | col = 'B' |
||
| 198 | |||
| 199 | for i in range(0, ca_len): |
||
| 200 | col = chr(ord('C') + i) |
||
| 201 | |||
| 202 | ws[col + '7'].fill = table_fill |
||
| 203 | ws[col + '7'].font = name_font |
||
| 204 | ws[col + '7'].alignment = c_c_alignment |
||
| 205 | ws[col + '7'] = report['meter']['energy_category_name'] + \ |
||
| 206 | " (" + report['meter']['unit_of_measure'] + ")" |
||
| 207 | ws[col + '7'].border = f_border |
||
| 208 | |||
| 209 | ws[col + '8'].font = name_font |
||
| 210 | ws[col + '8'].alignment = c_c_alignment |
||
| 211 | ws[col + '8'] = round(reporting_period_data['total_in_category'], 2) |
||
| 212 | ws[col + '8'].border = f_border |
||
| 213 | |||
| 214 | ws[col + '9'].font = name_font |
||
| 215 | ws[col + '9'].alignment = c_c_alignment |
||
| 216 | ws[col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
||
| 217 | if reporting_period_data['increment_rate'] is not None else "-" |
||
| 218 | ws[col + '9'].border = f_border |
||
| 219 | |||
| 220 | # TCE TCO2E |
||
| 221 | end_col = col |
||
| 222 | # TCE |
||
| 223 | tce_col = chr(ord(end_col) + 1) |
||
| 224 | ws[tce_col + '7'].fill = table_fill |
||
| 225 | ws[tce_col + '7'].font = name_font |
||
| 226 | ws[tce_col + '7'].alignment = c_c_alignment |
||
| 227 | ws[tce_col + '7'] = "吨标准煤 (TCE)" |
||
| 228 | ws[tce_col + '7'].border = f_border |
||
| 229 | |||
| 230 | ws[tce_col + '8'].font = name_font |
||
| 231 | ws[tce_col + '8'].alignment = c_c_alignment |
||
| 232 | ws[tce_col + '8'] = round(reporting_period_data['total_in_kgce'] / 1000, 2) |
||
| 233 | ws[tce_col + '8'].border = f_border |
||
| 234 | |||
| 235 | ws[tce_col + '9'].font = name_font |
||
| 236 | ws[tce_col + '9'].alignment = c_c_alignment |
||
| 237 | ws[tce_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
||
| 238 | if reporting_period_data['increment_rate'] is not None else "-" |
||
| 239 | ws[tce_col + '9'].border = f_border |
||
| 240 | |||
| 241 | # TCO2E |
||
| 242 | tco2e_col = chr(ord(end_col) + 2) |
||
| 243 | ws[tco2e_col + '7'].fill = table_fill |
||
| 244 | ws[tco2e_col + '7'].font = name_font |
||
| 245 | ws[tco2e_col + '7'].alignment = c_c_alignment |
||
| 246 | ws[tco2e_col + '7'] = "吨二氧化碳排放 (TCO2E)" |
||
| 247 | ws[tco2e_col + '7'].border = f_border |
||
| 248 | |||
| 249 | ws[tco2e_col + '8'].font = name_font |
||
| 250 | ws[tco2e_col + '8'].alignment = c_c_alignment |
||
| 251 | ws[tco2e_col + '8'] = round(reporting_period_data['total_in_kgco2e'] / 1000, 2) |
||
| 252 | ws[tco2e_col + '8'].border = f_border |
||
| 253 | |||
| 254 | ws[tco2e_col + '9'].font = name_font |
||
| 255 | ws[tco2e_col + '9'].alignment = c_c_alignment |
||
| 256 | ws[tco2e_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
||
| 257 | if reporting_period_data['increment_rate'] is not None else "-" |
||
| 258 | ws[tco2e_col + '9'].border = f_border |
||
| 259 | |||
| 260 | else: |
||
| 261 | for i in range(6, 9 + 1): |
||
| 262 | ws.rows_dimensions[i].height = 0.1 |
||
| 263 | |||
| 264 | ###################################### |
||
| 265 | |||
| 266 | has_cost_datail_flag = True |
||
| 267 | reporting_period_data = report['reporting_period'] |
||
| 268 | category = report['meter']['energy_category_name'] |
||
| 269 | ca_len = len(category) |
||
| 270 | times = reporting_period_data['timestamps'] |
||
| 271 | |||
| 272 | if "values" not in reporting_period_data.keys() or len(reporting_period_data['values']) == 0: |
||
| 273 | has_cost_datail_flag = False |
||
| 274 | |||
| 275 | if has_cost_datail_flag: |
||
| 276 | ws['B11'].font = title_font |
||
| 277 | ws['B11'] = name + '详细数据' |
||
| 278 | |||
| 279 | ws.row_dimensions[18].height = 60 |
||
| 280 | ws['B18'].fill = table_fill |
||
| 281 | ws['B18'].font = title_font |
||
| 282 | ws['B18'].border = f_border |
||
| 283 | ws['B18'].alignment = c_c_alignment |
||
| 284 | ws['B18'] = '日期时间' |
||
| 285 | time = times |
||
| 286 | has_data = False |
||
| 287 | max_row = 0 |
||
| 288 | if len(time) > 0: |
||
| 289 | has_data = True |
||
| 290 | max_row = 18 + len(time) |
||
| 291 | |||
| 292 | if has_data: |
||
| 293 | |||
| 294 | end_data_row_number = 19 |
||
| 295 | |||
| 296 | for i in range(0, len(time)): |
||
| 297 | col = 'B' |
||
| 298 | end_data_row_number = 19 + i |
||
| 299 | row = str(end_data_row_number) |
||
| 300 | |||
| 301 | ws[col + row].font = title_font |
||
| 302 | ws[col + row].alignment = c_c_alignment |
||
| 303 | ws[col + row] = time[i] |
||
| 304 | ws[col + row].border = f_border |
||
| 305 | |||
| 306 | ws['B' + str(end_data_row_number + 1)].font = title_font |
||
| 307 | ws['B' + str(end_data_row_number + 1)].alignment = c_c_alignment |
||
| 308 | ws['B' + str(end_data_row_number + 1)] = '总计' |
||
| 309 | ws['B' + str(end_data_row_number + 1)].border = f_border |
||
| 310 | |||
| 311 | for i in range(0, ca_len): |
||
| 312 | |||
| 313 | col = chr(ord('C') + i) |
||
| 314 | |||
| 315 | ws[col + '18'].fill = table_fill |
||
| 316 | ws[col + '18'].font = title_font |
||
| 317 | ws[col + '18'].alignment = c_c_alignment |
||
| 318 | ws[col + '18'] = report['meter']['energy_category_name'] + \ |
||
| 319 | " (" + report['meter']['unit_of_measure'] + ")" |
||
| 320 | ws[col + '18'].border = f_border |
||
| 321 | |||
| 322 | time = times |
||
| 323 | time_len = len(time) |
||
| 324 | |||
| 325 | for j in range(0, time_len): |
||
| 326 | row = str(19 + j) |
||
| 327 | |||
| 328 | ws[col + row].font = title_font |
||
| 329 | ws[col + row].alignment = c_c_alignment |
||
| 330 | ws[col + row] = round(reporting_period_data['values'][j], 2) |
||
| 331 | ws[col + row].border = f_border |
||
| 332 | |||
| 333 | ws[col + str(end_data_row_number + 1)].font = title_font |
||
| 334 | ws[col + str(end_data_row_number + 1)].alignment = c_c_alignment |
||
| 335 | ws[col + str(end_data_row_number + 1)] = round(reporting_period_data['total_in_category'], 2) |
||
| 336 | ws[col + str(end_data_row_number + 1)].border = f_border |
||
| 337 | |||
| 338 | line = LineChart() |
||
| 339 | line.title = '报告期成本 - ' + report['meter']['energy_category_name'] + \ |
||
| 340 | " (" + report['meter']['unit_of_measure'] + ")" |
||
| 341 | line_data = Reference(ws, min_col=3, min_row=18, max_row=max_row) |
||
| 342 | line.series.append(Series(line_data, title_from_data=True)) |
||
| 343 | labels = Reference(ws, min_col=2, min_row=19, max_row=max_row) |
||
| 344 | line.set_categories(labels) |
||
| 345 | line_data = line.series[0] |
||
| 346 | line_data.marker.symbol = "circle" |
||
| 347 | line_data.smooth = True |
||
| 348 | line.x_axis.crosses = 'min' |
||
| 349 | line.dLbls = DataLabelList() |
||
| 350 | line.dLbls.dLblPos = 't' |
||
| 351 | line.dLbls.showVal = True |
||
| 352 | line.height = 8.25 |
||
| 353 | line.width = 24 |
||
| 354 | ws.add_chart(line, "B12") |
||
| 355 | else: |
||
| 356 | for i in range(11, 43 + 1): |
||
| 357 | ws.row_dimensions[i].height = 0.0 |
||
| 358 | |||
| 359 | filename = str(uuid.uuid4()) + '.xlsx' |
||
| 360 | wb.save(filename) |
||
| 361 | |||
| 362 | return filename |
||
| 363 |