@@ 70-962 (lines=893) @@ | ||
67 | return base64_message |
|
68 | ||
69 | ||
70 | def generate_excel(report, |
|
71 | name, |
|
72 | base_period_start_datetime_local, |
|
73 | base_period_end_datetime_local, |
|
74 | reporting_start_datetime_local, |
|
75 | reporting_end_datetime_local, |
|
76 | period_type, |
|
77 | language): |
|
78 | ||
79 | trans = get_translation(language) |
|
80 | trans.install() |
|
81 | _ = trans.gettext |
|
82 | ||
83 | wb = Workbook() |
|
84 | ws = wb.active |
|
85 | ws.title = "TenantCost" |
|
86 | ||
87 | # Row height |
|
88 | ws.row_dimensions[1].height = 102 |
|
89 | for i in range(2, 2000 + 1): |
|
90 | ws.row_dimensions[i].height = 42 |
|
91 | ||
92 | # Col width |
|
93 | ws.column_dimensions['A'].width = 1.5 |
|
94 | ||
95 | ws.column_dimensions['B'].width = 25.0 |
|
96 | ||
97 | for i in range(ord('C'), ord('Z')): |
|
98 | ws.column_dimensions[chr(i)].width = 15.0 |
|
99 | ||
100 | # Font |
|
101 | name_font = Font(name='Arial', size=15, bold=True) |
|
102 | title_font = Font(name='Arial', size=15, bold=True) |
|
103 | ||
104 | table_fill = PatternFill(fill_type='solid', fgColor='90ee90') |
|
105 | f_border = Border(left=Side(border_style='medium'), |
|
106 | right=Side(border_style='medium'), |
|
107 | bottom=Side(border_style='medium'), |
|
108 | top=Side(border_style='medium') |
|
109 | ) |
|
110 | b_border = Border( |
|
111 | bottom=Side(border_style='medium'), |
|
112 | ) |
|
113 | ||
114 | b_c_alignment = Alignment(vertical='bottom', |
|
115 | horizontal='center', |
|
116 | text_rotation=0, |
|
117 | wrap_text=True, |
|
118 | shrink_to_fit=False, |
|
119 | indent=0) |
|
120 | c_c_alignment = Alignment(vertical='center', |
|
121 | horizontal='center', |
|
122 | text_rotation=0, |
|
123 | wrap_text=True, |
|
124 | shrink_to_fit=False, |
|
125 | indent=0) |
|
126 | b_r_alignment = Alignment(vertical='bottom', |
|
127 | horizontal='right', |
|
128 | text_rotation=0, |
|
129 | wrap_text=True, |
|
130 | shrink_to_fit=False, |
|
131 | indent=0) |
|
132 | ||
133 | # Img |
|
134 | img = Image("excelexporters/myems.png") |
|
135 | ws.add_image(img, 'A1') |
|
136 | ||
137 | # Title |
|
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'] = name |
|
143 | ||
144 | ws['D3'].alignment = b_r_alignment |
|
145 | ws['D3'] = _('Period Type') + ':' |
|
146 | ws['E3'].border = b_border |
|
147 | ws['E3'].alignment = b_c_alignment |
|
148 | ws['E3'] = period_type |
|
149 | ||
150 | ws['B4'].alignment = b_r_alignment |
|
151 | ws['B4'] = _('Reporting Start Datetime') + ':' |
|
152 | ws['C4'].border = b_border |
|
153 | ws['C4'].alignment = b_c_alignment |
|
154 | ws['C4'] = reporting_start_datetime_local |
|
155 | ||
156 | ws['D4'].alignment = b_r_alignment |
|
157 | ws['D4'] = _('Reporting End Datetime') + ':' |
|
158 | ws['E4'].border = b_border |
|
159 | ws['E4'].alignment = b_c_alignment |
|
160 | ws['E4'] = reporting_end_datetime_local |
|
161 | ||
162 | is_base_period_timestamp_exists_flag = is_base_period_timestamp_exists(report['base_period']) |
|
163 | ||
164 | if is_base_period_timestamp_exists_flag: |
|
165 | ws['B5'].alignment = b_r_alignment |
|
166 | ws['B5'] = _('Base Period Start Datetime') + ':' |
|
167 | ws['C5'].border = b_border |
|
168 | ws['C5'].alignment = b_c_alignment |
|
169 | ws['C5'] = base_period_start_datetime_local |
|
170 | ||
171 | ws['D5'].alignment = b_r_alignment |
|
172 | ws['D5'] = _('Base Period End Datetime') + ':' |
|
173 | ws['E5'].border = b_border |
|
174 | ws['E5'].alignment = b_c_alignment |
|
175 | ws['E5'] = base_period_end_datetime_local |
|
176 | ||
177 | if "reporting_period" not in report.keys() or \ |
|
178 | "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
|
179 | filename = str(uuid.uuid4()) + '.xlsx' |
|
180 | wb.save(filename) |
|
181 | ||
182 | return filename |
|
183 | ||
184 | #################################################################################################################### |
|
185 | ||
186 | reporting_period_data = report['reporting_period'] |
|
187 | ||
188 | if "names" not in reporting_period_data.keys() or \ |
|
189 | reporting_period_data['names'] is None or \ |
|
190 | len(reporting_period_data['names']) == 0: |
|
191 | for i in range(7, 11 + 1): |
|
192 | ws.row_dimensions[i].height = 0.1 |
|
193 | else: |
|
194 | ws['B7'].font = title_font |
|
195 | ws['B7'] = name + ' ' + _('Reporting Period Costs') |
|
196 | ||
197 | category = reporting_period_data['names'] |
|
198 | ca_len = len(category) |
|
199 | ||
200 | ws.row_dimensions[8].height = 60 |
|
201 | ws['B8'].fill = table_fill |
|
202 | ws['B8'].border = f_border |
|
203 | ||
204 | ws['B9'].font = title_font |
|
205 | ws['B9'].alignment = c_c_alignment |
|
206 | ws['B9'] = _('Cost') |
|
207 | ws['B9'].border = f_border |
|
208 | ||
209 | ws['B10'].font = title_font |
|
210 | ws['B10'].alignment = c_c_alignment |
|
211 | ws['B10'] = _('Per Unit Area') |
|
212 | ws['B10'].border = f_border |
|
213 | ||
214 | ws['B11'].font = title_font |
|
215 | ws['B11'].alignment = c_c_alignment |
|
216 | ws['B11'] = _('Increment Rate') |
|
217 | ws['B11'].border = f_border |
|
218 | ||
219 | col = 'B' |
|
220 | ||
221 | for i in range(0, ca_len): |
|
222 | col = chr(ord('C') + i) |
|
223 | ws[col + '8'].fill = table_fill |
|
224 | ws[col + '8'].font = name_font |
|
225 | ws[col + '8'].alignment = c_c_alignment |
|
226 | ws[col + '8'] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
|
227 | ws[col + '8'].border = f_border |
|
228 | ||
229 | ws[col + '9'].font = name_font |
|
230 | ws[col + '9'].alignment = c_c_alignment |
|
231 | ws[col + '9'] = round2(reporting_period_data['subtotals'][i], 2) |
|
232 | ws[col + '9'].border = f_border |
|
233 | ||
234 | ws[col + '10'].font = name_font |
|
235 | ws[col + '10'].alignment = c_c_alignment |
|
236 | ws[col + '10'] = round2(reporting_period_data['subtotals_per_unit_area'][i], 2) |
|
237 | ws[col + '10'].border = f_border |
|
238 | ||
239 | ws[col + '11'].font = name_font |
|
240 | ws[col + '11'].alignment = c_c_alignment |
|
241 | ws[col + '11'] = str(round2(reporting_period_data['increment_rates'][i] * 100, 2)) + "%" \ |
|
242 | if reporting_period_data['increment_rates'][i] is not None else "-" |
|
243 | ws[col + '11'].border = f_border |
|
244 | ||
245 | end_col = chr(ord(col)+1) |
|
246 | ws[end_col + '8'].fill = table_fill |
|
247 | ws[end_col + '8'].font = name_font |
|
248 | ws[end_col + '8'].alignment = c_c_alignment |
|
249 | ws[end_col + '8'] = _("Total") + " (" + reporting_period_data['total_unit'] + ")" |
|
250 | ws[end_col + '8'].border = f_border |
|
251 | ||
252 | ws[end_col + '9'].font = name_font |
|
253 | ws[end_col + '9'].alignment = c_c_alignment |
|
254 | ws[end_col + '9'] = round2(reporting_period_data['total'], 2) |
|
255 | ws[end_col + '9'].border = f_border |
|
256 | ||
257 | ws[end_col + '10'].font = name_font |
|
258 | ws[end_col + '10'].alignment = c_c_alignment |
|
259 | ws[end_col + '10'] = round2(reporting_period_data['total_per_unit_area'], 2) |
|
260 | ws[end_col + '10'].border = f_border |
|
261 | ||
262 | ws[end_col + '11'].font = name_font |
|
263 | ws[end_col + '11'].alignment = c_c_alignment |
|
264 | ws[end_col + '11'] = str(round2(reporting_period_data['total_increment_rate'] * 100, 2)) + "%" \ |
|
265 | if reporting_period_data['total_increment_rate'] is not None else "-" |
|
266 | ws[end_col + '11'].border = f_border |
|
267 | ||
268 | #################################################################################################################### |
|
269 | if "toppeaks" not in reporting_period_data.keys() or \ |
|
270 | reporting_period_data['toppeaks'] is None or \ |
|
271 | len(reporting_period_data['toppeaks']) == 0: |
|
272 | for i in range(13, 19 + 1): |
|
273 | ws.row_dimensions[i].height = 0.1 |
|
274 | else: |
|
275 | electricity_index = -1 |
|
276 | for i in range(len(reporting_period_data['energy_category_ids'])): |
|
277 | if reporting_period_data['energy_category_ids'][i] == 1: |
|
278 | electricity_index = i |
|
279 | break |
|
280 | ||
281 | ws['B13'].font = title_font |
|
282 | ws['B13'] = name + ' ' + _('Electricity Costs by Time-Of-Use') |
|
283 | ||
284 | ws['B14'].fill = table_fill |
|
285 | ws['B14'].font = name_font |
|
286 | ws['B14'].alignment = c_c_alignment |
|
287 | ws['B14'].border = f_border |
|
288 | ||
289 | ws['C14'].fill = table_fill |
|
290 | ws['C14'].font = name_font |
|
291 | ws['C14'].alignment = c_c_alignment |
|
292 | ws['C14'].border = f_border |
|
293 | ws['C14'] = _('Electricity Costs by Time-Of-Use') |
|
294 | ||
295 | ws['B15'].font = title_font |
|
296 | ws['B15'].alignment = c_c_alignment |
|
297 | ws['B15'] = _('TopPeak') |
|
298 | ws['B15'].border = f_border |
|
299 | ||
300 | ws['C15'].font = title_font |
|
301 | ws['C15'].alignment = c_c_alignment |
|
302 | ws['C15'].border = f_border |
|
303 | ws['C15'] = round2(reporting_period_data['toppeaks'][electricity_index], 2) if electricity_index >= 0 else "-" |
|
304 | ||
305 | ws['B16'].font = title_font |
|
306 | ws['B16'].alignment = c_c_alignment |
|
307 | ws['B16'] = _('OnPeak') |
|
308 | ws['B16'].border = f_border |
|
309 | ||
310 | ws['C16'].font = title_font |
|
311 | ws['C16'].alignment = c_c_alignment |
|
312 | ws['C16'].border = f_border |
|
313 | ws['C16'] = round2(reporting_period_data['onpeaks'][electricity_index], 2) if electricity_index >= 0 else "-" |
|
314 | ||
315 | ws['B17'].font = title_font |
|
316 | ws['B17'].alignment = c_c_alignment |
|
317 | ws['B17'] = _('MidPeak') |
|
318 | ws['B17'].border = f_border |
|
319 | ||
320 | ws['C17'].font = title_font |
|
321 | ws['C17'].alignment = c_c_alignment |
|
322 | ws['C17'].border = f_border |
|
323 | ws['C17'] = round2(reporting_period_data['midpeaks'][electricity_index], 2) if electricity_index >= 0 else "-" |
|
324 | ||
325 | ws['B18'].font = title_font |
|
326 | ws['B18'].alignment = c_c_alignment |
|
327 | ws['B18'] = _('OffPeak') |
|
328 | ws['B18'].border = f_border |
|
329 | ||
330 | ws['C18'].font = title_font |
|
331 | ws['C18'].alignment = c_c_alignment |
|
332 | ws['C18'].border = f_border |
|
333 | ws['C18'] = round2(reporting_period_data['offpeaks'][electricity_index], 2) if electricity_index >= 0 else "-" |
|
334 | ||
335 | pie = PieChart() |
|
336 | pie.title = name + ' ' + _('Electricity Costs by Time-Of-Use') |
|
337 | labels = Reference(ws, min_col=2, min_row=15, max_row=18) |
|
338 | pie_data = Reference(ws, min_col=3, min_row=14, max_row=18) |
|
339 | pie.add_data(pie_data, titles_from_data=True) |
|
340 | pie.set_categories(labels) |
|
341 | pie.height = 6.6 |
|
342 | pie.width = 9 |
|
343 | s1 = pie.series[0] |
|
344 | s1.dLbls = DataLabelList() |
|
345 | s1.dLbls.showCatName = False |
|
346 | s1.dLbls.showVal = False |
|
347 | s1.dLbls.showPercent = True |
|
348 | ws.add_chart(pie, "D14") |
|
349 | ||
350 | #################################################################################################################### |
|
351 | current_row_number = 20 |
|
352 | if "subtotals" not in reporting_period_data.keys() or \ |
|
353 | reporting_period_data['subtotals'] is None or \ |
|
354 | len(reporting_period_data['subtotals']) == 0: |
|
355 | pass |
|
356 | else: |
|
357 | ws['B' + str(current_row_number)].font = title_font |
|
358 | ws['B' + str(current_row_number)] = name + ' ' + _('Costs Proportion') |
|
359 | ||
360 | current_row_number += 1 |
|
361 | ||
362 | table_start_row_number = current_row_number |
|
363 | ||
364 | ws['B' + str(current_row_number)].fill = table_fill |
|
365 | ws['B' + str(current_row_number)].font = name_font |
|
366 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
|
367 | ws['B' + str(current_row_number)].border = f_border |
|
368 | ||
369 | ws['C' + str(current_row_number)].fill = table_fill |
|
370 | ws['C' + str(current_row_number)].font = name_font |
|
371 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
|
372 | ws['C' + str(current_row_number)].border = f_border |
|
373 | ws['C' + str(current_row_number)] = _('Costs Proportion') |
|
374 | ||
375 | current_row_number += 1 |
|
376 | ||
377 | ca_len = len(reporting_period_data['names']) |
|
378 | ||
379 | for i in range(0, ca_len): |
|
380 | ws['B' + str(current_row_number)].font = title_font |
|
381 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
|
382 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
|
383 | ws['B' + str(current_row_number)].border = f_border |
|
384 | ||
385 | ws['C' + str(current_row_number)].font = title_font |
|
386 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
|
387 | ws['C' + str(current_row_number)].border = f_border |
|
388 | ws['C' + str(current_row_number)] = round2(reporting_period_data['subtotals'][i], 2) |
|
389 | ||
390 | current_row_number += 1 |
|
391 | ||
392 | table_end_row_number = current_row_number - 1 |
|
393 | ||
394 | pie = PieChart() |
|
395 | pie.title = name + ' ' + _('Costs Proportion') |
|
396 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
|
397 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
|
398 | pie.add_data(pie_data, titles_from_data=True) |
|
399 | pie.set_categories(labels) |
|
400 | pie.height = 6.6 |
|
401 | pie.width = 9 |
|
402 | s1 = pie.series[0] |
|
403 | s1.dLbls = DataLabelList() |
|
404 | s1.dLbls.showCatName = False |
|
405 | s1.dLbls.showVal = False |
|
406 | s1.dLbls.showPercent = True |
|
407 | table_cell = 'D' + str(table_start_row_number) |
|
408 | ws.add_chart(pie, table_cell) |
|
409 | ||
410 | if ca_len < 4: |
|
411 | current_row_number = current_row_number - ca_len + 4 |
|
412 | ||
413 | current_row_number += 1 |
|
414 | ||
415 | #################################################################################################################### |
|
416 | table_start_draw_flag = current_row_number + 1 |
|
417 | ||
418 | if "timestamps" not in reporting_period_data.keys() or \ |
|
419 | reporting_period_data['timestamps'] is None or \ |
|
420 | len(reporting_period_data['timestamps']) == 0: |
|
421 | pass |
|
422 | else: |
|
423 | if not is_base_period_timestamp_exists_flag: |
|
424 | reporting_period_data = report['reporting_period'] |
|
425 | times = reporting_period_data['timestamps'] |
|
426 | ca_len = len(report['reporting_period']['names']) |
|
427 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
|
428 | ws['B' + str(current_row_number)].font = title_font |
|
429 | ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data') |
|
430 | ||
431 | current_row_number += 1 |
|
432 | # 1: Stand for blank line 2: Stand for title |
|
433 | current_row_number += ca_len * 6 + real_timestamps_len * 6 + 1 + 2 |
|
434 | table_start_row_number = current_row_number |
|
435 | ||
436 | time = times[0] |
|
437 | has_data = False |
|
438 | ||
439 | if len(time) > 0: |
|
440 | has_data = True |
|
441 | ||
442 | if has_data: |
|
443 | ||
444 | ws.row_dimensions[current_row_number].height = 60 |
|
445 | current_col_number = 2 |
|
446 | col = format_cell.get_column_letter(current_col_number) |
|
447 | ws[col + str(current_row_number)].fill = table_fill |
|
448 | ws[col + str(current_row_number)].font = title_font |
|
449 | ws[col + str(current_row_number)].border = f_border |
|
450 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
451 | ws[col + str(current_row_number)] = _('Datetime') |
|
452 | ||
453 | for i in range(0, ca_len): |
|
454 | current_col_number += 1 |
|
455 | col = format_cell.get_column_letter(current_col_number) |
|
456 | ||
457 | ws[col + str(current_row_number)].fill = table_fill |
|
458 | ws[col + str(current_row_number)].font = title_font |
|
459 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
460 | ws[col + str(current_row_number)] = reporting_period_data['names'][i] + \ |
|
461 | " (" + reporting_period_data['units'][i] + ")" |
|
462 | ws[col + str(current_row_number)].border = f_border |
|
463 | ||
464 | current_col_number += 1 |
|
465 | col = format_cell.get_column_letter(current_col_number) |
|
466 | ws[col + str(current_row_number)].fill = table_fill |
|
467 | ws[col + str(current_row_number)].font = title_font |
|
468 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
469 | ws[col + str(current_row_number)] = _('Total') + '(' + report['reporting_period']['total_unit'] + ')' |
|
470 | ws[col + str(current_row_number)].border = f_border |
|
471 | ||
472 | current_row_number += 1 |
|
473 | ||
474 | for i in range(0, len(time)): |
|
475 | current_col_number = 2 |
|
476 | col = format_cell.get_column_letter(current_col_number) |
|
477 | ||
478 | ws[col + str(current_row_number)].font = title_font |
|
479 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
480 | ws[col + str(current_row_number)] = time[i] |
|
481 | ws[col + str(current_row_number)].border = f_border |
|
482 | ||
483 | total = Decimal(0.0) |
|
484 | ||
485 | for j in range(0, ca_len): |
|
486 | current_col_number += 1 |
|
487 | col = format_cell.get_column_letter(current_col_number) |
|
488 | ||
489 | ws[col + str(current_row_number)].font = title_font |
|
490 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
491 | ws[col + str(current_row_number)] = round2(reporting_period_data['values'][j][i], 2) |
|
492 | total += reporting_period_data['values'][j][i] |
|
493 | ws[col + str(current_row_number)].border = f_border |
|
494 | ||
495 | current_col_number += 1 |
|
496 | col = format_cell.get_column_letter(current_col_number) |
|
497 | ws[col + str(current_row_number)].font = title_font |
|
498 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
499 | ws[col + str(current_row_number)] = round2(total, 2) |
|
500 | ws[col + str(current_row_number)].border = f_border |
|
501 | ||
502 | current_row_number += 1 |
|
503 | ||
504 | table_end_row_number = current_row_number - 1 |
|
505 | ||
506 | current_col_number = 2 |
|
507 | col = format_cell.get_column_letter(current_col_number) |
|
508 | ||
509 | ws[col + str(current_row_number)].font = title_font |
|
510 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
511 | ws[col + str(current_row_number)] = _('Subtotal') |
|
512 | ws[col + str(current_row_number)].border = f_border |
|
513 | ||
514 | subtotals = Decimal(0.0) |
|
515 | ||
516 | for i in range(0, ca_len): |
|
517 | current_col_number += 1 |
|
518 | col = format_cell.get_column_letter(current_col_number) |
|
519 | ws[col + str(current_row_number)].font = title_font |
|
520 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
521 | ws[col + str(current_row_number)] = round2(reporting_period_data['subtotals'][i], 2) |
|
522 | subtotals += reporting_period_data['subtotals'][i] |
|
523 | ws[col + str(current_row_number)].border = f_border |
|
524 | ||
525 | # line |
|
526 | line = LineChart() |
|
527 | line.title = _('Reporting Period Costs') + ' - ' \ |
|
528 | + reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
|
529 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
|
530 | line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, |
|
531 | max_row=table_end_row_number) |
|
532 | line.add_data(line_data, titles_from_data=True) |
|
533 | line.set_categories(labels) |
|
534 | line_data = line.series[0] |
|
535 | line_data.marker.symbol = "auto" |
|
536 | line_data.smooth = True |
|
537 | line.x_axis.crosses = 'min' |
|
538 | line.height = 8.25 |
|
539 | line.width = 24 |
|
540 | chart_col = 'B' |
|
541 | chart_cell = chart_col + str(table_start_draw_flag + 6 * i) |
|
542 | ws.add_chart(line, chart_cell) |
|
543 | ||
544 | current_col_number += 1 |
|
545 | col = format_cell.get_column_letter(current_col_number) |
|
546 | ws[col + str(current_row_number)].font = title_font |
|
547 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
548 | ws[col + str(current_row_number)] = round2(subtotals, 2) |
|
549 | ws[col + str(current_row_number)].border = f_border |
|
550 | ||
551 | current_row_number += 2 |
|
552 | else: |
|
553 | base_period_data = report['base_period'] |
|
554 | reporting_period_data = report['reporting_period'] |
|
555 | base_period_timestamps = base_period_data['timestamps'] |
|
556 | reporting_period_timestamps = reporting_period_data['timestamps'] |
|
557 | # Tip: |
|
558 | # base_period_data['names'] == reporting_period_data['names'] |
|
559 | # base_period_data['units'] == reporting_period_data['units'] |
|
560 | base_period_data_ca_len = len(base_period_data['names']) |
|
561 | reporting_period_data_ca_len = len(reporting_period_data['names']) |
|
562 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
|
563 | ws['B' + str(current_row_number)].font = title_font |
|
564 | ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data') |
|
565 | ||
566 | current_row_number += 1 |
|
567 | # 1: Stand for blank line 2: Stand for title |
|
568 | current_row_number += reporting_period_data_ca_len * 6 + real_timestamps_len * 6 + 1 + 2 |
|
569 | table_start_row_number = current_row_number |
|
570 | ||
571 | has_data = False |
|
572 | ||
573 | if len(base_period_timestamps[0]) or len(reporting_period_timestamps[0]) > 0: |
|
574 | has_data = True |
|
575 | ||
576 | if has_data: |
|
577 | ws.row_dimensions[current_row_number].height = 60 |
|
578 | current_col_number = 2 |
|
579 | col = format_cell.get_column_letter(current_col_number) |
|
580 | ws[col + str(current_row_number)].fill = table_fill |
|
581 | ws[col + str(current_row_number)].font = title_font |
|
582 | ws[col + str(current_row_number)].border = f_border |
|
583 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
584 | ws[col + str(current_row_number)] = _('Base Period') + " - " + _('Datetime') |
|
585 | ||
586 | for i in range(0, base_period_data_ca_len): |
|
587 | current_col_number += 1 |
|
588 | col = format_cell.get_column_letter(current_col_number) |
|
589 | ||
590 | ws[col + str(current_row_number)].fill = table_fill |
|
591 | ws[col + str(current_row_number)].font = title_font |
|
592 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
593 | ws[col + str(current_row_number)] = _('Base Period') + " - " + base_period_data['names'][i] + \ |
|
594 | " (" + base_period_data['units'][i] + ")" |
|
595 | ws[col + str(current_row_number)].border = f_border |
|
596 | ||
597 | current_col_number += 1 |
|
598 | col = format_cell.get_column_letter(current_col_number) |
|
599 | ws[col + str(current_row_number)].fill = table_fill |
|
600 | ws[col + str(current_row_number)].font = title_font |
|
601 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
602 | ws[col + str(current_row_number)] = _('Base Period') + " - " \ |
|
603 | + _('Total') + '(' + report['reporting_period']['total_unit'] + ')' |
|
604 | ws[col + str(current_row_number)].border = f_border |
|
605 | ||
606 | current_col_number += 1 |
|
607 | col = format_cell.get_column_letter(current_col_number) |
|
608 | ||
609 | ws[col + str(current_row_number)].fill = table_fill |
|
610 | ws[col + str(current_row_number)].font = title_font |
|
611 | ws[col + str(current_row_number)].border = f_border |
|
612 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
613 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " + _('Datetime') |
|
614 | ||
615 | for i in range(0, reporting_period_data_ca_len): |
|
616 | current_col_number += 1 |
|
617 | col = format_cell.get_column_letter(current_col_number) |
|
618 | ws[col + str(current_row_number)].fill = table_fill |
|
619 | ws[col + str(current_row_number)].font = title_font |
|
620 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
621 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " \ |
|
622 | + reporting_period_data['names'][i] + " (" + \ |
|
623 | reporting_period_data['units'][i] + ")" |
|
624 | ws[col + str(current_row_number)].border = f_border |
|
625 | ||
626 | current_col_number += 1 |
|
627 | col = format_cell.get_column_letter(current_col_number) |
|
628 | ws[col + str(current_row_number)].fill = table_fill |
|
629 | ws[col + str(current_row_number)].font = title_font |
|
630 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
631 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " \ |
|
632 | + _('Total') + '(' + report['reporting_period']['total_unit'] + ')' |
|
633 | ws[col + str(current_row_number)].border = f_border |
|
634 | ||
635 | current_row_number += 1 |
|
636 | ||
637 | max_timestamps_len = len(base_period_timestamps[0]) \ |
|
638 | if len(base_period_timestamps[0]) >= len(reporting_period_timestamps[0]) \ |
|
639 | else len(reporting_period_timestamps[0]) |
|
640 | ||
641 | for i in range(0, max_timestamps_len): |
|
642 | current_col_number = 2 |
|
643 | col = format_cell.get_column_letter(current_col_number) |
|
644 | ws[col + str(current_row_number)].font = title_font |
|
645 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
646 | ws[col + str(current_row_number)] = base_period_timestamps[0][i] \ |
|
647 | if i < len(base_period_timestamps[0]) else None |
|
648 | ws[col + str(current_row_number)].border = f_border |
|
649 | ||
650 | base_period_total = Decimal(0.0) |
|
651 | ||
652 | for j in range(0, base_period_data_ca_len): |
|
653 | current_col_number += 1 |
|
654 | col = format_cell.get_column_letter(current_col_number) |
|
655 | ||
656 | ws[col + str(current_row_number)].font = title_font |
|
657 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
658 | ws[col + str(current_row_number)] = round2(base_period_data['values'][j][i], 2) \ |
|
659 | if i < len(base_period_data['values'][j]) else None |
|
660 | if i < len(base_period_timestamps[0]): |
|
661 | base_period_total += base_period_data['values'][j][i] |
|
662 | ws[col + str(current_row_number)].border = f_border |
|
663 | ||
664 | current_col_number += 1 |
|
665 | col = format_cell.get_column_letter(current_col_number) |
|
666 | ws[col + str(current_row_number)].font = title_font |
|
667 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
668 | ws[col + str(current_row_number)] = round2(base_period_total, 2) \ |
|
669 | if i < len(base_period_timestamps[0]) else None |
|
670 | ws[col + str(current_row_number)].border = f_border |
|
671 | ||
672 | current_col_number += 1 |
|
673 | col = format_cell.get_column_letter(current_col_number) |
|
674 | ||
675 | ws[col + str(current_row_number)].font = title_font |
|
676 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
677 | ws[col + str(current_row_number)] = reporting_period_timestamps[0][i] \ |
|
678 | if i < len(reporting_period_timestamps[0]) else None |
|
679 | ws[col + str(current_row_number)].border = f_border |
|
680 | ||
681 | reporting_period_total = Decimal(0.0) |
|
682 | ||
683 | for j in range(0, reporting_period_data_ca_len): |
|
684 | current_col_number += 1 |
|
685 | col = format_cell.get_column_letter(current_col_number) |
|
686 | ||
687 | ws[col + str(current_row_number)].font = title_font |
|
688 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
689 | ws[col + str(current_row_number)] = round2(reporting_period_data['values'][j][i], 2) \ |
|
690 | if i < len(reporting_period_data['values'][j]) else None |
|
691 | if i < len(reporting_period_timestamps[0]): |
|
692 | reporting_period_total += reporting_period_data['values'][j][i] |
|
693 | ws[col + str(current_row_number)].border = f_border |
|
694 | ||
695 | current_col_number += 1 |
|
696 | col = format_cell.get_column_letter(current_col_number) |
|
697 | ws[col + str(current_row_number)].font = title_font |
|
698 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
699 | ws[col + str(current_row_number)] = round2(reporting_period_total, 2) \ |
|
700 | if i < len(reporting_period_timestamps[0]) else None |
|
701 | ws[col + str(current_row_number)].border = f_border |
|
702 | ||
703 | current_row_number += 1 |
|
704 | ||
705 | current_col_number = 2 |
|
706 | col = format_cell.get_column_letter(current_col_number) |
|
707 | ws[col + str(current_row_number)].font = title_font |
|
708 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
709 | ws[col + str(current_row_number)] = _('Subtotal') |
|
710 | ws[col + str(current_row_number)].border = f_border |
|
711 | ||
712 | base_period_subtotals = Decimal(0.0) |
|
713 | ||
714 | for i in range(0, base_period_data_ca_len): |
|
715 | current_col_number += 1 |
|
716 | col = format_cell.get_column_letter(current_col_number) |
|
717 | ws[col + str(current_row_number)].font = title_font |
|
718 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
719 | ws[col + str(current_row_number)] = round2(base_period_data['subtotals'][i], 2) |
|
720 | base_period_subtotals += base_period_data['subtotals'][i] |
|
721 | ws[col + str(current_row_number)].border = f_border |
|
722 | ||
723 | current_col_number += 1 |
|
724 | col = format_cell.get_column_letter(current_col_number) |
|
725 | ws[col + str(current_row_number)].font = title_font |
|
726 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
727 | ws[col + str(current_row_number)] = round2(base_period_subtotals, 2) |
|
728 | ws[col + str(current_row_number)].border = f_border |
|
729 | ||
730 | current_col_number += 1 |
|
731 | col = format_cell.get_column_letter(current_col_number) |
|
732 | ||
733 | ws[col + str(current_row_number)].font = title_font |
|
734 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
735 | ws[col + str(current_row_number)] = _('Subtotal') |
|
736 | ws[col + str(current_row_number)].border = f_border |
|
737 | ||
738 | reporting_period_subtotals = Decimal(0.0) |
|
739 | ||
740 | for i in range(0, reporting_period_data_ca_len): |
|
741 | current_col_number += 1 |
|
742 | col = format_cell.get_column_letter(current_col_number) |
|
743 | ws[col + str(current_row_number)].font = title_font |
|
744 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
745 | ws[col + str(current_row_number)] = round2(reporting_period_data['subtotals'][i], 2) |
|
746 | reporting_period_subtotals += reporting_period_data['subtotals'][i] |
|
747 | ws[col + str(current_row_number)].border = f_border |
|
748 | ||
749 | current_col_number += 1 |
|
750 | col = format_cell.get_column_letter(current_col_number) |
|
751 | ws[col + str(current_row_number)].font = title_font |
|
752 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
753 | ws[col + str(current_row_number)] = round2(reporting_period_subtotals, 2) |
|
754 | ws[col + str(current_row_number)].border = f_border |
|
755 | ||
756 | for i in range(0, reporting_period_data_ca_len): |
|
757 | # line |
|
758 | line = LineChart() |
|
759 | line.title = _('Base Period Costs') + " / " \ |
|
760 | + _('Reporting Period Costs') + ' - ' \ |
|
761 | + reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
|
762 | labels = Reference(ws, min_col=2 + base_period_data_ca_len + 1 + 1, |
|
763 | min_row=table_start_row_number + 1, |
|
764 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
|
765 | base_line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, |
|
766 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
|
767 | reporting_line_data = Reference(ws, min_col=3 + base_period_data_ca_len + 1 + 1 + i, |
|
768 | min_row=table_start_row_number, |
|
769 | max_row=table_start_row_number |
|
770 | + len(reporting_period_timestamps[0])) |
|
771 | line.add_data(base_line_data, titles_from_data=True) |
|
772 | line.add_data(reporting_line_data, titles_from_data=True) |
|
773 | line.set_categories(labels) |
|
774 | for j in range(len(line.series)): |
|
775 | line.series[j].marker.symbol = "auto" |
|
776 | line.series[j].smooth = True |
|
777 | line.x_axis.crosses = 'min' |
|
778 | line.height = 8.25 |
|
779 | line.width = 24 |
|
780 | chart_col = 'B' |
|
781 | chart_cell = chart_col + str(table_start_draw_flag + 6 * i) |
|
782 | ws.add_chart(line, chart_cell) |
|
783 | ||
784 | current_row_number += 2 |
|
785 | ||
786 | #################################################################################################################### |
|
787 | current_sheet_parameters_row_number = table_start_draw_flag + len(reporting_period_data['names']) * 6 + 1 |
|
788 | if 'parameters' not in report.keys() or \ |
|
789 | report['parameters'] is None or \ |
|
790 | 'names' not in report['parameters'].keys() or \ |
|
791 | report['parameters']['names'] is None or \ |
|
792 | len(report['parameters']['names']) == 0 or \ |
|
793 | 'timestamps' not in report['parameters'].keys() or \ |
|
794 | report['parameters']['timestamps'] is None or \ |
|
795 | len(report['parameters']['timestamps']) == 0 or \ |
|
796 | 'values' not in report['parameters'].keys() or \ |
|
797 | report['parameters']['values'] is None or \ |
|
798 | len(report['parameters']['values']) == 0 or \ |
|
799 | timestamps_data_all_equal_0(report['parameters']['timestamps']): |
|
800 | pass |
|
801 | else: |
|
802 | ################################################################################################################ |
|
803 | # new worksheet |
|
804 | ################################################################################################################ |
|
805 | ||
806 | parameters_data = report['parameters'] |
|
807 | parameters_names_len = len(parameters_data['names']) |
|
808 | ||
809 | file_name = (re.sub(r'[^A-Z]', '', ws.title)) + '_' |
|
810 | parameters_ws = wb.create_sheet(file_name + _('Parameters')) |
|
811 | ||
812 | parameters_timestamps_data_max_len = \ |
|
813 | get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
|
814 | ||
815 | # Row height |
|
816 | parameters_ws.row_dimensions[1].height = 102 |
|
817 | for i in range(2, 7 + 1): |
|
818 | parameters_ws.row_dimensions[i].height = 42 |
|
819 | ||
820 | for i in range(8, parameters_timestamps_data_max_len + 10): |
|
821 | parameters_ws.row_dimensions[i].height = 60 |
|
822 | ||
823 | # Col width |
|
824 | parameters_ws.column_dimensions['A'].width = 1.5 |
|
825 | ||
826 | parameters_ws.column_dimensions['B'].width = 25.0 |
|
827 | ||
828 | for i in range(3, 12 + parameters_names_len * 3): |
|
829 | parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
|
830 | ||
831 | # Img |
|
832 | img = Image("excelexporters/myems.png") |
|
833 | parameters_ws.add_image(img, 'A1') |
|
834 | ||
835 | # Title |
|
836 | parameters_ws['B3'].alignment = b_r_alignment |
|
837 | parameters_ws['B3'] = _('Name') + ':' |
|
838 | parameters_ws['C3'].border = b_border |
|
839 | parameters_ws['C3'].alignment = b_c_alignment |
|
840 | parameters_ws['C3'] = name |
|
841 | ||
842 | parameters_ws['D3'].alignment = b_r_alignment |
|
843 | parameters_ws['D3'] = _('Period Type') + ':' |
|
844 | parameters_ws['E3'].border = b_border |
|
845 | parameters_ws['E3'].alignment = b_c_alignment |
|
846 | parameters_ws['E3'] = period_type |
|
847 | ||
848 | parameters_ws['B4'].alignment = b_r_alignment |
|
849 | parameters_ws['B4'] = _('Reporting Start Datetime') + ':' |
|
850 | parameters_ws['C4'].border = b_border |
|
851 | parameters_ws['C4'].alignment = b_c_alignment |
|
852 | parameters_ws['C4'] = reporting_start_datetime_local |
|
853 | ||
854 | parameters_ws['D4'].alignment = b_r_alignment |
|
855 | parameters_ws['D4'] = _('Reporting End Datetime') + ':' |
|
856 | parameters_ws['E4'].border = b_border |
|
857 | parameters_ws['E4'].alignment = b_c_alignment |
|
858 | parameters_ws['E4'] = reporting_end_datetime_local |
|
859 | ||
860 | parameters_ws_current_row_number = 6 |
|
861 | ||
862 | parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
|
863 | parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' ' + _('Parameters') |
|
864 | ||
865 | parameters_ws_current_row_number += 1 |
|
866 | ||
867 | parameters_table_start_row_number = parameters_ws_current_row_number |
|
868 | ||
869 | parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
|
870 | ||
871 | parameters_ws_current_row_number += 1 |
|
872 | ||
873 | table_current_col_number = 2 |
|
874 | ||
875 | for i in range(0, parameters_names_len): |
|
876 | ||
877 | if len(parameters_data['timestamps'][i]) == 0: |
|
878 | continue |
|
879 | ||
880 | col = format_cell.get_column_letter(table_current_col_number) |
|
881 | ||
882 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
|
883 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
|
884 | ||
885 | col = format_cell.get_column_letter(table_current_col_number + 1) |
|
886 | ||
887 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
|
888 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
|
889 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = name_font |
|
890 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = c_c_alignment |
|
891 | parameters_ws[col + str(parameters_ws_current_row_number - 1)] = parameters_data['names'][i] |
|
892 | ||
893 | table_current_row_number = parameters_ws_current_row_number |
|
894 | ||
895 | for j, value in enumerate(list(parameters_data['timestamps'][i])): |
|
896 | col = format_cell.get_column_letter(table_current_col_number) |
|
897 | ||
898 | parameters_ws[col + str(table_current_row_number)].border = f_border |
|
899 | parameters_ws[col + str(table_current_row_number)].font = title_font |
|
900 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
|
901 | parameters_ws[col + str(table_current_row_number)] = value |
|
902 | ||
903 | col = format_cell.get_column_letter(table_current_col_number + 1) |
|
904 | ||
905 | parameters_ws[col + str(table_current_row_number)].border = f_border |
|
906 | parameters_ws[col + str(table_current_row_number)].font = title_font |
|
907 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
|
908 | parameters_ws[col + str(table_current_row_number)] = round2(parameters_data['values'][i][j], 2) |
|
909 | ||
910 | table_current_row_number += 1 |
|
911 | ||
912 | table_current_col_number = table_current_col_number + 3 |
|
913 | ||
914 | ################################################################################################################ |
|
915 | # parameters chart and parameters table |
|
916 | ################################################################################################################ |
|
917 | ||
918 | ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
|
919 | ws['B' + str(current_sheet_parameters_row_number)] = name + ' ' + _('Parameters') |
|
920 | ||
921 | current_sheet_parameters_row_number += 1 |
|
922 | ||
923 | chart_start_row_number = current_sheet_parameters_row_number |
|
924 | ||
925 | col_index = 0 |
|
926 | ||
927 | for i in range(0, parameters_names_len): |
|
928 | ||
929 | if len(parameters_data['timestamps'][i]) == 0: |
|
930 | continue |
|
931 | ||
932 | line = LineChart() |
|
933 | data_col = 3 + col_index * 3 |
|
934 | labels_col = 2 + col_index * 3 |
|
935 | col_index += 1 |
|
936 | line.title = _('Parameters') + ' - ' + \ |
|
937 | parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
|
938 | labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
|
939 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
|
940 | line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
|
941 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
|
942 | line.add_data(line_data, titles_from_data=True) |
|
943 | line.set_categories(labels) |
|
944 | line_data = line.series[0] |
|
945 | line_data.marker.symbol = "auto" |
|
946 | line_data.smooth = True |
|
947 | line.x_axis.crosses = 'min' |
|
948 | line.height = 8.25 |
|
949 | line.width = 24 |
|
950 | chart_col = 'B' |
|
951 | chart_cell = chart_col + str(chart_start_row_number) |
|
952 | chart_start_row_number += 6 |
|
953 | ws.add_chart(line, chart_cell) |
|
954 | ||
955 | current_sheet_parameters_row_number = chart_start_row_number |
|
956 | ||
957 | current_sheet_parameters_row_number += 1 |
|
958 | #################################################################################################################### |
|
959 | filename = str(uuid.uuid4()) + '.xlsx' |
|
960 | wb.save(filename) |
|
961 | ||
962 | return filename |
|
963 | ||
964 | ||
965 | def timestamps_data_all_equal_0(lists): |
@@ 70-960 (lines=891) @@ | ||
67 | return base64_message |
|
68 | ||
69 | ||
70 | def generate_excel(report, |
|
71 | name, |
|
72 | base_period_start_datetime_local, |
|
73 | base_period_end_datetime_local, |
|
74 | reporting_start_datetime_local, |
|
75 | reporting_end_datetime_local, |
|
76 | period_type, |
|
77 | language): |
|
78 | trans = get_translation(language) |
|
79 | trans.install() |
|
80 | _ = trans.gettext |
|
81 | ||
82 | wb = Workbook() |
|
83 | ws = wb.active |
|
84 | ws.title = "TenantCarbon" |
|
85 | ||
86 | # Row height |
|
87 | ws.row_dimensions[1].height = 102 |
|
88 | for i in range(2, 2000 + 1): |
|
89 | ws.row_dimensions[i].height = 42 |
|
90 | ||
91 | # Col width |
|
92 | ws.column_dimensions['A'].width = 1.5 |
|
93 | ||
94 | ws.column_dimensions['B'].width = 25.0 |
|
95 | ||
96 | for i in range(ord('C'), ord('Z')): |
|
97 | ws.column_dimensions[chr(i)].width = 15.0 |
|
98 | ||
99 | # Font |
|
100 | name_font = Font(name='Arial', size=15, bold=True) |
|
101 | title_font = Font(name='Arial', size=15, bold=True) |
|
102 | ||
103 | table_fill = PatternFill(fill_type='solid', fgColor='90ee90') |
|
104 | f_border = Border(left=Side(border_style='medium'), |
|
105 | right=Side(border_style='medium'), |
|
106 | bottom=Side(border_style='medium'), |
|
107 | top=Side(border_style='medium') |
|
108 | ) |
|
109 | b_border = Border( |
|
110 | bottom=Side(border_style='medium'), |
|
111 | ) |
|
112 | ||
113 | b_c_alignment = Alignment(vertical='bottom', |
|
114 | horizontal='center', |
|
115 | text_rotation=0, |
|
116 | wrap_text=True, |
|
117 | shrink_to_fit=False, |
|
118 | indent=0) |
|
119 | c_c_alignment = Alignment(vertical='center', |
|
120 | horizontal='center', |
|
121 | text_rotation=0, |
|
122 | wrap_text=True, |
|
123 | shrink_to_fit=False, |
|
124 | indent=0) |
|
125 | b_r_alignment = Alignment(vertical='bottom', |
|
126 | horizontal='right', |
|
127 | text_rotation=0, |
|
128 | wrap_text=True, |
|
129 | shrink_to_fit=False, |
|
130 | indent=0) |
|
131 | ||
132 | # Img |
|
133 | img = Image("excelexporters/myems.png") |
|
134 | ws.add_image(img, 'A1') |
|
135 | ||
136 | # Title |
|
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'] = name |
|
142 | ||
143 | ws['D3'].alignment = b_r_alignment |
|
144 | ws['D3'] = _('Period Type') + ':' |
|
145 | ws['E3'].border = b_border |
|
146 | ws['E3'].alignment = b_c_alignment |
|
147 | ws['E3'] = period_type |
|
148 | ||
149 | ws['B4'].alignment = b_r_alignment |
|
150 | ws['B4'] = _('Reporting Start Datetime') + ':' |
|
151 | ws['C4'].border = b_border |
|
152 | ws['C4'].alignment = b_c_alignment |
|
153 | ws['C4'] = reporting_start_datetime_local |
|
154 | ||
155 | ws['D4'].alignment = b_r_alignment |
|
156 | ws['D4'] = _('Reporting End Datetime') + ':' |
|
157 | ws['E4'].border = b_border |
|
158 | ws['E4'].alignment = b_c_alignment |
|
159 | ws['E4'] = reporting_end_datetime_local |
|
160 | ||
161 | is_base_period_timestamp_exists_flag = is_base_period_timestamp_exists(report['base_period']) |
|
162 | ||
163 | if is_base_period_timestamp_exists_flag: |
|
164 | ws['B5'].alignment = b_r_alignment |
|
165 | ws['B5'] = _('Base Period Start Datetime') + ':' |
|
166 | ws['C5'].border = b_border |
|
167 | ws['C5'].alignment = b_c_alignment |
|
168 | ws['C5'] = base_period_start_datetime_local |
|
169 | ||
170 | ws['D5'].alignment = b_r_alignment |
|
171 | ws['D5'] = _('Base Period End Datetime') + ':' |
|
172 | ws['E5'].border = b_border |
|
173 | ws['E5'].alignment = b_c_alignment |
|
174 | ws['E5'] = base_period_end_datetime_local |
|
175 | ||
176 | if "reporting_period" not in report.keys() or \ |
|
177 | "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
|
178 | filename = str(uuid.uuid4()) + '.xlsx' |
|
179 | wb.save(filename) |
|
180 | ||
181 | return filename |
|
182 | ||
183 | #################################################################################################################### |
|
184 | ||
185 | reporting_period_data = report['reporting_period'] |
|
186 | ||
187 | if "names" not in reporting_period_data.keys() or \ |
|
188 | reporting_period_data['names'] is None or \ |
|
189 | len(reporting_period_data['names']) == 0: |
|
190 | for i in range(7, 11 + 1): |
|
191 | ws.row_dimensions[i].height = 0.1 |
|
192 | else: |
|
193 | ws['B7'].font = title_font |
|
194 | ws['B7'] = name + ' ' + _('Reporting Period Carbon Dioxide Emissions') |
|
195 | ||
196 | category = reporting_period_data['names'] |
|
197 | ca_len = len(category) |
|
198 | ||
199 | ws.row_dimensions[8].height = 60 |
|
200 | ws['B8'].fill = table_fill |
|
201 | ws['B8'].border = f_border |
|
202 | ||
203 | ws['B9'].font = title_font |
|
204 | ws['B9'].alignment = c_c_alignment |
|
205 | ws['B9'] = _('Carbon Dioxide Emissions') |
|
206 | ws['B9'].border = f_border |
|
207 | ||
208 | ws['B10'].font = title_font |
|
209 | ws['B10'].alignment = c_c_alignment |
|
210 | ws['B10'] = _('Per Unit Area') |
|
211 | ws['B10'].border = f_border |
|
212 | ||
213 | ws['B11'].font = title_font |
|
214 | ws['B11'].alignment = c_c_alignment |
|
215 | ws['B11'] = _('Increment Rate') |
|
216 | ws['B11'].border = f_border |
|
217 | ||
218 | col = 'B' |
|
219 | ||
220 | for i in range(0, ca_len): |
|
221 | col = chr(ord('C') + i) |
|
222 | ws[col + '8'].fill = table_fill |
|
223 | ws[col + '8'].font = name_font |
|
224 | ws[col + '8'].alignment = c_c_alignment |
|
225 | ws[col + '8'] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
|
226 | ws[col + '8'].border = f_border |
|
227 | ||
228 | ws[col + '9'].font = name_font |
|
229 | ws[col + '9'].alignment = c_c_alignment |
|
230 | ws[col + '9'] = round2(reporting_period_data['subtotals'][i], 2) |
|
231 | ws[col + '9'].border = f_border |
|
232 | ||
233 | ws[col + '10'].font = name_font |
|
234 | ws[col + '10'].alignment = c_c_alignment |
|
235 | ws[col + '10'] = round2(reporting_period_data['subtotals_per_unit_area'][i], 2) |
|
236 | ws[col + '10'].border = f_border |
|
237 | ||
238 | ws[col + '11'].font = name_font |
|
239 | ws[col + '11'].alignment = c_c_alignment |
|
240 | ws[col + '11'] = str(round2(reporting_period_data['increment_rates'][i] * 100, 2)) + "%" \ |
|
241 | if reporting_period_data['increment_rates'][i] is not None else "-" |
|
242 | ws[col + '11'].border = f_border |
|
243 | ||
244 | end_col = chr(ord(col)+1) |
|
245 | ws[end_col + '8'].fill = table_fill |
|
246 | ws[end_col + '8'].font = name_font |
|
247 | ws[end_col + '8'].alignment = c_c_alignment |
|
248 | ws[end_col + '8'] = _("Total") + " (" + reporting_period_data['total_unit'] + ")" |
|
249 | ws[end_col + '8'].border = f_border |
|
250 | ||
251 | ws[end_col + '9'].font = name_font |
|
252 | ws[end_col + '9'].alignment = c_c_alignment |
|
253 | ws[end_col + '9'] = round2(reporting_period_data['total'], 2) |
|
254 | ws[end_col + '9'].border = f_border |
|
255 | ||
256 | ws[end_col + '10'].font = name_font |
|
257 | ws[end_col + '10'].alignment = c_c_alignment |
|
258 | ws[end_col + '10'] = round2(reporting_period_data['total_per_unit_area'], 2) |
|
259 | ws[end_col + '10'].border = f_border |
|
260 | ||
261 | ws[end_col + '11'].font = name_font |
|
262 | ws[end_col + '11'].alignment = c_c_alignment |
|
263 | ws[end_col + '11'] = str(round2(reporting_period_data['total_increment_rate'] * 100, 2)) + "%" \ |
|
264 | if reporting_period_data['total_increment_rate'] is not None else "-" |
|
265 | ws[end_col + '11'].border = f_border |
|
266 | ||
267 | #################################################################################################################### |
|
268 | if "toppeaks" not in reporting_period_data.keys() or \ |
|
269 | reporting_period_data['toppeaks'] is None or \ |
|
270 | len(reporting_period_data['toppeaks']) == 0: |
|
271 | for i in range(13, 19 + 1): |
|
272 | ws.row_dimensions[i].height = 0.1 |
|
273 | else: |
|
274 | electricity_index = -1 |
|
275 | for i in range(len(reporting_period_data['energy_category_ids'])): |
|
276 | if reporting_period_data['energy_category_ids'][i] == 1: |
|
277 | electricity_index = i |
|
278 | break |
|
279 | ||
280 | ws['B13'].font = title_font |
|
281 | ws['B13'] = name + ' ' + _('Electricity Carbon Dioxide Emissions by Time-Of-Use') |
|
282 | ||
283 | ws['B14'].fill = table_fill |
|
284 | ws['B14'].font = name_font |
|
285 | ws['B14'].alignment = c_c_alignment |
|
286 | ws['B14'].border = f_border |
|
287 | ||
288 | ws['C14'].fill = table_fill |
|
289 | ws['C14'].font = name_font |
|
290 | ws['C14'].alignment = c_c_alignment |
|
291 | ws['C14'].border = f_border |
|
292 | ws['C14'] = _('Electricity Carbon Dioxide Emissions by Time-Of-Use') |
|
293 | ||
294 | ws['B15'].font = title_font |
|
295 | ws['B15'].alignment = c_c_alignment |
|
296 | ws['B15'] = _('TopPeak') |
|
297 | ws['B15'].border = f_border |
|
298 | ||
299 | ws['C15'].font = title_font |
|
300 | ws['C15'].alignment = c_c_alignment |
|
301 | ws['C15'].border = f_border |
|
302 | ws['C15'] = round2(reporting_period_data['toppeaks'][electricity_index], 2) if electricity_index >= 0 else "-" |
|
303 | ||
304 | ws['B16'].font = title_font |
|
305 | ws['B16'].alignment = c_c_alignment |
|
306 | ws['B16'] = _('OnPeak') |
|
307 | ws['B16'].border = f_border |
|
308 | ||
309 | ws['C16'].font = title_font |
|
310 | ws['C16'].alignment = c_c_alignment |
|
311 | ws['C16'].border = f_border |
|
312 | ws['C16'] = round2(reporting_period_data['onpeaks'][electricity_index], 2) if electricity_index >= 0 else "-" |
|
313 | ||
314 | ws['B17'].font = title_font |
|
315 | ws['B17'].alignment = c_c_alignment |
|
316 | ws['B17'] = _('MidPeak') |
|
317 | ws['B17'].border = f_border |
|
318 | ||
319 | ws['C17'].font = title_font |
|
320 | ws['C17'].alignment = c_c_alignment |
|
321 | ws['C17'].border = f_border |
|
322 | ws['C17'] = round2(reporting_period_data['midpeaks'][electricity_index], 2) if electricity_index >= 0 else "-" |
|
323 | ||
324 | ws['B18'].font = title_font |
|
325 | ws['B18'].alignment = c_c_alignment |
|
326 | ws['B18'] = _('OffPeak') |
|
327 | ws['B18'].border = f_border |
|
328 | ||
329 | ws['C18'].font = title_font |
|
330 | ws['C18'].alignment = c_c_alignment |
|
331 | ws['C18'].border = f_border |
|
332 | ws['C18'] = round2(reporting_period_data['offpeaks'][electricity_index], 2) if electricity_index >= 0 else "-" |
|
333 | ||
334 | pie = PieChart() |
|
335 | pie.title = name + ' ' + _('Electricity Carbon Dioxide Emissions by Time-Of-Use') |
|
336 | labels = Reference(ws, min_col=2, min_row=15, max_row=18) |
|
337 | pie_data = Reference(ws, min_col=3, min_row=14, max_row=18) |
|
338 | pie.add_data(pie_data, titles_from_data=True) |
|
339 | pie.set_categories(labels) |
|
340 | pie.height = 6.6 |
|
341 | pie.width = 9 |
|
342 | s1 = pie.series[0] |
|
343 | s1.dLbls = DataLabelList() |
|
344 | s1.dLbls.showCatName = False |
|
345 | s1.dLbls.showVal = False |
|
346 | s1.dLbls.showPercent = True |
|
347 | ws.add_chart(pie, "D14") |
|
348 | ||
349 | #################################################################################################################### |
|
350 | current_row_number = 20 |
|
351 | if "subtotals" not in reporting_period_data.keys() or \ |
|
352 | reporting_period_data['subtotals'] is None or \ |
|
353 | len(reporting_period_data['subtotals']) == 0: |
|
354 | pass |
|
355 | else: |
|
356 | ws['B' + str(current_row_number)].font = title_font |
|
357 | ws['B' + str(current_row_number)] = name + ' ' + _('Carbon Dioxide Emissions Proportion') |
|
358 | ||
359 | current_row_number += 1 |
|
360 | ||
361 | table_start_row_number = current_row_number |
|
362 | ||
363 | ws['B' + str(current_row_number)].fill = table_fill |
|
364 | ws['B' + str(current_row_number)].font = name_font |
|
365 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
|
366 | ws['B' + str(current_row_number)].border = f_border |
|
367 | ||
368 | ws['C' + str(current_row_number)].fill = table_fill |
|
369 | ws['C' + str(current_row_number)].font = name_font |
|
370 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
|
371 | ws['C' + str(current_row_number)].border = f_border |
|
372 | ws['C' + str(current_row_number)] = _('Carbon Dioxide Emissions Proportion') |
|
373 | ||
374 | current_row_number += 1 |
|
375 | ||
376 | ca_len = len(reporting_period_data['names']) |
|
377 | ||
378 | for i in range(0, ca_len): |
|
379 | ws['B' + str(current_row_number)].font = title_font |
|
380 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
|
381 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
|
382 | ws['B' + str(current_row_number)].border = f_border |
|
383 | ||
384 | ws['C' + str(current_row_number)].font = title_font |
|
385 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
|
386 | ws['C' + str(current_row_number)].border = f_border |
|
387 | ws['C' + str(current_row_number)] = round2(reporting_period_data['subtotals'][i], 2) |
|
388 | ||
389 | current_row_number += 1 |
|
390 | ||
391 | table_end_row_number = current_row_number - 1 |
|
392 | ||
393 | pie = PieChart() |
|
394 | pie.title = name + ' ' + _('Carbon Dioxide Emissions Proportion') |
|
395 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
|
396 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
|
397 | pie.add_data(pie_data, titles_from_data=True) |
|
398 | pie.set_categories(labels) |
|
399 | pie.height = 6.6 |
|
400 | pie.width = 9 |
|
401 | s1 = pie.series[0] |
|
402 | s1.dLbls = DataLabelList() |
|
403 | s1.dLbls.showCatName = False |
|
404 | s1.dLbls.showVal = False |
|
405 | s1.dLbls.showPercent = True |
|
406 | table_cell = 'D' + str(table_start_row_number) |
|
407 | ws.add_chart(pie, table_cell) |
|
408 | ||
409 | if ca_len < 4: |
|
410 | current_row_number = current_row_number - ca_len + 4 |
|
411 | ||
412 | current_row_number += 1 |
|
413 | ||
414 | #################################################################################################################### |
|
415 | table_start_draw_flag = current_row_number + 1 |
|
416 | if "timestamps" not in reporting_period_data.keys() or \ |
|
417 | reporting_period_data['timestamps'] is None or \ |
|
418 | len(reporting_period_data['timestamps']) == 0: |
|
419 | pass |
|
420 | else: |
|
421 | if not is_base_period_timestamp_exists_flag: |
|
422 | reporting_period_data = report['reporting_period'] |
|
423 | times = reporting_period_data['timestamps'] |
|
424 | ca_len = len(report['reporting_period']['names']) |
|
425 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
|
426 | ws['B' + str(current_row_number)].font = title_font |
|
427 | ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data') |
|
428 | ||
429 | current_row_number += 1 |
|
430 | # 1: Stand for blank line 2: Stand for title |
|
431 | current_row_number += ca_len * 6 + real_timestamps_len * 6 + 1 + 2 |
|
432 | table_start_row_number = current_row_number |
|
433 | ||
434 | time = times[0] |
|
435 | has_data = False |
|
436 | ||
437 | if len(time) > 0: |
|
438 | has_data = True |
|
439 | ||
440 | if has_data: |
|
441 | ||
442 | ws.row_dimensions[current_row_number].height = 60 |
|
443 | current_col_number = 2 |
|
444 | col = format_cell.get_column_letter(current_col_number) |
|
445 | ws[col + str(current_row_number)].fill = table_fill |
|
446 | ws[col + str(current_row_number)].font = title_font |
|
447 | ws[col + str(current_row_number)].border = f_border |
|
448 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
449 | ws[col + str(current_row_number)] = _('Datetime') |
|
450 | ||
451 | for i in range(0, ca_len): |
|
452 | current_col_number += 1 |
|
453 | col = format_cell.get_column_letter(current_col_number) |
|
454 | ||
455 | ws[col + str(current_row_number)].fill = table_fill |
|
456 | ws[col + str(current_row_number)].font = title_font |
|
457 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
458 | ws[col + str(current_row_number)] = reporting_period_data['names'][i] + \ |
|
459 | " (" + reporting_period_data['units'][i] + ")" |
|
460 | ws[col + str(current_row_number)].border = f_border |
|
461 | ||
462 | current_col_number += 1 |
|
463 | col = format_cell.get_column_letter(current_col_number) |
|
464 | ws[col + str(current_row_number)].fill = table_fill |
|
465 | ws[col + str(current_row_number)].font = title_font |
|
466 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
467 | ws[col + str(current_row_number)] = _('Total') + '(' + report['reporting_period']['total_unit'] + ')' |
|
468 | ws[col + str(current_row_number)].border = f_border |
|
469 | ||
470 | current_row_number += 1 |
|
471 | ||
472 | for i in range(0, len(time)): |
|
473 | current_col_number = 2 |
|
474 | col = format_cell.get_column_letter(current_col_number) |
|
475 | ||
476 | ws[col + str(current_row_number)].font = title_font |
|
477 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
478 | ws[col + str(current_row_number)] = time[i] |
|
479 | ws[col + str(current_row_number)].border = f_border |
|
480 | ||
481 | total = Decimal(0.0) |
|
482 | ||
483 | for j in range(0, ca_len): |
|
484 | current_col_number += 1 |
|
485 | col = format_cell.get_column_letter(current_col_number) |
|
486 | ||
487 | ws[col + str(current_row_number)].font = title_font |
|
488 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
489 | ws[col + str(current_row_number)] = round2(reporting_period_data['values'][j][i], 2) |
|
490 | total += reporting_period_data['values'][j][i] |
|
491 | ws[col + str(current_row_number)].border = f_border |
|
492 | ||
493 | current_col_number += 1 |
|
494 | col = format_cell.get_column_letter(current_col_number) |
|
495 | ws[col + str(current_row_number)].font = title_font |
|
496 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
497 | ws[col + str(current_row_number)] = round2(total, 2) |
|
498 | ws[col + str(current_row_number)].border = f_border |
|
499 | ||
500 | current_row_number += 1 |
|
501 | ||
502 | table_end_row_number = current_row_number - 1 |
|
503 | ||
504 | current_col_number = 2 |
|
505 | col = format_cell.get_column_letter(current_col_number) |
|
506 | ||
507 | ws[col + str(current_row_number)].font = title_font |
|
508 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
509 | ws[col + str(current_row_number)] = _('Subtotal') |
|
510 | ws[col + str(current_row_number)].border = f_border |
|
511 | ||
512 | subtotals = Decimal(0.0) |
|
513 | ||
514 | for i in range(0, ca_len): |
|
515 | current_col_number += 1 |
|
516 | col = format_cell.get_column_letter(current_col_number) |
|
517 | ws[col + str(current_row_number)].font = title_font |
|
518 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
519 | ws[col + str(current_row_number)] = round2(reporting_period_data['subtotals'][i], 2) |
|
520 | subtotals += reporting_period_data['subtotals'][i] |
|
521 | ws[col + str(current_row_number)].border = f_border |
|
522 | ||
523 | # line |
|
524 | line = LineChart() |
|
525 | line.title = _('Reporting Period Carbon Dioxide Emissions') + ' - ' \ |
|
526 | + reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
|
527 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
|
528 | line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, |
|
529 | max_row=table_end_row_number) |
|
530 | line.add_data(line_data, titles_from_data=True) |
|
531 | line.set_categories(labels) |
|
532 | line_data = line.series[0] |
|
533 | line_data.marker.symbol = "auto" |
|
534 | line_data.smooth = True |
|
535 | line.x_axis.crosses = 'min' |
|
536 | line.height = 8.25 |
|
537 | line.width = 24 |
|
538 | chart_col = 'B' |
|
539 | chart_cell = chart_col + str(table_start_draw_flag + 6 * i) |
|
540 | ws.add_chart(line, chart_cell) |
|
541 | ||
542 | current_col_number += 1 |
|
543 | col = format_cell.get_column_letter(current_col_number) |
|
544 | ws[col + str(current_row_number)].font = title_font |
|
545 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
546 | ws[col + str(current_row_number)] = round2(subtotals, 2) |
|
547 | ws[col + str(current_row_number)].border = f_border |
|
548 | ||
549 | current_row_number += 2 |
|
550 | else: |
|
551 | base_period_data = report['base_period'] |
|
552 | reporting_period_data = report['reporting_period'] |
|
553 | base_period_timestamps = base_period_data['timestamps'] |
|
554 | reporting_period_timestamps = reporting_period_data['timestamps'] |
|
555 | # Tip: |
|
556 | # base_period_data['names'] == reporting_period_data['names'] |
|
557 | # base_period_data['units'] == reporting_period_data['units'] |
|
558 | base_period_data_ca_len = len(base_period_data['names']) |
|
559 | reporting_period_data_ca_len = len(reporting_period_data['names']) |
|
560 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
|
561 | ws['B' + str(current_row_number)].font = title_font |
|
562 | ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data') |
|
563 | ||
564 | current_row_number += 1 |
|
565 | # 1: Stand for blank line 2: Stand for title |
|
566 | current_row_number += reporting_period_data_ca_len * 6 + real_timestamps_len * 6 + 1 + 2 |
|
567 | table_start_row_number = current_row_number |
|
568 | ||
569 | has_data = False |
|
570 | ||
571 | if len(base_period_timestamps[0]) or len(reporting_period_timestamps[0]) > 0: |
|
572 | has_data = True |
|
573 | ||
574 | if has_data: |
|
575 | ws.row_dimensions[current_row_number].height = 60 |
|
576 | current_col_number = 2 |
|
577 | col = format_cell.get_column_letter(current_col_number) |
|
578 | ws[col + str(current_row_number)].fill = table_fill |
|
579 | ws[col + str(current_row_number)].font = title_font |
|
580 | ws[col + str(current_row_number)].border = f_border |
|
581 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
582 | ws[col + str(current_row_number)] = _('Base Period') + " - " + _('Datetime') |
|
583 | ||
584 | for i in range(0, base_period_data_ca_len): |
|
585 | current_col_number += 1 |
|
586 | col = format_cell.get_column_letter(current_col_number) |
|
587 | ||
588 | ws[col + str(current_row_number)].fill = table_fill |
|
589 | ws[col + str(current_row_number)].font = title_font |
|
590 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
591 | ws[col + str(current_row_number)] = _('Base Period') + " - " + base_period_data['names'][i] + \ |
|
592 | " (" + base_period_data['units'][i] + ")" |
|
593 | ws[col + str(current_row_number)].border = f_border |
|
594 | ||
595 | current_col_number += 1 |
|
596 | col = format_cell.get_column_letter(current_col_number) |
|
597 | ws[col + str(current_row_number)].fill = table_fill |
|
598 | ws[col + str(current_row_number)].font = title_font |
|
599 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
600 | ws[col + str(current_row_number)] = _('Base Period') + " - " \ |
|
601 | + _('Total') + '(' + report['reporting_period']['total_unit'] + ')' |
|
602 | ws[col + str(current_row_number)].border = f_border |
|
603 | ||
604 | current_col_number += 1 |
|
605 | col = format_cell.get_column_letter(current_col_number) |
|
606 | ||
607 | ws[col + str(current_row_number)].fill = table_fill |
|
608 | ws[col + str(current_row_number)].font = title_font |
|
609 | ws[col + str(current_row_number)].border = f_border |
|
610 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
611 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " + _('Datetime') |
|
612 | ||
613 | for i in range(0, reporting_period_data_ca_len): |
|
614 | current_col_number += 1 |
|
615 | col = format_cell.get_column_letter(current_col_number) |
|
616 | ws[col + str(current_row_number)].fill = table_fill |
|
617 | ws[col + str(current_row_number)].font = title_font |
|
618 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
619 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " \ |
|
620 | + reporting_period_data['names'][i] + " (" + \ |
|
621 | reporting_period_data['units'][i] + ")" |
|
622 | ws[col + str(current_row_number)].border = f_border |
|
623 | ||
624 | current_col_number += 1 |
|
625 | col = format_cell.get_column_letter(current_col_number) |
|
626 | ws[col + str(current_row_number)].fill = table_fill |
|
627 | ws[col + str(current_row_number)].font = title_font |
|
628 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
629 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " \ |
|
630 | + _('Total') + '(' + report['reporting_period']['total_unit'] + ')' |
|
631 | ws[col + str(current_row_number)].border = f_border |
|
632 | ||
633 | current_row_number += 1 |
|
634 | ||
635 | max_timestamps_len = len(base_period_timestamps[0]) \ |
|
636 | if len(base_period_timestamps[0]) >= len(reporting_period_timestamps[0]) \ |
|
637 | else len(reporting_period_timestamps[0]) |
|
638 | ||
639 | for i in range(0, max_timestamps_len): |
|
640 | current_col_number = 2 |
|
641 | col = format_cell.get_column_letter(current_col_number) |
|
642 | ws[col + str(current_row_number)].font = title_font |
|
643 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
644 | ws[col + str(current_row_number)] = base_period_timestamps[0][i] \ |
|
645 | if i < len(base_period_timestamps[0]) else None |
|
646 | ws[col + str(current_row_number)].border = f_border |
|
647 | ||
648 | base_period_total = Decimal(0.0) |
|
649 | ||
650 | for j in range(0, base_period_data_ca_len): |
|
651 | current_col_number += 1 |
|
652 | col = format_cell.get_column_letter(current_col_number) |
|
653 | ||
654 | ws[col + str(current_row_number)].font = title_font |
|
655 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
656 | ws[col + str(current_row_number)] = round2(base_period_data['values'][j][i], 2) \ |
|
657 | if i < len(base_period_data['values'][j]) else None |
|
658 | if i < len(base_period_timestamps[0]): |
|
659 | base_period_total += base_period_data['values'][j][i] |
|
660 | ws[col + str(current_row_number)].border = f_border |
|
661 | ||
662 | current_col_number += 1 |
|
663 | col = format_cell.get_column_letter(current_col_number) |
|
664 | ws[col + str(current_row_number)].font = title_font |
|
665 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
666 | ws[col + str(current_row_number)] = round2(base_period_total, 2) \ |
|
667 | if i < len(base_period_timestamps[0]) else None |
|
668 | ws[col + str(current_row_number)].border = f_border |
|
669 | ||
670 | current_col_number += 1 |
|
671 | col = format_cell.get_column_letter(current_col_number) |
|
672 | ||
673 | ws[col + str(current_row_number)].font = title_font |
|
674 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
675 | ws[col + str(current_row_number)] = reporting_period_timestamps[0][i] \ |
|
676 | if i < len(reporting_period_timestamps[0]) else None |
|
677 | ws[col + str(current_row_number)].border = f_border |
|
678 | ||
679 | reporting_period_total = Decimal(0.0) |
|
680 | ||
681 | for j in range(0, reporting_period_data_ca_len): |
|
682 | current_col_number += 1 |
|
683 | col = format_cell.get_column_letter(current_col_number) |
|
684 | ||
685 | ws[col + str(current_row_number)].font = title_font |
|
686 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
687 | ws[col + str(current_row_number)] = round2(reporting_period_data['values'][j][i], 2) \ |
|
688 | if i < len(reporting_period_data['values'][j]) else None |
|
689 | if i < len(reporting_period_timestamps[0]): |
|
690 | reporting_period_total += reporting_period_data['values'][j][i] |
|
691 | ws[col + str(current_row_number)].border = f_border |
|
692 | ||
693 | current_col_number += 1 |
|
694 | col = format_cell.get_column_letter(current_col_number) |
|
695 | ws[col + str(current_row_number)].font = title_font |
|
696 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
697 | ws[col + str(current_row_number)] = round2(reporting_period_total, 2) \ |
|
698 | if i < len(reporting_period_timestamps[0]) else None |
|
699 | ws[col + str(current_row_number)].border = f_border |
|
700 | ||
701 | current_row_number += 1 |
|
702 | ||
703 | current_col_number = 2 |
|
704 | col = format_cell.get_column_letter(current_col_number) |
|
705 | ws[col + str(current_row_number)].font = title_font |
|
706 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
707 | ws[col + str(current_row_number)] = _('Subtotal') |
|
708 | ws[col + str(current_row_number)].border = f_border |
|
709 | ||
710 | base_period_subtotals = Decimal(0.0) |
|
711 | ||
712 | for i in range(0, base_period_data_ca_len): |
|
713 | current_col_number += 1 |
|
714 | col = format_cell.get_column_letter(current_col_number) |
|
715 | ws[col + str(current_row_number)].font = title_font |
|
716 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
717 | ws[col + str(current_row_number)] = round2(base_period_data['subtotals'][i], 2) |
|
718 | base_period_subtotals += base_period_data['subtotals'][i] |
|
719 | ws[col + str(current_row_number)].border = f_border |
|
720 | ||
721 | current_col_number += 1 |
|
722 | col = format_cell.get_column_letter(current_col_number) |
|
723 | ws[col + str(current_row_number)].font = title_font |
|
724 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
725 | ws[col + str(current_row_number)] = round2(base_period_subtotals, 2) |
|
726 | ws[col + str(current_row_number)].border = f_border |
|
727 | ||
728 | current_col_number += 1 |
|
729 | col = format_cell.get_column_letter(current_col_number) |
|
730 | ||
731 | ws[col + str(current_row_number)].font = title_font |
|
732 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
733 | ws[col + str(current_row_number)] = _('Subtotal') |
|
734 | ws[col + str(current_row_number)].border = f_border |
|
735 | ||
736 | reporting_period_subtotals = Decimal(0.0) |
|
737 | ||
738 | for i in range(0, reporting_period_data_ca_len): |
|
739 | current_col_number += 1 |
|
740 | col = format_cell.get_column_letter(current_col_number) |
|
741 | ws[col + str(current_row_number)].font = title_font |
|
742 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
743 | ws[col + str(current_row_number)] = round2(reporting_period_data['subtotals'][i], 2) |
|
744 | reporting_period_subtotals += reporting_period_data['subtotals'][i] |
|
745 | ws[col + str(current_row_number)].border = f_border |
|
746 | ||
747 | current_col_number += 1 |
|
748 | col = format_cell.get_column_letter(current_col_number) |
|
749 | ws[col + str(current_row_number)].font = title_font |
|
750 | ws[col + str(current_row_number)].alignment = c_c_alignment |
|
751 | ws[col + str(current_row_number)] = round2(reporting_period_subtotals, 2) |
|
752 | ws[col + str(current_row_number)].border = f_border |
|
753 | ||
754 | for i in range(0, reporting_period_data_ca_len): |
|
755 | # line |
|
756 | line = LineChart() |
|
757 | line.title = _('Base Period Carbon Dioxide Emissions') + " / " \ |
|
758 | + _('Reporting Period Carbon Dioxide Emissions') + ' - ' \ |
|
759 | + reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
|
760 | labels = Reference(ws, min_col=2 + base_period_data_ca_len + 1 + 1, |
|
761 | min_row=table_start_row_number + 1, |
|
762 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
|
763 | base_line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, |
|
764 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
|
765 | reporting_line_data = Reference(ws, min_col=3 + base_period_data_ca_len + 1 + 1 + i, |
|
766 | min_row=table_start_row_number, |
|
767 | max_row=table_start_row_number |
|
768 | + len(reporting_period_timestamps[0])) |
|
769 | line.add_data(base_line_data, titles_from_data=True) |
|
770 | line.add_data(reporting_line_data, titles_from_data=True) |
|
771 | line.set_categories(labels) |
|
772 | for j in range(len(line.series)): |
|
773 | line.series[j].marker.symbol = "auto" |
|
774 | line.series[j].smooth = True |
|
775 | line.x_axis.crosses = 'min' |
|
776 | line.height = 8.25 |
|
777 | line.width = 24 |
|
778 | chart_col = 'B' |
|
779 | chart_cell = chart_col + str(table_start_draw_flag + 6 * i) |
|
780 | ws.add_chart(line, chart_cell) |
|
781 | ||
782 | current_row_number += 2 |
|
783 | ||
784 | #################################################################################################################### |
|
785 | current_sheet_parameters_row_number = table_start_draw_flag + len(reporting_period_data['names']) * 6 + 1 |
|
786 | if 'parameters' not in report.keys() or \ |
|
787 | report['parameters'] is None or \ |
|
788 | 'names' not in report['parameters'].keys() or \ |
|
789 | report['parameters']['names'] is None or \ |
|
790 | len(report['parameters']['names']) == 0 or \ |
|
791 | 'timestamps' not in report['parameters'].keys() or \ |
|
792 | report['parameters']['timestamps'] is None or \ |
|
793 | len(report['parameters']['timestamps']) == 0 or \ |
|
794 | 'values' not in report['parameters'].keys() or \ |
|
795 | report['parameters']['values'] is None or \ |
|
796 | len(report['parameters']['values']) == 0 or \ |
|
797 | timestamps_data_all_equal_0(report['parameters']['timestamps']): |
|
798 | pass |
|
799 | else: |
|
800 | ################################################################################################################ |
|
801 | # new worksheet |
|
802 | ################################################################################################################ |
|
803 | ||
804 | parameters_data = report['parameters'] |
|
805 | parameters_names_len = len(parameters_data['names']) |
|
806 | ||
807 | file_name = (re.sub(r'[^A-Z]', '', ws.title)) + '_' |
|
808 | parameters_ws = wb.create_sheet(file_name + _('Parameters')) |
|
809 | ||
810 | parameters_timestamps_data_max_len = \ |
|
811 | get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
|
812 | ||
813 | # Row height |
|
814 | parameters_ws.row_dimensions[1].height = 102 |
|
815 | for i in range(2, 7 + 1): |
|
816 | parameters_ws.row_dimensions[i].height = 42 |
|
817 | ||
818 | for i in range(8, parameters_timestamps_data_max_len + 10): |
|
819 | parameters_ws.row_dimensions[i].height = 60 |
|
820 | ||
821 | # Col width |
|
822 | parameters_ws.column_dimensions['A'].width = 1.5 |
|
823 | ||
824 | parameters_ws.column_dimensions['B'].width = 25.0 |
|
825 | ||
826 | for i in range(3, 12 + parameters_names_len * 3): |
|
827 | parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
|
828 | ||
829 | # Img |
|
830 | img = Image("excelexporters/myems.png") |
|
831 | parameters_ws.add_image(img, 'A1') |
|
832 | ||
833 | # Title |
|
834 | parameters_ws['B3'].alignment = b_r_alignment |
|
835 | parameters_ws['B3'] = _('Name') + ':' |
|
836 | parameters_ws['C3'].border = b_border |
|
837 | parameters_ws['C3'].alignment = b_c_alignment |
|
838 | parameters_ws['C3'] = name |
|
839 | ||
840 | parameters_ws['D3'].alignment = b_r_alignment |
|
841 | parameters_ws['D3'] = _('Period Type') + ':' |
|
842 | parameters_ws['E3'].border = b_border |
|
843 | parameters_ws['E3'].alignment = b_c_alignment |
|
844 | parameters_ws['E3'] = period_type |
|
845 | ||
846 | parameters_ws['B4'].alignment = b_r_alignment |
|
847 | parameters_ws['B4'] = _('Reporting Start Datetime') + ':' |
|
848 | parameters_ws['C4'].border = b_border |
|
849 | parameters_ws['C4'].alignment = b_c_alignment |
|
850 | parameters_ws['C4'] = reporting_start_datetime_local |
|
851 | ||
852 | parameters_ws['D4'].alignment = b_r_alignment |
|
853 | parameters_ws['D4'] = _('Reporting End Datetime') + ':' |
|
854 | parameters_ws['E4'].border = b_border |
|
855 | parameters_ws['E4'].alignment = b_c_alignment |
|
856 | parameters_ws['E4'] = reporting_end_datetime_local |
|
857 | ||
858 | parameters_ws_current_row_number = 6 |
|
859 | ||
860 | parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
|
861 | parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' ' + _('Parameters') |
|
862 | ||
863 | parameters_ws_current_row_number += 1 |
|
864 | ||
865 | parameters_table_start_row_number = parameters_ws_current_row_number |
|
866 | ||
867 | parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
|
868 | ||
869 | parameters_ws_current_row_number += 1 |
|
870 | ||
871 | table_current_col_number = 2 |
|
872 | ||
873 | for i in range(0, parameters_names_len): |
|
874 | ||
875 | if len(parameters_data['timestamps'][i]) == 0: |
|
876 | continue |
|
877 | ||
878 | col = format_cell.get_column_letter(table_current_col_number) |
|
879 | ||
880 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
|
881 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
|
882 | ||
883 | col = format_cell.get_column_letter(table_current_col_number + 1) |
|
884 | ||
885 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
|
886 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
|
887 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = name_font |
|
888 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = c_c_alignment |
|
889 | parameters_ws[col + str(parameters_ws_current_row_number - 1)] = parameters_data['names'][i] |
|
890 | ||
891 | table_current_row_number = parameters_ws_current_row_number |
|
892 | ||
893 | for j, value in enumerate(list(parameters_data['timestamps'][i])): |
|
894 | col = format_cell.get_column_letter(table_current_col_number) |
|
895 | ||
896 | parameters_ws[col + str(table_current_row_number)].border = f_border |
|
897 | parameters_ws[col + str(table_current_row_number)].font = title_font |
|
898 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
|
899 | parameters_ws[col + str(table_current_row_number)] = value |
|
900 | ||
901 | col = format_cell.get_column_letter(table_current_col_number + 1) |
|
902 | ||
903 | parameters_ws[col + str(table_current_row_number)].border = f_border |
|
904 | parameters_ws[col + str(table_current_row_number)].font = title_font |
|
905 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
|
906 | parameters_ws[col + str(table_current_row_number)] = round2(parameters_data['values'][i][j], 2) |
|
907 | ||
908 | table_current_row_number += 1 |
|
909 | ||
910 | table_current_col_number = table_current_col_number + 3 |
|
911 | ||
912 | ################################################################################################################ |
|
913 | # parameters chart and parameters table |
|
914 | ################################################################################################################ |
|
915 | ||
916 | ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
|
917 | ws['B' + str(current_sheet_parameters_row_number)] = name + ' ' + _('Parameters') |
|
918 | ||
919 | current_sheet_parameters_row_number += 1 |
|
920 | ||
921 | chart_start_row_number = current_sheet_parameters_row_number |
|
922 | ||
923 | col_index = 0 |
|
924 | ||
925 | for i in range(0, parameters_names_len): |
|
926 | ||
927 | if len(parameters_data['timestamps'][i]) == 0: |
|
928 | continue |
|
929 | ||
930 | line = LineChart() |
|
931 | data_col = 3 + col_index * 3 |
|
932 | labels_col = 2 + col_index * 3 |
|
933 | col_index += 1 |
|
934 | line.title = _('Parameters') + ' - ' + \ |
|
935 | parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
|
936 | labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
|
937 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
|
938 | line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
|
939 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
|
940 | line.add_data(line_data, titles_from_data=True) |
|
941 | line.set_categories(labels) |
|
942 | line_data = line.series[0] |
|
943 | line_data.marker.symbol = "auto" |
|
944 | line_data.smooth = True |
|
945 | line.x_axis.crosses = 'min' |
|
946 | line.height = 8.25 |
|
947 | line.width = 24 |
|
948 | chart_col = 'B' |
|
949 | chart_cell = chart_col + str(chart_start_row_number) |
|
950 | chart_start_row_number += 6 |
|
951 | ws.add_chart(line, chart_cell) |
|
952 | ||
953 | current_sheet_parameters_row_number = chart_start_row_number |
|
954 | ||
955 | current_sheet_parameters_row_number += 1 |
|
956 | #################################################################################################################### |
|
957 | filename = str(uuid.uuid4()) + '.xlsx' |
|
958 | wb.save(filename) |
|
959 | ||
960 | return filename |
|
961 | ||
962 | ||
963 | def timestamps_data_all_equal_0(lists): |