Conditions | 70 |
Total Lines | 756 |
Code Lines | 570 |
Lines | 756 |
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.spacecarbon.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 | View Code Duplication | 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 | ws.title = "SpaceCarbon" |
||
72 | |||
73 | # Row height |
||
74 | ws.row_dimensions[1].height = 102 |
||
75 | for i in range(2, 2000 + 1): |
||
76 | ws.row_dimensions[i].height = 42 |
||
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='Arial', size=15, bold=True) |
||
88 | title_font = Font(name='Arial', size=15, bold=True) |
||
89 | |||
90 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
||
91 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
||
92 | right=Side(border_style='medium', color='00000000'), |
||
93 | bottom=Side(border_style='medium', color='00000000'), |
||
94 | top=Side(border_style='medium', color='00000000') |
||
95 | ) |
||
96 | b_border = Border( |
||
97 | bottom=Side(border_style='medium', color='00000000'), |
||
98 | ) |
||
99 | |||
100 | b_c_alignment = Alignment(vertical='bottom', |
||
101 | horizontal='center', |
||
102 | text_rotation=0, |
||
103 | wrap_text=True, |
||
104 | shrink_to_fit=False, |
||
105 | indent=0) |
||
106 | c_c_alignment = Alignment(vertical='center', |
||
107 | horizontal='center', |
||
108 | text_rotation=0, |
||
109 | wrap_text=True, |
||
110 | shrink_to_fit=False, |
||
111 | indent=0) |
||
112 | b_r_alignment = Alignment(vertical='bottom', |
||
113 | horizontal='right', |
||
114 | text_rotation=0, |
||
115 | wrap_text=True, |
||
116 | shrink_to_fit=False, |
||
117 | indent=0) |
||
118 | |||
119 | # Img |
||
120 | img = Image("excelexporters/myems.png") |
||
121 | ws.add_image(img, 'A1') |
||
122 | |||
123 | # Title |
||
124 | ws['B3'].alignment = b_r_alignment |
||
125 | ws['B3'] = 'Name:' |
||
126 | ws['C3'].border = b_border |
||
127 | ws['C3'].alignment = b_c_alignment |
||
128 | ws['C3'] = name |
||
129 | |||
130 | ws['D3'].alignment = b_r_alignment |
||
131 | ws['D3'] = 'Period:' |
||
132 | ws['E3'].border = b_border |
||
133 | ws['E3'].alignment = b_c_alignment |
||
134 | ws['E3'] = period_type |
||
135 | |||
136 | ws['B4'].alignment = b_r_alignment |
||
137 | ws['B4'] = 'Reporting Start Datetime:' |
||
138 | ws['C4'].border = b_border |
||
139 | ws['C4'].alignment = b_c_alignment |
||
140 | ws['C4'] = reporting_start_datetime_local |
||
141 | |||
142 | ws['D4'].alignment = b_r_alignment |
||
143 | ws['D4'] = 'Reporting End Datetime:' |
||
144 | ws['E4'].border = b_border |
||
145 | ws['E4'].alignment = b_c_alignment |
||
146 | ws['E4'] = reporting_end_datetime_local |
||
147 | |||
148 | if "reporting_period" not in report.keys() or \ |
||
149 | "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
||
150 | filename = str(uuid.uuid4()) + '.xlsx' |
||
151 | wb.save(filename) |
||
152 | |||
153 | return filename |
||
154 | |||
155 | #################################################################################################################### |
||
156 | |||
157 | reporting_period_data = report['reporting_period'] |
||
158 | |||
159 | has_carbon_data_flag = True |
||
160 | |||
161 | if "names" not in reporting_period_data.keys() or \ |
||
162 | reporting_period_data['names'] is None or \ |
||
163 | len(reporting_period_data['names']) == 0: |
||
164 | has_carbon_data_flag = False |
||
165 | |||
166 | if has_carbon_data_flag: |
||
167 | ws['B6'].font = title_font |
||
168 | ws['B6'] = name + ' ' + 'Reporting Period Carbon Dioxide Emissions' |
||
169 | |||
170 | category = reporting_period_data['names'] |
||
171 | ca_len = len(category) |
||
172 | |||
173 | ws.row_dimensions[7].height = 60 |
||
174 | ws['B7'].fill = table_fill |
||
175 | ws['B7'].border = f_border |
||
176 | |||
177 | ws['B8'].font = title_font |
||
178 | ws['B8'].alignment = c_c_alignment |
||
179 | ws['B8'] = 'Carbon Dioxide Emissions' |
||
180 | ws['B8'].border = f_border |
||
181 | |||
182 | ws['B9'].font = title_font |
||
183 | ws['B9'].alignment = c_c_alignment |
||
184 | ws['B9'] = 'Per Unit Area' |
||
185 | ws['B9'].border = f_border |
||
186 | |||
187 | ws['B10'].font = title_font |
||
188 | ws['B10'].alignment = c_c_alignment |
||
189 | ws['B10'] = 'Increment Rate' |
||
190 | ws['B10'].border = f_border |
||
191 | |||
192 | col = '' |
||
193 | |||
194 | for i in range(0, ca_len): |
||
195 | col = chr(ord('C') + i) |
||
196 | |||
197 | ws[col + '7'].fill = table_fill |
||
198 | ws[col + '7'].font = name_font |
||
199 | ws[col + '7'].alignment = c_c_alignment |
||
200 | ws[col + '7'] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
201 | ws[col + '7'].border = f_border |
||
202 | |||
203 | ws[col + '8'].font = name_font |
||
204 | ws[col + '8'].alignment = c_c_alignment |
||
205 | ws[col + '8'] = round(reporting_period_data['subtotals'][i], 2) |
||
206 | ws[col + '8'].border = f_border |
||
207 | |||
208 | ws[col + '9'].font = name_font |
||
209 | ws[col + '9'].alignment = c_c_alignment |
||
210 | ws[col + '9'] = round(reporting_period_data['subtotals_per_unit_area'][i], 2) |
||
211 | ws[col + '9'].border = f_border |
||
212 | |||
213 | ws[col + '10'].font = name_font |
||
214 | ws[col + '10'].alignment = c_c_alignment |
||
215 | ws[col + '10'] = str(round(reporting_period_data['increment_rates'][i] * 100, 2)) + "%" \ |
||
216 | if reporting_period_data['increment_rates'][i] is not None else "-" |
||
217 | ws[col + '10'].border = f_border |
||
218 | |||
219 | col = chr(ord(col) + 1) |
||
220 | |||
221 | ws[col + '7'].fill = table_fill |
||
222 | ws[col + '7'].font = name_font |
||
223 | ws[col + '7'].alignment = c_c_alignment |
||
224 | ws[col + '7'] = "Total (" + reporting_period_data['total_unit'] + ")" |
||
225 | ws[col + '7'].border = f_border |
||
226 | |||
227 | ws[col + '8'].font = name_font |
||
228 | ws[col + '8'].alignment = c_c_alignment |
||
229 | ws[col + '8'] = round(reporting_period_data['total'], 2) |
||
230 | ws[col + '8'].border = f_border |
||
231 | |||
232 | ws[col + '9'].font = name_font |
||
233 | ws[col + '9'].alignment = c_c_alignment |
||
234 | ws[col + '9'] = round(reporting_period_data['total_per_unit_area'], 2) |
||
235 | ws[col + '9'].border = f_border |
||
236 | |||
237 | ws[col + '10'].font = name_font |
||
238 | ws[col + '10'].alignment = c_c_alignment |
||
239 | ws[col + '10'] = str(round(reporting_period_data['total_increment_rate'] * 100, 2)) + "%" \ |
||
240 | if reporting_period_data['total_increment_rate'] is not None else "-" |
||
241 | ws[col + '10'].border = f_border |
||
242 | |||
243 | else: |
||
244 | for i in range(6, 10 + 1): |
||
245 | ws.row_dimensions[i].height = 0.1 |
||
246 | |||
247 | #################################################################################################################### |
||
248 | |||
249 | has_ele_peak_flag = True |
||
250 | if "toppeaks" not in reporting_period_data.keys() or \ |
||
251 | reporting_period_data['toppeaks'] is None or \ |
||
252 | len(reporting_period_data['toppeaks']) == 0: |
||
253 | has_ele_peak_flag = False |
||
254 | |||
255 | if has_ele_peak_flag: |
||
256 | ws['B12'].font = title_font |
||
257 | ws['B12'] = name + ' ' + 'Electricity Carbon Dioxide Emissions by Time-Of-Use' |
||
258 | |||
259 | ws.row_dimensions[13].height = 60 |
||
260 | ws['B13'].fill = table_fill |
||
261 | ws['B13'].font = name_font |
||
262 | ws['B13'].alignment = c_c_alignment |
||
263 | ws['B13'].border = f_border |
||
264 | |||
265 | ws['C13'].fill = table_fill |
||
266 | ws['C13'].font = name_font |
||
267 | ws['C13'].alignment = c_c_alignment |
||
268 | ws['C13'].border = f_border |
||
269 | ws['C13'] = 'Electricity Carbon Dioxide Emissions by Time-Of-Use' |
||
270 | |||
271 | ws['B14'].font = title_font |
||
272 | ws['B14'].alignment = c_c_alignment |
||
273 | ws['B14'] = 'TopPeak' |
||
274 | ws['B14'].border = f_border |
||
275 | |||
276 | ws['C14'].font = title_font |
||
277 | ws['C14'].alignment = c_c_alignment |
||
278 | ws['C14'].border = f_border |
||
279 | ws['C14'] = round(reporting_period_data['toppeaks'][0], 2) |
||
280 | |||
281 | ws['B15'].font = title_font |
||
282 | ws['B15'].alignment = c_c_alignment |
||
283 | ws['B15'] = 'OnPeak' |
||
284 | ws['B15'].border = f_border |
||
285 | |||
286 | ws['C15'].font = title_font |
||
287 | ws['C15'].alignment = c_c_alignment |
||
288 | ws['C15'].border = f_border |
||
289 | ws['C15'] = round(reporting_period_data['onpeaks'][0], 2) |
||
290 | |||
291 | ws['B16'].font = title_font |
||
292 | ws['B16'].alignment = c_c_alignment |
||
293 | ws['B16'] = 'MidPeak' |
||
294 | ws['B16'].border = f_border |
||
295 | |||
296 | ws['C16'].font = title_font |
||
297 | ws['C16'].alignment = c_c_alignment |
||
298 | ws['C16'].border = f_border |
||
299 | ws['C16'] = round(reporting_period_data['midpeaks'][0], 2) |
||
300 | |||
301 | ws['B17'].font = title_font |
||
302 | ws['B17'].alignment = c_c_alignment |
||
303 | ws['B17'] = 'OffPeak' |
||
304 | ws['B17'].border = f_border |
||
305 | |||
306 | ws['C17'].font = title_font |
||
307 | ws['C17'].alignment = c_c_alignment |
||
308 | ws['C17'].border = f_border |
||
309 | ws['C17'] = round(reporting_period_data['offpeaks'][0], 2) |
||
310 | |||
311 | pie = PieChart() |
||
312 | pie.title = name + 'Electricity Carbon Dioxide Emissions by Time-Of-Use' |
||
313 | labels = Reference(ws, min_col=2, min_row=14, max_row=17) |
||
314 | pie_data = Reference(ws, min_col=3, min_row=13, max_row=17) |
||
315 | pie.add_data(pie_data, titles_from_data=True) |
||
316 | pie.set_categories(labels) |
||
317 | pie.height = 7.25 |
||
318 | pie.width = 9 |
||
319 | s1 = pie.series[0] |
||
320 | s1.dLbls = DataLabelList() |
||
321 | s1.dLbls.showCatName = False |
||
322 | s1.dLbls.showVal = True |
||
323 | s1.dLbls.showPercent = True |
||
324 | |||
325 | ws.add_chart(pie, "D13") |
||
326 | |||
327 | else: |
||
328 | for i in range(12, 18 + 1): |
||
329 | ws.row_dimensions[i].height = 0.1 |
||
330 | |||
331 | #################################################################################################################### |
||
332 | |||
333 | current_row_number = 19 |
||
334 | |||
335 | has_subtotals_data_flag = True |
||
336 | |||
337 | if 'subtotals' not in reporting_period_data.keys() or \ |
||
338 | reporting_period_data['subtotals'] is None: |
||
339 | has_subtotals_data_flag = False |
||
340 | |||
341 | if has_subtotals_data_flag: |
||
342 | ws['B' + str(current_row_number)].font = title_font |
||
343 | ws['B' + str(current_row_number)] = name + ' ' + 'Carbon Dioxide Emissions Proportion' |
||
344 | |||
345 | current_row_number += 1 |
||
346 | table_start_row_number = current_row_number |
||
347 | |||
348 | ws['B' + str(current_row_number)].fill = table_fill |
||
349 | ws['B' + str(current_row_number)].font = name_font |
||
350 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
351 | ws['B' + str(current_row_number)].border = f_border |
||
352 | |||
353 | ws['C' + str(current_row_number)].fill = table_fill |
||
354 | ws['C' + str(current_row_number)].font = name_font |
||
355 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
356 | ws['C' + str(current_row_number)].border = f_border |
||
357 | ws['C' + str(current_row_number)] = 'Carbon Dioxide Emissions Proportion' |
||
358 | |||
359 | current_row_number += 1 |
||
360 | |||
361 | category = reporting_period_data['names'] |
||
362 | ca_len = len(category) |
||
363 | |||
364 | for i in range(0, ca_len): |
||
365 | ws['B' + str(current_row_number)].font = title_font |
||
366 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
367 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] + \ |
||
368 | ' (' + reporting_period_data['units'][i] + ')' |
||
369 | ws['B' + str(current_row_number)].border = f_border |
||
370 | |||
371 | ws['C' + str(current_row_number)].font = title_font |
||
372 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
373 | ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 3) |
||
374 | ws['C' + str(current_row_number)].border = f_border |
||
375 | |||
376 | current_row_number += 1 |
||
377 | |||
378 | table_end_row_number = current_row_number - 1 |
||
379 | |||
380 | pie = PieChart() |
||
381 | pie.title = name + ' ' + 'Carbon Dioxide Emissions Proportion' |
||
382 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
383 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
||
384 | pie.add_data(pie_data, titles_from_data=True) |
||
385 | pie.set_categories(labels) |
||
386 | pie.height = 6.6 |
||
387 | pie.width = 9 |
||
388 | s1 = pie.series[0] |
||
389 | s1.dLbls = DataLabelList() |
||
390 | s1.dLbls.showCatName = False |
||
391 | s1.dLbls.showVal = True |
||
392 | s1.dLbls.showPercent = True |
||
393 | |||
394 | ws.add_chart(pie, 'D' + str(table_start_row_number)) |
||
395 | |||
396 | if ca_len < 4: |
||
397 | current_row_number = current_row_number - ca_len + 4 |
||
398 | |||
399 | current_row_number += 1 |
||
400 | |||
401 | #################################################################################################################### |
||
402 | |||
403 | reporting_period_data = report['reporting_period'] |
||
404 | times = reporting_period_data['timestamps'] |
||
405 | has_detail_data_flag = True |
||
406 | ca_len = len(report['reporting_period']['names']) |
||
407 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
||
408 | table_row = (current_row_number + 1) + ca_len * 6 + real_timestamps_len * 7 |
||
409 | current_end_row_number = (current_row_number + 1) + ca_len * 6 + len(times[0]) + 3 + real_timestamps_len * 7 |
||
410 | if "timestamps" not in reporting_period_data.keys() or \ |
||
411 | reporting_period_data['timestamps'] is None or \ |
||
412 | len(reporting_period_data['timestamps']) == 0: |
||
413 | has_detail_data_flag = False |
||
414 | |||
415 | if has_detail_data_flag: |
||
416 | ws['B' + str(current_row_number)].font = title_font |
||
417 | ws['B' + str(current_row_number)] = name + ' ' + 'Detailed Data' |
||
418 | |||
419 | ws.row_dimensions[table_row].height = 60 |
||
420 | ws['B' + str(table_row)].fill = table_fill |
||
421 | ws['B' + str(table_row)].font = title_font |
||
422 | ws['B' + str(table_row)].border = f_border |
||
423 | ws['B' + str(table_row)].alignment = c_c_alignment |
||
424 | ws['B' + str(table_row)] = 'Datetime' |
||
425 | time = times[0] |
||
426 | has_data = False |
||
427 | max_row = 0 |
||
428 | if len(time) > 0: |
||
429 | has_data = True |
||
430 | max_row = table_row + len(time) |
||
431 | |||
432 | if has_data: |
||
433 | for i in range(0, len(time)): |
||
434 | col = 'B' |
||
435 | row = str(table_row + 1 + i) |
||
436 | ws[col + row].font = title_font |
||
437 | ws[col + row].alignment = c_c_alignment |
||
438 | ws[col + row] = time[i] |
||
439 | ws[col + row].border = f_border |
||
440 | |||
441 | for i in range(0, ca_len): |
||
442 | |||
443 | col = chr(ord('C') + i) |
||
444 | |||
445 | ws[col + str(table_row)].fill = table_fill |
||
446 | ws[col + str(table_row)].font = title_font |
||
447 | ws[col + str(table_row)].alignment = c_c_alignment |
||
448 | ws[col + str(table_row)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][ |
||
449 | i] + ")" |
||
450 | ws[col + str(table_row)].border = f_border |
||
451 | |||
452 | # 39 data |
||
453 | time = times[i] |
||
454 | time_len = len(time) |
||
455 | |||
456 | for j in range(0, time_len): |
||
457 | row = str(table_row + 1 + j) |
||
458 | ws[col + row].font = title_font |
||
459 | ws[col + row].alignment = c_c_alignment |
||
460 | ws[col + row] = round(reporting_period_data['values'][i][j], 2) |
||
461 | ws[col + row].border = f_border |
||
462 | |||
463 | line = LineChart() |
||
464 | line.title = 'Reporting Period Carbon Dioxide Emissions - ' + reporting_period_data['names'][i] + \ |
||
465 | " (" + reporting_period_data['units'][i] + ")" |
||
466 | labels = Reference(ws, min_col=2, min_row=table_row + 1, max_row=max_row) |
||
467 | line_data = Reference(ws, min_col=3 + i, min_row=table_row, max_row=max_row) |
||
468 | line.add_data(line_data, titles_from_data=True) |
||
469 | line.set_categories(labels) |
||
470 | line_data = line.series[0] |
||
471 | line_data.marker.symbol = "circle" |
||
472 | line_data.smooth = True |
||
473 | line.x_axis.crosses = 'min' |
||
474 | line.height = 8.25 |
||
475 | line.width = 24 |
||
476 | line.dLbls = DataLabelList() |
||
477 | line.dLbls.dLblPos = 't' |
||
478 | line.dLbls.showVal = True |
||
479 | line.dLbls.showPercent = False |
||
480 | chart_col = 'B' |
||
481 | chart_cell = chart_col + str(current_row_number + 1 + 6 * i) |
||
482 | table_start_draw_flag = current_row_number |
||
483 | ws.add_chart(line, chart_cell) |
||
484 | |||
485 | row = str(max_row + 1) |
||
486 | |||
487 | ws['B' + row].font = title_font |
||
488 | ws['B' + row].alignment = c_c_alignment |
||
489 | ws['B' + row] = 'Subtotal' |
||
490 | ws['B' + row].border = f_border |
||
491 | |||
492 | col = '' |
||
493 | |||
494 | for i in range(0, ca_len): |
||
495 | col = chr(ord('C') + i) |
||
496 | row = str(max_row + 1) |
||
497 | ws[col + row].font = title_font |
||
498 | ws[col + row].alignment = c_c_alignment |
||
499 | ws[col + row] = round(reporting_period_data['subtotals'][i], 2) |
||
500 | ws[col + row].border = f_border |
||
501 | |||
502 | col = chr(ord(col) + 1) |
||
503 | |||
504 | ws[col + str(table_row)].fill = table_fill |
||
505 | ws[col + str(table_row)].font = title_font |
||
506 | ws[col + str(table_row)].alignment = c_c_alignment |
||
507 | ws[col + str(table_row)] = 'Total (' + report['reporting_period']['total_unit'] + ')' |
||
508 | ws[col + str(table_row)].border = f_border |
||
509 | |||
510 | total_sum = 0 |
||
511 | |||
512 | for j in range(0, len(time)): |
||
513 | row = str(table_row + 1 + j) |
||
514 | ws[col + row].font = title_font |
||
515 | ws[col + row].alignment = c_c_alignment |
||
516 | periodic_sum = reporting_period_values_periodic_sum(reporting_period_data, j, ca_len) |
||
517 | total_sum += periodic_sum |
||
518 | ws[col + row] = round(periodic_sum, 2) |
||
519 | ws[col + row].border = f_border |
||
520 | |||
521 | row = str(table_row + 1 + len(time)) |
||
522 | ws[col + row].font = title_font |
||
523 | ws[col + row].alignment = c_c_alignment |
||
524 | ws[col + row] = round(total_sum, 2) |
||
525 | ws[col + row].border = f_border |
||
526 | |||
527 | current_row_number = current_end_row_number |
||
528 | |||
529 | #################################################################################################################### |
||
530 | |||
531 | has_child_flag = True |
||
532 | |||
533 | if "child_space" not in report.keys() or "energy_category_names" not in report['child_space'].keys() or \ |
||
534 | len(report['child_space']["energy_category_names"]) == 0 \ |
||
535 | or 'child_space_names_array' not in report['child_space'].keys() \ |
||
536 | or report['child_space']['energy_category_names'] is None \ |
||
537 | or len(report['child_space']['child_space_names_array']) == 0 \ |
||
538 | or len(report['child_space']['child_space_names_array'][0]) == 0: |
||
539 | has_child_flag = False |
||
540 | |||
541 | if has_child_flag: |
||
542 | child = report['child_space'] |
||
543 | |||
544 | ws['B' + str(current_row_number)].font = title_font |
||
545 | ws['B' + str(current_row_number)] = name + ' ' + 'Child Spaces Data' |
||
546 | |||
547 | current_row_number += 1 |
||
548 | table_start_row_number = current_row_number |
||
549 | |||
550 | ws.row_dimensions[current_row_number].height = 60 |
||
551 | ws['B' + str(current_row_number)].fill = table_fill |
||
552 | ws['B' + str(current_row_number)].font = name_font |
||
553 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
554 | ws['B' + str(current_row_number)].border = f_border |
||
555 | ws['B' + str(current_row_number)] = 'Child Space' |
||
556 | ca_len = len(child['energy_category_names']) |
||
557 | |||
558 | col = '' |
||
559 | |||
560 | for i in range(0, ca_len): |
||
561 | col = chr(ord('C') + i) |
||
562 | ws[col + str(current_row_number)].fill = table_fill |
||
563 | ws[col + str(current_row_number)].font = name_font |
||
564 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
565 | ws[col + str(current_row_number)].border = f_border |
||
566 | ws[col + str(current_row_number)] = child['energy_category_names'][i] + ' ' + '(' + child['units'][i] + ')' |
||
567 | |||
568 | col = chr(ord(col) + 1) |
||
569 | ws[col + str(current_row_number)].fill = table_fill |
||
570 | ws[col + str(current_row_number)].font = name_font |
||
571 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
572 | ws[col + str(current_row_number)].border = f_border |
||
573 | ws[col + str(current_row_number)] = 'Total (' + report['reporting_period']['total_unit'] + ')' |
||
574 | |||
575 | space_len = len(child['child_space_names_array'][0]) |
||
576 | |||
577 | for i in range(0, space_len): |
||
578 | current_row_number += 1 |
||
579 | row = str(current_row_number) |
||
580 | |||
581 | ws['B' + row].font = title_font |
||
582 | ws['B' + row].alignment = c_c_alignment |
||
583 | ws['B' + row] = child['child_space_names_array'][0][i] |
||
584 | ws['B' + row].border = f_border |
||
585 | |||
586 | col = '' |
||
587 | periodic_sum = 0 |
||
588 | |||
589 | for j in range(0, ca_len): |
||
590 | col = chr(ord('C') + j) |
||
591 | ws[col + row].font = name_font |
||
592 | ws[col + row].alignment = c_c_alignment |
||
593 | periodic_sum += child['subtotals_array'][j][i] |
||
594 | ws[col + row] = round(child['subtotals_array'][j][i], 2) |
||
595 | ws[col + row].border = f_border |
||
596 | |||
597 | col = chr(ord(col) + 1) |
||
598 | ws[col + row].font = name_font |
||
599 | ws[col + row].alignment = c_c_alignment |
||
600 | ws[col + row] = round(periodic_sum, 2) |
||
601 | ws[col + row].border = f_border |
||
602 | |||
603 | table_end_row_number = current_row_number |
||
604 | current_row_number += 1 |
||
605 | chart_start_row_number = current_row_number |
||
606 | |||
607 | # Pie |
||
608 | for i in range(0, ca_len): |
||
609 | pie = PieChart() |
||
610 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
611 | pie_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, |
||
612 | max_row=table_end_row_number) |
||
613 | pie.add_data(pie_data, titles_from_data=True) |
||
614 | pie.set_categories(labels) |
||
615 | pie.height = 6.6 |
||
616 | pie.width = 8 |
||
617 | pie.title = ws.cell(column=3 + i, row=table_start_row_number).value |
||
618 | s1 = pie.series[0] |
||
619 | s1.dLbls = DataLabelList() |
||
620 | s1.dLbls.showCatName = False |
||
621 | s1.dLbls.showVal = True |
||
622 | s1.dLbls.showPercent = True |
||
623 | chart_cell = '' |
||
624 | if i % 2 == 0: |
||
625 | chart_cell = 'B' + str(chart_start_row_number) |
||
626 | else: |
||
627 | chart_cell = 'E' + str(chart_start_row_number) |
||
628 | chart_start_row_number += 5 |
||
629 | ws.add_chart(pie, chart_cell) |
||
630 | |||
631 | current_row_number = chart_start_row_number |
||
632 | |||
633 | if ca_len % 2 == 1: |
||
634 | current_row_number += 5 |
||
635 | |||
636 | current_row_number += 1 |
||
637 | #################################################################################################################### |
||
638 | current_sheet_parameters_row_number = table_start_draw_flag + ca_len * 6 + 1 |
||
639 | has_parameters_names_and_timestamps_and_values_data = True |
||
640 | if 'parameters' not in report.keys() or \ |
||
641 | report['parameters'] is None or \ |
||
642 | 'names' not in report['parameters'].keys() or \ |
||
643 | report['parameters']['names'] is None or \ |
||
644 | len(report['parameters']['names']) == 0 or \ |
||
645 | 'timestamps' not in report['parameters'].keys() or \ |
||
646 | report['parameters']['timestamps'] is None or \ |
||
647 | len(report['parameters']['timestamps']) == 0 or \ |
||
648 | 'values' not in report['parameters'].keys() or \ |
||
649 | report['parameters']['values'] is None or \ |
||
650 | len(report['parameters']['values']) == 0 or \ |
||
651 | timestamps_data_all_equal_0(report['parameters']['timestamps']): |
||
652 | has_parameters_names_and_timestamps_and_values_data = False |
||
653 | if has_parameters_names_and_timestamps_and_values_data: |
||
654 | |||
655 | ################################################################################################################ |
||
656 | # new worksheet |
||
657 | ################################################################################################################ |
||
658 | |||
659 | parameters_data = report['parameters'] |
||
660 | parameters_names_len = len(parameters_data['names']) |
||
661 | |||
662 | file_name = "Space"+re.sub(r'[^A-Z]', '', ws.title.strip('S')) + "_" |
||
663 | parameters_ws = wb.create_sheet(file_name + 'Parameters') |
||
664 | |||
665 | parameters_timestamps_data_max_len = \ |
||
666 | get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
||
667 | |||
668 | # Row height |
||
669 | parameters_ws.row_dimensions[1].height = 102 |
||
670 | for i in range(2, 7 + 1): |
||
671 | parameters_ws.row_dimensions[i].height = 42 |
||
672 | |||
673 | for i in range(8, parameters_timestamps_data_max_len + 10): |
||
674 | parameters_ws.row_dimensions[i].height = 60 |
||
675 | |||
676 | # Col width |
||
677 | parameters_ws.column_dimensions['A'].width = 1.5 |
||
678 | |||
679 | parameters_ws.column_dimensions['B'].width = 25.0 |
||
680 | |||
681 | for i in range(3, 12 + parameters_names_len * 3): |
||
682 | parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
||
683 | |||
684 | # Img |
||
685 | img = Image("excelexporters/myems.png") |
||
686 | parameters_ws.add_image(img, 'A1') |
||
687 | |||
688 | # Title |
||
689 | parameters_ws['B3'].alignment = b_r_alignment |
||
690 | parameters_ws['B3'] = 'Name:' |
||
691 | parameters_ws['C3'].border = b_border |
||
692 | parameters_ws['C3'].alignment = b_c_alignment |
||
693 | parameters_ws['C3'] = name |
||
694 | |||
695 | parameters_ws['D3'].alignment = b_r_alignment |
||
696 | parameters_ws['D3'] = 'Period:' |
||
697 | parameters_ws['E3'].border = b_border |
||
698 | parameters_ws['E3'].alignment = b_c_alignment |
||
699 | parameters_ws['E3'] = period_type |
||
700 | |||
701 | parameters_ws['B4'].alignment = b_r_alignment |
||
702 | parameters_ws['B4'] = 'Reporting Start Datetime:' |
||
703 | parameters_ws['C4'].border = b_border |
||
704 | parameters_ws['C4'].alignment = b_c_alignment |
||
705 | parameters_ws['C4'] = reporting_start_datetime_local |
||
706 | |||
707 | parameters_ws['D4'].alignment = b_r_alignment |
||
708 | parameters_ws['D4'] = 'Reporting End Datetime:' |
||
709 | parameters_ws['E4'].border = b_border |
||
710 | parameters_ws['E4'].alignment = b_c_alignment |
||
711 | parameters_ws['E4'] = reporting_end_datetime_local |
||
712 | |||
713 | parameters_ws_current_row_number = 6 |
||
714 | |||
715 | parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
||
716 | parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' ' + 'Parameters' |
||
717 | |||
718 | parameters_ws_current_row_number += 1 |
||
719 | |||
720 | parameters_table_start_row_number = parameters_ws_current_row_number |
||
721 | |||
722 | parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
||
723 | |||
724 | parameters_ws_current_row_number += 1 |
||
725 | |||
726 | table_current_col_number = 2 |
||
727 | |||
728 | for i in range(0, parameters_names_len): |
||
729 | |||
730 | if len(parameters_data['timestamps'][i]) == 0: |
||
731 | continue |
||
732 | |||
733 | col = format_cell.get_column_letter(table_current_col_number) |
||
734 | |||
735 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
736 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
737 | |||
738 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
739 | |||
740 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
741 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
742 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = name_font |
||
743 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = c_c_alignment |
||
744 | parameters_ws[col + str(parameters_ws_current_row_number - 1)] = parameters_data['names'][i] |
||
745 | |||
746 | table_current_row_number = parameters_ws_current_row_number |
||
747 | |||
748 | for j, value in enumerate(list(parameters_data['timestamps'][i])): |
||
749 | col = format_cell.get_column_letter(table_current_col_number) |
||
750 | |||
751 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
752 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
753 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
754 | parameters_ws[col + str(table_current_row_number)] = value |
||
755 | |||
756 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
757 | |||
758 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
759 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
760 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
761 | parameters_ws[col + str(table_current_row_number)] = round(parameters_data['values'][i][j], 2) |
||
762 | |||
763 | table_current_row_number += 1 |
||
764 | |||
765 | table_current_col_number = table_current_col_number + 3 |
||
766 | |||
767 | ################################################################################################################ |
||
768 | # parameters chart and parameters table |
||
769 | ################################################################################################################ |
||
770 | |||
771 | ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
||
772 | ws['B' + str(current_sheet_parameters_row_number)] = name + ' ' + 'Parameters' |
||
773 | |||
774 | current_sheet_parameters_row_number += 1 |
||
775 | |||
776 | chart_start_row_number = current_sheet_parameters_row_number |
||
777 | |||
778 | col_index = 0 |
||
779 | |||
780 | for i in range(0, parameters_names_len): |
||
781 | |||
782 | if len(parameters_data['timestamps'][i]) == 0: |
||
783 | continue |
||
784 | |||
785 | line = LineChart() |
||
786 | data_col = 3 + col_index * 3 |
||
787 | labels_col = 2 + col_index * 3 |
||
788 | col_index += 1 |
||
789 | line.title = 'Parameters - ' + \ |
||
790 | parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
||
791 | labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
||
792 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
793 | line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
||
794 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
795 | line.add_data(line_data, titles_from_data=True) |
||
796 | line.set_categories(labels) |
||
797 | line_data = line.series[0] |
||
798 | line_data.marker.symbol = "circle" |
||
799 | line_data.smooth = True |
||
800 | line.x_axis.crosses = 'min' |
||
801 | line.height = 8.25 |
||
802 | line.width = 24 |
||
803 | line.dLbls = DataLabelList() |
||
804 | line.dLbls.dLblPos = 't' |
||
805 | line.dLbls.showVal = False |
||
806 | line.dLbls.showPercent = False |
||
807 | chart_col = 'B' |
||
808 | chart_cell = chart_col + str(chart_start_row_number) |
||
809 | chart_start_row_number += 6 |
||
810 | ws.add_chart(line, chart_cell) |
||
811 | |||
812 | current_sheet_parameters_row_number = chart_start_row_number |
||
813 | |||
814 | current_sheet_parameters_row_number += 1 |
||
815 | #################################################################################################################### |
||
816 | filename = str(uuid.uuid4()) + '.xlsx' |
||
817 | wb.save(filename) |
||
818 | |||
819 | return filename |
||
820 | |||
853 |