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