| Conditions | 24 |
| Total Lines | 289 |
| Code Lines | 216 |
| Lines | 86 |
| Ratio | 29.76 % |
| 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.offlinemetercost.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 |
||
| 61 | def generate_excel(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type): |
||
| 62 | |||
| 63 | wb = Workbook() |
||
| 64 | |||
| 65 | # todo |
||
| 66 | ws = wb.active |
||
| 67 | |||
| 68 | # Row height |
||
| 69 | ws.row_dimensions[1].height = 118 |
||
| 70 | for i in range(2, 11 + 1): |
||
| 71 | ws.row_dimensions[i].height = 30 |
||
| 72 | |||
| 73 | for i in range(12, 43 + 1): |
||
| 74 | ws.row_dimensions[i].height = 30 |
||
| 75 | |||
| 76 | # Col width |
||
| 77 | ws.column_dimensions['A'].width = 1.5 |
||
| 78 | |||
| 79 | for i in range(ord('B'), ord('I')): |
||
| 80 | ws.column_dimensions[chr(i)].width = 15.0 |
||
| 81 | |||
| 82 | # Font |
||
| 83 | name_font = Font(name='Constantia', size=15, bold=True) |
||
| 84 | title_font = Font(name='宋体', size=15, bold=True) |
||
| 85 | data_font = Font(name='Franklin Gothic Book', size=11) |
||
| 86 | |||
| 87 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
||
| 88 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
||
| 89 | right=Side(border_style='medium', color='00000000'), |
||
| 90 | bottom=Side(border_style='medium', color='00000000'), |
||
| 91 | top=Side(border_style='medium', color='00000000') |
||
| 92 | ) |
||
| 93 | b_border = Border( |
||
| 94 | bottom=Side(border_style='medium', color='00000000'), |
||
| 95 | ) |
||
| 96 | |||
| 97 | b_c_alignment = Alignment(vertical='bottom', |
||
| 98 | horizontal='center', |
||
| 99 | text_rotation=0, |
||
| 100 | wrap_text=False, |
||
| 101 | shrink_to_fit=False, |
||
| 102 | indent=0) |
||
| 103 | c_c_alignment = Alignment(vertical='center', |
||
| 104 | horizontal='center', |
||
| 105 | text_rotation=0, |
||
| 106 | wrap_text=False, |
||
| 107 | shrink_to_fit=False, |
||
| 108 | indent=0) |
||
| 109 | b_r_alignment = Alignment(vertical='bottom', |
||
| 110 | horizontal='right', |
||
| 111 | text_rotation=0, |
||
| 112 | wrap_text=False, |
||
| 113 | shrink_to_fit=False, |
||
| 114 | indent=0) |
||
| 115 | c_r_alignment = Alignment(vertical='bottom', |
||
| 116 | horizontal='center', |
||
| 117 | text_rotation=0, |
||
| 118 | wrap_text=False, |
||
| 119 | shrink_to_fit=False, |
||
| 120 | indent=0) |
||
| 121 | |||
| 122 | # Img |
||
| 123 | img = Image("excelexporters/myems.png") |
||
| 124 | ws.add_image(img, 'B1') |
||
| 125 | |||
| 126 | # Title |
||
| 127 | ws['B3'].font = name_font |
||
| 128 | ws['B3'].alignment = b_r_alignment |
||
| 129 | ws['B3'] = 'Name:' |
||
| 130 | ws['C3'].border = b_border |
||
| 131 | ws['C3'].alignment = b_c_alignment |
||
| 132 | ws['C3'].font = name_font |
||
| 133 | ws['C3'] = name |
||
| 134 | |||
| 135 | ws['D3'].font = name_font |
||
| 136 | ws['D3'].alignment = b_r_alignment |
||
| 137 | ws['D3'] = 'Period:' |
||
| 138 | ws['E3'].border = b_border |
||
| 139 | ws['E3'].alignment = b_c_alignment |
||
| 140 | ws['E3'].font = name_font |
||
| 141 | ws['E3'] = period_type |
||
| 142 | |||
| 143 | ws['F3'].font = name_font |
||
| 144 | ws['F3'].alignment = b_r_alignment |
||
| 145 | ws['F3'] = 'Date:' |
||
| 146 | ws.merge_cells("G3:H3") |
||
| 147 | ws['G3'].border = b_border |
||
| 148 | ws['G3'].alignment = b_c_alignment |
||
| 149 | ws['G3'].font = name_font |
||
| 150 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
||
| 151 | |||
| 152 | if "reporting_period" not in report.keys() or \ |
||
| 153 | "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
||
| 154 | filename = str(uuid.uuid4()) + '.xlsx' |
||
| 155 | wb.save(filename) |
||
| 156 | |||
| 157 | return filename |
||
| 158 | |||
| 159 | ############################### |
||
| 160 | |||
| 161 | has_cost_data_flag = True |
||
| 162 | |||
| 163 | if "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
||
| 164 | has_cost_data_flag = False |
||
| 165 | |||
| 166 | View Code Duplication | if has_cost_data_flag: |
|
| 167 | ws['B6'].font = title_font |
||
| 168 | ws['B6'] = name + '报告期成本' |
||
| 169 | |||
| 170 | reporting_period_data = report['reporting_period'] |
||
| 171 | category = report['offline_meter']['energy_category_name'] |
||
| 172 | ca_len = len(category) |
||
| 173 | |||
| 174 | ws['B7'].fill = table_fill |
||
| 175 | |||
| 176 | ws['B8'].font = title_font |
||
| 177 | ws['B8'].alignment = c_c_alignment |
||
| 178 | ws['B8'] = '成本' |
||
| 179 | ws['B8'].border = f_border |
||
| 180 | |||
| 181 | ws['B9'].font = title_font |
||
| 182 | ws['B9'].alignment = c_c_alignment |
||
| 183 | ws['B9'] = '环比' |
||
| 184 | ws['B9'].border = f_border |
||
| 185 | |||
| 186 | col = 'B' |
||
| 187 | |||
| 188 | for i in range(0, ca_len): |
||
| 189 | col = chr(ord('C')+i) |
||
| 190 | |||
| 191 | ws[col + '7'].fill = table_fill |
||
| 192 | ws[col + '7'].font = name_font |
||
| 193 | ws[col + '7'].alignment = c_c_alignment |
||
| 194 | ws[col + '7'] = report['offline_meter']['energy_category_name'] + \ |
||
| 195 | " (" + report['offline_meter']['unit_of_measure'] + ")" |
||
| 196 | ws[col + '7'].border = f_border |
||
| 197 | |||
| 198 | ws[col + '8'].font = name_font |
||
| 199 | ws[col + '8'].alignment = c_c_alignment |
||
| 200 | ws[col + '8'] = round(reporting_period_data['total_in_category'], 0) |
||
| 201 | ws[col + '8'].border = f_border |
||
| 202 | |||
| 203 | ws[col + '9'].font = name_font |
||
| 204 | ws[col + '9'].alignment = c_c_alignment |
||
| 205 | ws[col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
||
| 206 | if reporting_period_data['increment_rate'] is not None else "-" |
||
| 207 | ws[col + '9'].border = f_border |
||
| 208 | |||
| 209 | # TCE TCO2E |
||
| 210 | end_col = col |
||
| 211 | # TCE |
||
| 212 | tce_col = chr(ord(end_col) + 1) |
||
| 213 | ws[tce_col + '7'].fill = table_fill |
||
| 214 | ws[tce_col + '7'].font = name_font |
||
| 215 | ws[tce_col + '7'].alignment = c_c_alignment |
||
| 216 | ws[tce_col + '7'] = "TCE" |
||
| 217 | ws[tce_col + '7'].border = f_border |
||
| 218 | |||
| 219 | ws[tce_col + '8'].font = name_font |
||
| 220 | ws[tce_col + '8'].alignment = c_c_alignment |
||
| 221 | ws[tce_col + '8'] = round(reporting_period_data['total_in_kgce'], 0) |
||
| 222 | ws[tce_col + '8'].border = f_border |
||
| 223 | |||
| 224 | ws[tce_col + '9'].font = name_font |
||
| 225 | ws[tce_col + '9'].alignment = c_c_alignment |
||
| 226 | ws[tce_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
||
| 227 | if reporting_period_data['increment_rate'] is not None else "-" |
||
| 228 | ws[tce_col + '9'].border = f_border |
||
| 229 | |||
| 230 | # TCO2E |
||
| 231 | tco2e_col = chr(ord(end_col) + 2) |
||
| 232 | ws[tco2e_col + '7'].fill = table_fill |
||
| 233 | ws[tco2e_col + '7'].font = name_font |
||
| 234 | ws[tco2e_col + '7'].alignment = c_c_alignment |
||
| 235 | ws[tco2e_col + '7'] = "TCO2E" |
||
| 236 | ws[tco2e_col + '7'].border = f_border |
||
| 237 | |||
| 238 | ws[tco2e_col + '8'].font = name_font |
||
| 239 | ws[tco2e_col + '8'].alignment = c_c_alignment |
||
| 240 | ws[tco2e_col + '8'] = round(reporting_period_data['total_in_kgco2e'], 0) |
||
| 241 | ws[tco2e_col + '8'].border = f_border |
||
| 242 | |||
| 243 | ws[tco2e_col + '9'].font = name_font |
||
| 244 | ws[tco2e_col + '9'].alignment = c_c_alignment |
||
| 245 | ws[tco2e_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
||
| 246 | if reporting_period_data['increment_rate'] is not None else "-" |
||
| 247 | ws[tco2e_col + '9'].border = f_border |
||
| 248 | |||
| 249 | else: |
||
| 250 | for i in range(6, 9+1): |
||
| 251 | ws.rows_dimensions[i].height = 0.1 |
||
| 252 | |||
| 253 | ###################################### |
||
| 254 | |||
| 255 | has_cost_datail_flag = True |
||
| 256 | reporting_period_data = report['reporting_period'] |
||
| 257 | category = report['offline_meter']['energy_category_name'] |
||
| 258 | ca_len = len(category) |
||
| 259 | times = reporting_period_data['timestamps'] |
||
| 260 | |||
| 261 | if "values" not in reporting_period_data.keys() or len(reporting_period_data['values']) == 0: |
||
| 262 | has_cost_datail_flag = False |
||
| 263 | |||
| 264 | if has_cost_datail_flag: |
||
| 265 | ws['B11'].font = title_font |
||
| 266 | ws['B11'] = name + '详细数据' |
||
| 267 | |||
| 268 | ws['B18'].fill = table_fill |
||
| 269 | ws['B18'].font = title_font |
||
| 270 | ws['B18'].border = f_border |
||
| 271 | ws['B18'].alignment = c_c_alignment |
||
| 272 | ws['B18'] = '日期时间' |
||
| 273 | time = times |
||
| 274 | has_data = False |
||
| 275 | max_row = 0 |
||
| 276 | if len(time) > 0: |
||
| 277 | has_data = True |
||
| 278 | max_row = 18 + len(time) |
||
| 279 | |||
| 280 | if has_data: |
||
| 281 | |||
| 282 | end_data_flag = 19 |
||
| 283 | |||
| 284 | for i in range(0, len(time)): |
||
| 285 | col = 'B' |
||
| 286 | end_data_flag = 19 + i |
||
| 287 | row = str(end_data_flag) |
||
| 288 | |||
| 289 | ws[col + row].font = title_font |
||
| 290 | ws[col + row].alignment = c_c_alignment |
||
| 291 | ws[col + row] = time[i] |
||
| 292 | ws[col + row].border = f_border |
||
| 293 | |||
| 294 | ws['B' + str(end_data_flag + 1)].font = title_font |
||
| 295 | ws['B' + str(end_data_flag + 1)].alignment = c_c_alignment |
||
| 296 | ws['B' + str(end_data_flag + 1)] = '总计' |
||
| 297 | ws['B' + str(end_data_flag + 1)].border = f_border |
||
| 298 | |||
| 299 | bar = BarChart() |
||
| 300 | |||
| 301 | for i in range(0, ca_len): |
||
| 302 | |||
| 303 | col = chr(ord('C') + i) |
||
| 304 | |||
| 305 | ws[col + '18'].fill = table_fill |
||
| 306 | ws[col + '18'].font = title_font |
||
| 307 | ws[col + '18'].alignment = c_c_alignment |
||
| 308 | ws[col + '18'] = report['offline_meter']['energy_category_name'] + \ |
||
| 309 | " (" + report['offline_meter']['unit_of_measure'] + ")" |
||
| 310 | ws[col + '18'].border = f_border |
||
| 311 | |||
| 312 | time = times |
||
| 313 | time_len = len(time) |
||
| 314 | |||
| 315 | sum_value = 0 |
||
| 316 | |||
| 317 | for j in range(0, time_len): |
||
| 318 | row = str(19 + j) |
||
| 319 | |||
| 320 | ws[col + row].font = title_font |
||
| 321 | ws[col + row].alignment = c_c_alignment |
||
| 322 | value = round(reporting_period_data['values'][j], 0) |
||
| 323 | sum_value += value |
||
| 324 | ws[col + row] = value |
||
| 325 | ws[col + row].border = f_border |
||
| 326 | |||
| 327 | ws[col + str(end_data_flag + 1)].font = title_font |
||
| 328 | ws[col + str(end_data_flag + 1)].alignment = c_c_alignment |
||
| 329 | ws[col + str(end_data_flag + 1)] = sum_value |
||
| 330 | ws[col + str(end_data_flag + 1)].border = f_border |
||
| 331 | |||
| 332 | bar_data = Reference(ws, min_col=3 + i, min_row=18, max_row=max_row) |
||
| 333 | bar.series.append(Series(bar_data, title_from_data=True)) |
||
| 334 | |||
| 335 | labels = Reference(ws, min_col=2, min_row=19, max_row=max_row) |
||
| 336 | bar.set_categories(labels) |
||
| 337 | bar.dLbls = DataLabelList() |
||
| 338 | bar.dLbls.showVal = True |
||
| 339 | bar.height = 5.25 |
||
| 340 | bar.width = len(time) |
||
| 341 | ws.add_chart(bar, "B12") |
||
| 342 | else: |
||
| 343 | for i in range(11, 43 + 1): |
||
| 344 | ws.row_dimensions[i].height = 0.0 |
||
| 345 | |||
| 346 | filename = str(uuid.uuid4()) + '.xlsx' |
||
| 347 | wb.save(filename) |
||
| 348 | |||
| 349 | return filename |
||
| 350 |