Conditions | 53 |
Total Lines | 570 |
Code Lines | 435 |
Lines | 349 |
Ratio | 61.23 % |
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.spacesaving.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 | wb = Workbook() |
||
70 | ws = wb.active |
||
71 | |||
72 | # Row height |
||
73 | ws.row_dimensions[1].height = 118 |
||
74 | for i in range(2, 2000 + 1): |
||
75 | ws.row_dimensions[i].height = 30 |
||
76 | |||
77 | # Col width |
||
78 | ws.column_dimensions['A'].width = 1.5 |
||
79 | |||
80 | ws.column_dimensions['B'].width = 25.0 |
||
81 | |||
82 | for i in range(ord('C'), ord('I')): |
||
83 | ws.column_dimensions[chr(i)].width = 25.0 |
||
84 | |||
85 | # Font |
||
86 | name_font = Font(name='Constantia', size=15, bold=True) |
||
87 | name_small_font = Font(name='Constantia', size=10, bold=True) |
||
88 | title_font = Font(name='宋体', size=15, bold=True) |
||
89 | title_small_font = Font(name='宋体', size=10, 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=False, |
||
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=False, |
||
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=False, |
||
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=False, |
||
124 | shrink_to_fit=False, |
||
125 | indent=0) |
||
126 | # Img |
||
127 | img = Image("excelexporters/myems.png") |
||
128 | # img = Image("myems.png") |
||
129 | ws.add_image(img, 'B1') |
||
130 | |||
131 | # Title |
||
132 | ws['B3'].font = name_font |
||
133 | ws['B3'].alignment = b_r_alignment |
||
134 | ws['B3'] = 'Name:' |
||
135 | ws['C3'].border = b_border |
||
136 | ws['C3'].alignment = b_c_alignment |
||
137 | ws['C3'].font = name_font |
||
138 | ws['C3'] = name |
||
139 | |||
140 | ws['D3'].font = name_font |
||
141 | ws['D3'].alignment = b_r_alignment |
||
142 | ws['D3'] = 'Period:' |
||
143 | ws['E3'].border = b_border |
||
144 | ws['E3'].alignment = b_c_alignment |
||
145 | ws['E3'].font = name_font |
||
146 | ws['E3'] = period_type |
||
147 | |||
148 | ws['F3'].font = name_font |
||
149 | ws['F3'].alignment = b_r_alignment |
||
150 | ws['F3'] = 'Date:' |
||
151 | ws.merge_cells("G3:J3") |
||
152 | for i in range(ord('G'), ord('K')): |
||
153 | ws[chr(i) + '3'].border = b_border |
||
154 | ws['G3'].alignment = b_c_alignment |
||
155 | ws['G3'].font = name_font |
||
156 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
||
157 | |||
158 | if "reporting_period" not in report.keys() or \ |
||
159 | "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
||
160 | filename = str(uuid.uuid4()) + '.xlsx' |
||
161 | wb.save(filename) |
||
162 | |||
163 | return filename |
||
164 | |||
165 | ################################## |
||
166 | |||
167 | current_row_number = 6 |
||
168 | |||
169 | reporting_period_data = report['reporting_period'] |
||
170 | |||
171 | has_names_data_flag = True |
||
172 | |||
173 | if "names" not in reporting_period_data.keys() or \ |
||
174 | reporting_period_data['names'] is None or \ |
||
175 | len(reporting_period_data['names']) == 0: |
||
176 | has_names_data_flag = False |
||
177 | |||
178 | View Code Duplication | if has_names_data_flag: |
|
179 | ws['B' + str(current_row_number)].font = title_font |
||
180 | ws['B' + str(current_row_number)] = name + ' 报告期节约' |
||
181 | |||
182 | current_row_number += 1 |
||
183 | |||
184 | category = reporting_period_data['names'] |
||
185 | ca_len = len(category) |
||
186 | |||
187 | ws['B' + str(current_row_number)].fill = table_fill |
||
188 | |||
189 | col = 'C' |
||
190 | |||
191 | for i in range(0, ca_len): |
||
192 | ws[col + str(current_row_number)].fill = table_fill |
||
193 | ws[col + str(current_row_number)].font = name_small_font |
||
194 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
195 | ws[col + str(current_row_number)].border = f_border |
||
196 | ws[col + str(current_row_number)] = \ |
||
197 | reporting_period_data['names'][i] + " (基线-实际) (" + reporting_period_data['units'][i] + ")" |
||
198 | |||
199 | col = chr(ord(col) + 1) |
||
200 | |||
201 | ws[col + str(current_row_number)].fill = table_fill |
||
202 | ws[col + str(current_row_number)].font = name_small_font |
||
203 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
204 | ws[col + str(current_row_number)].border = f_border |
||
205 | ws[col + str(current_row_number)] = '吨标准煤 (基线-实际) (TCE)' |
||
206 | |||
207 | col = chr(ord(col) + 1) |
||
208 | |||
209 | ws[col + str(current_row_number)].fill = table_fill |
||
210 | ws[col + str(current_row_number)].font = name_small_font |
||
211 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
212 | ws[col + str(current_row_number)].border = f_border |
||
213 | ws[col + str(current_row_number)] = '吨二氧化碳排放 (基线-实际) (TCO2E)' |
||
214 | |||
215 | col = chr(ord(col) + 1) |
||
216 | |||
217 | current_row_number += 1 |
||
218 | |||
219 | ws['B' + str(current_row_number)].font = title_font |
||
220 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
221 | ws['B' + str(current_row_number)].border = f_border |
||
222 | ws['B' + str(current_row_number)] = '节约' |
||
223 | |||
224 | col = 'C' |
||
225 | |||
226 | for i in range(0, ca_len): |
||
227 | ws[col + str(current_row_number)].font = name_font |
||
228 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
229 | ws[col + str(current_row_number)].border = f_border |
||
230 | ws[col + str(current_row_number)] = round(reporting_period_data['subtotals_saving'][i], 2) |
||
231 | |||
232 | col = chr(ord(col) + 1) |
||
233 | |||
234 | ws[col + str(current_row_number)].font = name_font |
||
235 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
236 | ws[col + str(current_row_number)].border = f_border |
||
237 | ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgce_saving'], 2) |
||
238 | |||
239 | col = chr(ord(col) + 1) |
||
240 | |||
241 | ws[col + str(current_row_number)].font = name_font |
||
242 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
243 | ws[col + str(current_row_number)].border = f_border |
||
244 | ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgco2e_saving'], 2) |
||
245 | |||
246 | col = chr(ord(col) + 1) |
||
247 | |||
248 | current_row_number += 1 |
||
249 | |||
250 | ws['B' + str(current_row_number)].font = title_font |
||
251 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
252 | ws['B' + str(current_row_number)].border = f_border |
||
253 | ws['B' + str(current_row_number)] = '单位面积值' |
||
254 | |||
255 | col = 'C' |
||
256 | |||
257 | for i in range(0, ca_len): |
||
258 | ws[col + str(current_row_number)].font = name_font |
||
259 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
260 | ws[col + str(current_row_number)].border = f_border |
||
261 | ws[col + str(current_row_number)] = round(reporting_period_data['subtotals_per_unit_area_saving'][i], 2) |
||
262 | |||
263 | col = chr(ord(col) + 1) |
||
264 | |||
265 | ws[col + str(current_row_number)].font = name_font |
||
266 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
267 | ws[col + str(current_row_number)].border = f_border |
||
268 | ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgco2e_per_unit_area_saving'], 2) |
||
269 | |||
270 | col = chr(ord(col) + 1) |
||
271 | |||
272 | ws[col + str(current_row_number)].font = name_font |
||
273 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
274 | ws[col + str(current_row_number)].border = f_border |
||
275 | ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgce_per_unit_area_saving'], 2) |
||
276 | |||
277 | col = chr(ord(col) + 1) |
||
278 | |||
279 | current_row_number += 1 |
||
280 | |||
281 | ws['B' + str(current_row_number)].font = title_font |
||
282 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
283 | ws['B' + str(current_row_number)].border = f_border |
||
284 | ws['B' + str(current_row_number)] = '环比' |
||
285 | |||
286 | col = 'C' |
||
287 | |||
288 | for i in range(0, ca_len): |
||
289 | ws[col + str(current_row_number)].font = name_font |
||
290 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
291 | ws[col + str(current_row_number)].border = f_border |
||
292 | ws[col + str(current_row_number)] = str( |
||
293 | round(reporting_period_data['increment_rates_saving'][i] * 100, 2)) + '%' \ |
||
294 | if reporting_period_data['increment_rates_saving'][i] is not None else '-' |
||
295 | |||
296 | col = chr(ord(col) + 1) |
||
297 | |||
298 | ws[col + str(current_row_number)].font = name_font |
||
299 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
300 | ws[col + str(current_row_number)].border = f_border |
||
301 | ws[col + str(current_row_number)] = str( |
||
302 | round(reporting_period_data['increment_rate_in_kgce_saving'] * 100, 2)) + '%' \ |
||
303 | if reporting_period_data['increment_rate_in_kgce_saving'] is not None else '-' |
||
304 | |||
305 | col = chr(ord(col) + 1) |
||
306 | |||
307 | ws[col + str(current_row_number)].font = name_font |
||
308 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
309 | ws[col + str(current_row_number)].border = f_border |
||
310 | ws[col + str(current_row_number)] = str( |
||
311 | round(reporting_period_data['increment_rate_in_kgco2e_saving'] * 100, 2)) + '%' \ |
||
312 | if reporting_period_data['increment_rate_in_kgco2e_saving'] is not None else '-' |
||
313 | |||
314 | col = chr(ord(col) + 1) |
||
315 | |||
316 | current_row_number += 2 |
||
317 | |||
318 | ws['B' + str(current_row_number)].font = title_font |
||
319 | ws['B' + str(current_row_number)] = name + ' 吨标准煤(TCE)占比' |
||
320 | |||
321 | current_row_number += 1 |
||
322 | table_start_row_number = current_row_number |
||
323 | chart_start_row_number = current_row_number |
||
324 | |||
325 | ws['B' + str(current_row_number)].fill = table_fill |
||
326 | |||
327 | ws['C' + str(current_row_number)].fill = table_fill |
||
328 | ws['C' + str(current_row_number)].font = name_small_font |
||
329 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
330 | ws['C' + str(current_row_number)].border = f_border |
||
331 | ws['C' + str(current_row_number)] = '吨标准煤(TCE)占比' |
||
332 | |||
333 | current_row_number += 1 |
||
334 | |||
335 | for i in range(0, ca_len): |
||
336 | ws['B' + str(current_row_number)].font = title_font |
||
337 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
338 | ws['B' + str(current_row_number)].border = f_border |
||
339 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
||
340 | |||
341 | ws['C' + str(current_row_number)].font = name_font |
||
342 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
343 | ws['C' + str(current_row_number)].border = f_border |
||
344 | ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals_in_kgce_saving'][i], 2) |
||
345 | |||
346 | current_row_number += 1 |
||
347 | |||
348 | table_end_row_number = current_row_number - 1 |
||
349 | |||
350 | if ca_len < 4: |
||
351 | current_row_number = current_row_number - ca_len + 4 |
||
352 | |||
353 | current_row_number += 1 |
||
354 | |||
355 | pie = PieChart() |
||
356 | pie.title = '吨标准煤(TCE)占比' |
||
357 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
358 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
||
359 | pie.add_data(pie_data, titles_from_data=True) |
||
360 | pie.set_categories(labels) |
||
361 | pie.height = 5.25 |
||
362 | pie.width = 9 |
||
363 | s1 = pie.series[0] |
||
364 | s1.dLbls = DataLabelList() |
||
365 | s1.dLbls.showCatName = False |
||
366 | s1.dLbls.showVal = True |
||
367 | s1.dLbls.showPercent = True |
||
368 | ws.add_chart(pie, 'D' + str(chart_start_row_number)) |
||
369 | |||
370 | ws['B' + str(current_row_number)].font = title_font |
||
371 | ws['B' + str(current_row_number)] = name + ' 吨二氧化碳排放(TCO2E)占比' |
||
372 | |||
373 | current_row_number += 1 |
||
374 | table_start_row_number = current_row_number |
||
375 | chart_start_row_number = current_row_number |
||
376 | |||
377 | ws['B' + str(current_row_number)].fill = table_fill |
||
378 | |||
379 | ws['C' + str(current_row_number)].fill = table_fill |
||
380 | ws['C' + str(current_row_number)].font = name_small_font |
||
381 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
382 | ws['C' + str(current_row_number)].border = f_border |
||
383 | ws['C' + str(current_row_number)] = '吨二氧化碳排放(TCO2E)占比' |
||
384 | |||
385 | current_row_number += 1 |
||
386 | |||
387 | for i in range(0, ca_len): |
||
388 | ws['B' + str(current_row_number)].font = title_font |
||
389 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
390 | ws['B' + str(current_row_number)].border = f_border |
||
391 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
||
392 | |||
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)] = round(reporting_period_data['subtotals_in_kgco2e_saving'][i], 2) |
||
397 | |||
398 | current_row_number += 1 |
||
399 | |||
400 | table_end_row_number = current_row_number - 1 |
||
401 | |||
402 | if ca_len < 4: |
||
403 | current_row_number = current_row_number - ca_len + 4 |
||
404 | |||
405 | current_row_number += 1 |
||
406 | |||
407 | pie = PieChart() |
||
408 | pie.title = '吨二氧化碳排放(TCO2E)占比' |
||
409 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
410 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
||
411 | pie.add_data(pie_data, titles_from_data=True) |
||
412 | pie.set_categories(labels) |
||
413 | pie.height = 5.25 |
||
414 | pie.width = 9 |
||
415 | s1 = pie.series[0] |
||
416 | s1.dLbls = DataLabelList() |
||
417 | s1.dLbls.showCatName = False |
||
418 | s1.dLbls.showVal = True |
||
419 | s1.dLbls.showPercent = True |
||
420 | ws.add_chart(pie, 'D' + str(chart_start_row_number)) |
||
421 | |||
422 | ############################################# |
||
423 | |||
424 | has_child_space_data_flag = True |
||
425 | |||
426 | if 'child_space' not in report.keys() or \ |
||
427 | report['child_space'] is None or \ |
||
428 | 'energy_category_names' not in report['child_space'].keys() or \ |
||
429 | report['child_space']['energy_category_names'] is None or \ |
||
430 | len(report['child_space']['energy_category_names']) == 0: |
||
431 | has_child_space_data_flag = False |
||
432 | |||
433 | if has_child_space_data_flag: |
||
434 | child_space_data = report['child_space'] |
||
435 | ca_len = len(child_space_data['energy_category_names']) |
||
436 | |||
437 | ws['B' + str(current_row_number)].font = title_font |
||
438 | ws['B' + str(current_row_number)] = name + ' 子空间数据' |
||
439 | |||
440 | current_row_number += 1 |
||
441 | table_start_row_number = current_row_number |
||
442 | |||
443 | ws['B' + str(current_row_number)].fill = table_fill |
||
444 | ws['B' + str(current_row_number)].font = name_font |
||
445 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
446 | ws['B' + str(current_row_number)].border = f_border |
||
447 | ws['B' + str(current_row_number)] = '子空间' |
||
448 | |||
449 | col = 'C' |
||
450 | |||
451 | for i in range(0, ca_len): |
||
452 | ws[col + str(current_row_number)].fill = table_fill |
||
453 | ws[col + str(current_row_number)].font = name_font |
||
454 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
455 | ws[col + str(current_row_number)].border = f_border |
||
456 | ws[col + str(current_row_number)] = \ |
||
457 | child_space_data['energy_category_names'][i] + " (" + child_space_data['units'][i] + ")" |
||
458 | col = chr(ord(col) + 1) |
||
459 | |||
460 | current_row_number += 1 |
||
461 | ca_child_len = len(child_space_data['child_space_names_array'][0]) |
||
462 | |||
463 | for i in range(0, ca_child_len): |
||
464 | ws['B' + str(current_row_number)].font = title_font |
||
465 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
466 | ws['B' + str(current_row_number)].border = f_border |
||
467 | ws['B' + str(current_row_number)] = child_space_data['child_space_names_array'][0][i] |
||
468 | current_row_number += 1 |
||
469 | |||
470 | current_row_number -= ca_child_len |
||
471 | |||
472 | for i in range(0, ca_child_len): |
||
473 | col = 'C' |
||
474 | for j in range(0, ca_len): |
||
475 | ws[col + str(current_row_number)].font = name_font |
||
476 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
477 | ws[col + str(current_row_number)].border = f_border |
||
478 | ws[col + str(current_row_number)] = child_space_data['subtotals_saving_array'][j][i] |
||
479 | col = chr(ord(col) + 1) |
||
480 | |||
481 | current_row_number += 1 |
||
482 | |||
483 | table_end_row_number = current_row_number - 1 |
||
484 | |||
485 | col = 'B' |
||
486 | |||
487 | for i in range(0, ca_len): |
||
488 | pie = PieChart() |
||
489 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
490 | pie_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number) |
||
491 | pie.add_data(pie_data, titles_from_data=True) |
||
492 | pie.set_categories(labels) |
||
493 | pie.title = reporting_period_data['names'][i] + " (" + \ |
||
494 | reporting_period_data['units'][i] + ")" |
||
495 | pie.height = 5.25 |
||
496 | pie.width = 9 |
||
497 | s1 = pie.series[0] |
||
498 | s1.dLbls = DataLabelList() |
||
499 | s1.dLbls.showCatName = False |
||
500 | s1.dLbls.showVal = True |
||
501 | s1.dLbls.showPercent = True |
||
502 | ws.add_chart(pie, col + str(current_row_number)) |
||
503 | col = chr(ord(col) + 2) |
||
504 | |||
505 | current_row_number += 6 |
||
506 | |||
507 | ################################ |
||
508 | |||
509 | has_values_saving_data = True |
||
510 | has_timestamps_data = True |
||
511 | |||
512 | if 'values_saving' not in reporting_period_data.keys() or \ |
||
513 | reporting_period_data['values_saving'] is None or \ |
||
514 | len(reporting_period_data['values_saving']) == 0: |
||
515 | has_values_saving_data = False |
||
516 | |||
517 | if 'timestamps' not in reporting_period_data.keys() or \ |
||
518 | reporting_period_data['timestamps'] is None or \ |
||
519 | len(reporting_period_data['timestamps']) == 0 or \ |
||
520 | len(reporting_period_data['timestamps'][0]) == 0: |
||
521 | has_timestamps_data = False |
||
522 | |||
523 | View Code Duplication | if has_values_saving_data and has_timestamps_data: |
|
524 | ca_len = len(reporting_period_data['names']) |
||
525 | time = reporting_period_data['timestamps'][0] |
||
526 | |||
527 | ws['B' + str(current_row_number)].font = title_font |
||
528 | ws['B' + str(current_row_number)] = name + ' 详细数据' |
||
529 | |||
530 | current_row_number += 1 |
||
531 | |||
532 | chart_start_row_number = current_row_number |
||
533 | |||
534 | current_row_number += ca_len * 5 |
||
535 | table_start_row_number = current_row_number |
||
536 | |||
537 | ws['B' + str(current_row_number)].fill = table_fill |
||
538 | ws['B' + str(current_row_number)].font = title_font |
||
539 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
540 | ws['B' + str(current_row_number)].border = f_border |
||
541 | ws['B' + str(current_row_number)] = '日期时间' |
||
542 | |||
543 | col = 'C' |
||
544 | |||
545 | for i in range(0, ca_len): |
||
546 | ws[col + str(current_row_number)].fill = table_fill |
||
547 | ws[col + str(current_row_number)].font = title_font |
||
548 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
549 | ws[col + str(current_row_number)].border = f_border |
||
550 | ws[col + str(current_row_number)] = \ |
||
551 | reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
552 | col = chr(ord(col) + 1) |
||
553 | |||
554 | current_row_number += 1 |
||
555 | |||
556 | for i in range(0, len(time)): |
||
557 | ws['B' + str(current_row_number)].font = title_font |
||
558 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
559 | ws['B' + str(current_row_number)].border = f_border |
||
560 | ws['B' + str(current_row_number)] = time[i] |
||
561 | |||
562 | col = 'C' |
||
563 | for j in range(0, ca_len): |
||
564 | ws[col + str(current_row_number)].font = title_font |
||
565 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
566 | ws[col + str(current_row_number)].border = f_border |
||
567 | ws[col + str(current_row_number)] = round(reporting_period_data['values_saving'][j][i], 2) \ |
||
568 | if reporting_period_data['values_saving'][j][i] is not None else 0.00 |
||
569 | col = chr(ord(col) + 1) |
||
570 | |||
571 | current_row_number += 1 |
||
572 | |||
573 | table_end_row_number = current_row_number - 1 |
||
574 | |||
575 | ws['B' + str(current_row_number)].font = title_font |
||
576 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
577 | ws['B' + str(current_row_number)].border = f_border |
||
578 | ws['B' + str(current_row_number)] = '小计' |
||
579 | |||
580 | col = 'C' |
||
581 | |||
582 | for i in range(0, ca_len): |
||
583 | ws[col + str(current_row_number)].font = title_font |
||
584 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
585 | ws[col + str(current_row_number)].border = f_border |
||
586 | ws[col + str(current_row_number)] = round(reporting_period_data['subtotals_saving'][i], 2) |
||
587 | col = chr(ord(col) + 1) |
||
588 | |||
589 | current_row_number += 2 |
||
590 | |||
591 | format_time_width_number = 1.0 |
||
592 | min_len_number = 1.0 |
||
593 | min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0 |
||
594 | |||
595 | if period_type == 'hourly': |
||
596 | format_time_width_number = 4.0 |
||
597 | min_len_number = 2 |
||
598 | min_width_number = 12.0 |
||
599 | elif period_type == 'daily': |
||
600 | format_time_width_number = 2.5 |
||
601 | min_len_number = 4 |
||
602 | min_width_number = 14.0 |
||
603 | elif period_type == 'monthly': |
||
604 | format_time_width_number = 2.1 |
||
605 | min_len_number = 4 |
||
606 | min_width_number = 12.4 |
||
607 | elif period_type == 'yearly': |
||
608 | format_time_width_number = 1.5 |
||
609 | min_len_number = 5 |
||
610 | min_width_number = 11.5 |
||
611 | |||
612 | for i in range(0, ca_len): |
||
613 | bar = BarChart() |
||
614 | bar.title = \ |
||
615 | reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
616 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
617 | bar_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number) |
||
618 | bar.add_data(bar_data, titles_from_data=True) |
||
619 | bar.set_categories(labels) |
||
620 | bar.height = 5.25 |
||
621 | bar.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number |
||
622 | bar.dLbls = DataLabelList() |
||
623 | bar.dLbls.showVal = True |
||
624 | bar.dLbls.showPercent = True |
||
625 | chart_col = 'B' |
||
626 | chart_cell = chart_col + str(chart_start_row_number) |
||
627 | chart_start_row_number += 5 |
||
628 | ws.add_chart(bar, chart_cell) |
||
629 | |||
630 | filename = str(uuid.uuid4()) + '.xlsx' |
||
631 | wb.save(filename) |
||
632 | |||
633 | return filename |
||
634 |