1 | import base64 |
||
2 | from core.utilities import get_translation |
||
3 | import os |
||
4 | import re |
||
5 | import uuid |
||
6 | from decimal import Decimal |
||
7 | import openpyxl.utils.cell as format_cell |
||
8 | from openpyxl import Workbook |
||
9 | from openpyxl.chart import PieChart, LineChart, Reference |
||
10 | from openpyxl.chart.label import DataLabelList |
||
11 | from openpyxl.drawing.image import Image |
||
12 | from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
||
13 | from core.utilities import round2 |
||
14 | |||
15 | ######################################################################################################################## |
||
16 | # PROCEDURES |
||
17 | # Step 1: Validate the report data |
||
18 | # Step 2: Generate excel file |
||
19 | # Step 3: Encode the excel file bytes to Base64 |
||
20 | ######################################################################################################################## |
||
21 | |||
22 | |||
23 | View Code Duplication | def export(report, |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
24 | name, |
||
25 | base_period_start_datetime_local, |
||
26 | base_period_end_datetime_local, |
||
27 | reporting_start_datetime_local, |
||
28 | reporting_end_datetime_local, |
||
29 | period_type, |
||
30 | language): |
||
31 | #################################################################################################################### |
||
32 | # Step 1: Validate the report data |
||
33 | #################################################################################################################### |
||
34 | if report is None: |
||
35 | return None |
||
36 | print(report) |
||
37 | |||
38 | #################################################################################################################### |
||
39 | # Step 2: Generate excel file from the report data |
||
40 | #################################################################################################################### |
||
41 | filename = generate_excel(report, |
||
42 | name, |
||
43 | base_period_start_datetime_local, |
||
44 | base_period_end_datetime_local, |
||
45 | reporting_start_datetime_local, |
||
46 | reporting_end_datetime_local, |
||
47 | period_type, |
||
48 | language) |
||
49 | #################################################################################################################### |
||
50 | # Step 3: Encode the excel file to Base64 |
||
51 | #################################################################################################################### |
||
52 | binary_file_data = b'' |
||
53 | try: |
||
54 | with open(filename, 'rb') as binary_file: |
||
55 | binary_file_data = binary_file.read() |
||
56 | except IOError as ex: |
||
57 | print(str(ex)) |
||
58 | |||
59 | # Base64 encode the bytes |
||
60 | base64_encoded_data = base64.b64encode(binary_file_data) |
||
61 | # get the Base64 encoded data using human-readable characters. |
||
62 | base64_message = base64_encoded_data.decode('utf-8') |
||
63 | # delete the file from server |
||
64 | try: |
||
65 | os.remove(filename) |
||
66 | except NotImplementedError as ex: |
||
67 | print(str(ex)) |
||
68 | return base64_message |
||
69 | |||
70 | |||
71 | View Code Duplication | def generate_excel(report, |
|
0 ignored issues
–
show
|
|||
72 | name, |
||
73 | base_period_start_datetime_local, |
||
74 | base_period_end_datetime_local, |
||
75 | reporting_start_datetime_local, |
||
76 | reporting_end_datetime_local, |
||
77 | period_type, |
||
78 | language): |
||
79 | trans = get_translation(language) |
||
80 | trans.install() |
||
81 | _ = trans.gettext |
||
82 | |||
83 | wb = Workbook() |
||
84 | ws = wb.active |
||
85 | ws.title = "CombinedEquipmentCarbon" |
||
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 \ |
||
179 | len(report['reporting_period']['names']) == 0: |
||
180 | filename = str(uuid.uuid4()) + '.xlsx' |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
181 | wb.save(filename) |
||
182 | |||
183 | return filename |
||
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, 10 + 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[7].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'] = _('Increment Rate') |
||
211 | ws['B10'].border = f_border |
||
212 | |||
213 | col = 'B' |
||
214 | |||
215 | for i in range(0, ca_len): |
||
216 | col = chr(ord('C') + i) |
||
217 | ws[col + '8'].fill = table_fill |
||
218 | ws[col + '8'].font = name_font |
||
219 | ws[col + '8'].alignment = c_c_alignment |
||
220 | ws[col + '8'] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
221 | ws[col + '8'].border = f_border |
||
222 | |||
223 | ws[col + '9'].font = name_font |
||
224 | ws[col + '9'].alignment = c_c_alignment |
||
225 | ws[col + '9'] = round2(reporting_period_data['subtotals'][i], 2) |
||
226 | ws[col + '9'].border = f_border |
||
227 | |||
228 | ws[col + '10'].font = name_font |
||
229 | ws[col + '10'].alignment = c_c_alignment |
||
230 | ws[col + '10'] = str(round2(reporting_period_data['increment_rates'][i] * 100, 2)) + "%" \ |
||
231 | if reporting_period_data['increment_rates'][i] is not None else "-" |
||
232 | ws[col + '10'].border = f_border |
||
233 | |||
234 | end_col = chr(ord(col) + 1) |
||
235 | ws[end_col + '8'].fill = table_fill |
||
236 | ws[end_col + '8'].font = name_font |
||
237 | ws[end_col + '8'].alignment = c_c_alignment |
||
238 | ws[end_col + '8'] = _("Total") + " (" + reporting_period_data['total_unit'] + ")" |
||
239 | ws[end_col + '8'].border = f_border |
||
240 | |||
241 | ws[end_col + '9'].font = name_font |
||
242 | ws[end_col + '9'].alignment = c_c_alignment |
||
243 | ws[end_col + '9'] = round2(reporting_period_data['total'], 2) |
||
244 | ws[end_col + '9'].border = f_border |
||
245 | |||
246 | ws[end_col + '10'].font = name_font |
||
247 | ws[end_col + '10'].alignment = c_c_alignment |
||
248 | ws[end_col + '10'] = str(round2(reporting_period_data['total_increment_rate'] * 100, 2)) + "%" \ |
||
249 | if reporting_period_data['total_increment_rate'] is not None else "-" |
||
250 | ws[end_col + '10'].border = f_border |
||
251 | |||
252 | if "toppeaks" not in reporting_period_data.keys() or \ |
||
253 | reporting_period_data['toppeaks'] is None or \ |
||
254 | len(reporting_period_data['toppeaks']) == 0: |
||
255 | for i in range(12, 18 + 1): |
||
256 | ws.row_dimensions[i].height = 0.1 |
||
257 | else: |
||
258 | electricity_index = -1 |
||
259 | for i in range(len(reporting_period_data['energy_category_ids'])): |
||
260 | if reporting_period_data['energy_category_ids'][i] == 1: |
||
261 | electricity_index = i |
||
262 | break |
||
263 | |||
264 | ws['B12'].font = title_font |
||
265 | ws['B12'] = name + _('Electricity Carbon Dioxide Emissions by Time-Of-Use') |
||
266 | |||
267 | ws['B13'].fill = table_fill |
||
268 | ws['B13'].font = name_font |
||
269 | ws['B13'].alignment = c_c_alignment |
||
270 | ws['B13'].border = f_border |
||
271 | |||
272 | ws['C13'].fill = table_fill |
||
273 | ws['C13'].font = name_font |
||
274 | ws['C13'].alignment = c_c_alignment |
||
275 | ws['C13'].border = f_border |
||
276 | ws['C13'] = _('Electricity Carbon Dioxide Emissions by Time-Of-Use') |
||
277 | |||
278 | ws['D13'].fill = table_fill |
||
279 | ws['D13'].font = name_font |
||
280 | ws['D13'].alignment = c_c_alignment |
||
281 | ws['D13'].border = f_border |
||
282 | ws['D13'] = _('Electricity Carbon Dioxide Emissions Proportion by Time-Of-Use') |
||
283 | |||
284 | carbonsum = None |
||
285 | |||
286 | if electricity_index >= 0: |
||
287 | carbonsum = round(reporting_period_data['toppeaks'][electricity_index], 2) + \ |
||
288 | round(reporting_period_data['onpeaks'][electricity_index], 2) + \ |
||
289 | round(reporting_period_data['midpeaks'][electricity_index], 2) + \ |
||
290 | round(reporting_period_data['offpeaks'][electricity_index], 2) + \ |
||
291 | round(reporting_period_data['deeps'][electricity_index], 2) |
||
292 | |||
293 | ws['B14'].font = title_font |
||
294 | ws['B14'].alignment = c_c_alignment |
||
295 | ws['B14'] = _('TopPeak') |
||
296 | ws['B14'].border = f_border |
||
297 | |||
298 | ws['C14'].font = title_font |
||
299 | ws['C14'].alignment = c_c_alignment |
||
300 | ws['C14'].border = f_border |
||
301 | ws['C14'] = round2(reporting_period_data['toppeaks'][electricity_index], 2) if electricity_index >= 0 else "-" |
||
302 | |||
303 | ws['D14'].font = title_font |
||
304 | ws['D14'].alignment = c_c_alignment |
||
305 | ws['D14'].border = f_border |
||
306 | ws['D14'] = '{:.2%}'.format(round2(reporting_period_data['toppeaks'][electricity_index], 2) / carbonsum) \ |
||
307 | if carbonsum is not None and carbonsum != Decimal(0.0) else " " |
||
308 | |||
309 | ws['B15'].font = title_font |
||
310 | ws['B15'].alignment = c_c_alignment |
||
311 | ws['B15'] = _('OnPeak') |
||
312 | ws['B15'].border = f_border |
||
313 | |||
314 | ws['C15'].font = title_font |
||
315 | ws['C15'].alignment = c_c_alignment |
||
316 | ws['C15'].border = f_border |
||
317 | ws['C15'] = round2(reporting_period_data['onpeaks'][electricity_index], 2) if electricity_index >= 0 else "-" |
||
318 | |||
319 | ws['D15'].font = title_font |
||
320 | ws['D15'].alignment = c_c_alignment |
||
321 | ws['D15'].border = f_border |
||
322 | ws['D15'] = '{:.2%}'.format(round2(reporting_period_data['onpeaks'][electricity_index], 2) / carbonsum) \ |
||
323 | if carbonsum is not None and carbonsum != Decimal(0.0) else " " |
||
324 | |||
325 | ws['B16'].font = title_font |
||
326 | ws['B16'].alignment = c_c_alignment |
||
327 | ws['B16'] = _('MidPeak') |
||
328 | ws['B16'].border = f_border |
||
329 | |||
330 | ws['C16'].font = title_font |
||
331 | ws['C16'].alignment = c_c_alignment |
||
332 | ws['C16'].border = f_border |
||
333 | ws['C16'] = round2(reporting_period_data['midpeaks'][electricity_index], 2) if electricity_index >= 0 else "-" |
||
334 | |||
335 | ws['D16'].font = title_font |
||
336 | ws['D16'].alignment = c_c_alignment |
||
337 | ws['D16'].border = f_border |
||
338 | ws['D16'] = '{:.2%}'.format(round2(reporting_period_data['midpeaks'][electricity_index], 2) / carbonsum) \ |
||
339 | if carbonsum is not None and carbonsum != Decimal(0.0) else " " |
||
340 | |||
341 | ws['B17'].font = title_font |
||
342 | ws['B17'].alignment = c_c_alignment |
||
343 | ws['B17'] = _('OffPeak') |
||
344 | ws['B17'].border = f_border |
||
345 | |||
346 | ws['C17'].font = title_font |
||
347 | ws['C17'].alignment = c_c_alignment |
||
348 | ws['C17'].border = f_border |
||
349 | ws['C17'] = round2(reporting_period_data['offpeaks'][electricity_index], 2) if electricity_index >= 0 else "-" |
||
350 | |||
351 | ws['D17'].font = title_font |
||
352 | ws['D17'].alignment = c_c_alignment |
||
353 | ws['D17'].border = f_border |
||
354 | ws['D17'] = '{:.2%}'.format(round2(reporting_period_data['offpeaks'][electricity_index], 2) / carbonsum) \ |
||
355 | if carbonsum is not None and carbonsum != Decimal(0.0) else " " |
||
356 | |||
357 | pie = PieChart() |
||
358 | pie.title = name + _('Electricity Carbon Dioxide Emissions by Time-Of-Use') |
||
359 | labels = Reference(ws, min_col=2, min_row=14, max_row=17) |
||
360 | pie_data = Reference(ws, min_col=3, min_row=13, max_row=17) |
||
361 | pie.add_data(pie_data, titles_from_data=True) |
||
362 | pie.set_categories(labels) |
||
363 | pie.height = 6.6 |
||
364 | pie.width = 9 |
||
365 | s1 = pie.series[0] |
||
366 | s1.dLbls = DataLabelList() |
||
367 | s1.dLbls.showCatName = False |
||
368 | s1.dLbls.showVal = False |
||
369 | s1.dLbls.showPercent = True |
||
370 | ws.add_chart(pie, "E13") |
||
371 | |||
372 | ################################################ |
||
373 | |||
374 | current_row_number = 19 |
||
375 | if "subtotals" not in reporting_period_data.keys() or \ |
||
376 | reporting_period_data['subtotals'] is None or \ |
||
377 | len(reporting_period_data['subtotals']) == 0: |
||
378 | for i in range(21, 29 + 1): |
||
379 | current_row_number = 30 |
||
380 | ws.row_dimensions[i].height = 0.1 |
||
381 | else: |
||
382 | ws['B' + str(current_row_number)].font = title_font |
||
383 | ws['B' + str(current_row_number)] = name + ' ' + _('Carbon Dioxide Emissions Proportion') |
||
384 | |||
385 | current_row_number += 1 |
||
386 | |||
387 | table_start_row_number = current_row_number |
||
388 | |||
389 | ws['B' + str(current_row_number)].fill = table_fill |
||
390 | ws['B' + str(current_row_number)].font = name_font |
||
391 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
392 | ws['B' + str(current_row_number)].border = f_border |
||
393 | |||
394 | ws['C' + str(current_row_number)].fill = table_fill |
||
395 | ws['C' + str(current_row_number)].font = name_font |
||
396 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
397 | ws['C' + str(current_row_number)].border = f_border |
||
398 | ws['C' + str(current_row_number)] = _('Carbon Dioxide Emissions') |
||
399 | |||
400 | ws['D' + str(current_row_number)].fill = table_fill |
||
401 | ws['D' + str(current_row_number)].font = name_font |
||
402 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
403 | ws['D' + str(current_row_number)].border = f_border |
||
404 | ws['D' + str(current_row_number)] = _('Carbon Dioxide Emissions Proportion') |
||
405 | |||
406 | current_row_number += 1 |
||
407 | |||
408 | ca_len = len(reporting_period_data['names']) |
||
409 | carbonsum = Decimal(0.0) |
||
410 | for i in range(0, ca_len): |
||
411 | carbonsum = round(reporting_period_data['subtotals'][i], 2) + carbonsum |
||
412 | for i in range(0, ca_len): |
||
413 | ws['B' + str(current_row_number)].font = title_font |
||
414 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
415 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
||
416 | ws['B' + str(current_row_number)].border = f_border |
||
417 | |||
418 | ws['C' + str(current_row_number)].font = title_font |
||
419 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
420 | ws['C' + str(current_row_number)].border = f_border |
||
421 | ws['C' + str(current_row_number)] = round2(reporting_period_data['subtotals'][i], 2) |
||
422 | |||
423 | ws['D' + str(current_row_number)].font = title_font |
||
424 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
425 | ws['D' + str(current_row_number)].border = f_border |
||
426 | ws['D' + str(current_row_number)] = '{:.2%}'.format(round2( |
||
427 | reporting_period_data['subtotals'][i], 2) / carbonsum) if \ |
||
428 | carbonsum is not None and carbonsum != Decimal(0.0) else " " |
||
429 | current_row_number += 1 |
||
430 | |||
431 | table_end_row_number = current_row_number - 1 |
||
432 | |||
433 | pie = PieChart() |
||
434 | pie.title = name + ' ' + _('Carbon Dioxide Emissions Proportion') |
||
435 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
436 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
||
437 | pie.add_data(pie_data, titles_from_data=True) |
||
438 | pie.set_categories(labels) |
||
439 | pie.height = 6.6 |
||
440 | pie.width = 9 |
||
441 | s1 = pie.series[0] |
||
442 | s1.dLbls = DataLabelList() |
||
443 | s1.dLbls.showCatName = False |
||
444 | s1.dLbls.showVal = False |
||
445 | s1.dLbls.showPercent = True |
||
446 | table_cell = 'E' + str(table_start_row_number) |
||
447 | ws.add_chart(pie, table_cell) |
||
448 | |||
449 | if ca_len < 4: |
||
450 | current_row_number = current_row_number - ca_len + 4 |
||
451 | |||
452 | current_row_number += 1 |
||
453 | |||
454 | #################################################################################################################### |
||
455 | table_start_draw_flag = current_row_number + 1 |
||
456 | |||
457 | if "timestamps" not in reporting_period_data.keys() or \ |
||
458 | reporting_period_data['timestamps'] is None or \ |
||
459 | len(reporting_period_data['timestamps']) == 0: |
||
460 | pass |
||
461 | else: |
||
462 | if not is_base_period_timestamp_exists_flag: |
||
463 | reporting_period_data = report['reporting_period'] |
||
464 | times = reporting_period_data['timestamps'] |
||
465 | ca_len = len(report['reporting_period']['names']) |
||
466 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
||
467 | ws['B' + str(current_row_number)].font = title_font |
||
468 | ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data') |
||
469 | |||
470 | current_row_number += 1 |
||
471 | # 1: Stand for blank line 2: Stand for title |
||
472 | current_row_number += ca_len * 6 + real_timestamps_len * 6 + 1 + 2 |
||
473 | table_start_row_number = current_row_number |
||
474 | |||
475 | time = times[0] |
||
476 | has_data = False |
||
477 | |||
478 | if len(time) > 0: |
||
479 | has_data = True |
||
480 | |||
481 | if has_data: |
||
482 | |||
483 | ws.row_dimensions[current_row_number].height = 60 |
||
484 | current_col_number = 2 |
||
485 | col = format_cell.get_column_letter(current_col_number) |
||
486 | ws[col + str(current_row_number)].fill = table_fill |
||
487 | ws[col + str(current_row_number)].font = title_font |
||
488 | ws[col + str(current_row_number)].border = f_border |
||
489 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
490 | ws[col + str(current_row_number)] = _('Datetime') |
||
491 | |||
492 | for i in range(0, ca_len): |
||
493 | current_col_number += 1 |
||
494 | col = format_cell.get_column_letter(current_col_number) |
||
495 | |||
496 | ws[col + str(current_row_number)].fill = table_fill |
||
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)] = reporting_period_data['names'][i] + \ |
||
500 | " (" + reporting_period_data['units'][i] + ")" |
||
501 | ws[col + str(current_row_number)].border = f_border |
||
502 | |||
503 | current_col_number += 1 |
||
504 | col = format_cell.get_column_letter(current_col_number) |
||
505 | ws[col + str(current_row_number)].fill = table_fill |
||
506 | ws[col + str(current_row_number)].font = title_font |
||
507 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
508 | ws[col + str(current_row_number)] = _('Total') + '(' + report['reporting_period']['total_unit'] + ')' |
||
509 | ws[col + str(current_row_number)].border = f_border |
||
510 | |||
511 | current_row_number += 1 |
||
512 | |||
513 | for i in range(0, len(time)): |
||
514 | current_col_number = 2 |
||
515 | col = format_cell.get_column_letter(current_col_number) |
||
516 | |||
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)] = time[i] |
||
520 | ws[col + str(current_row_number)].border = f_border |
||
521 | |||
522 | total = Decimal(0.0) |
||
523 | |||
524 | for j in range(0, ca_len): |
||
525 | current_col_number += 1 |
||
526 | col = format_cell.get_column_letter(current_col_number) |
||
527 | |||
528 | ws[col + str(current_row_number)].font = title_font |
||
529 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
530 | ws[col + str(current_row_number)] = round2(reporting_period_data['values'][j][i], 2) |
||
531 | total += reporting_period_data['values'][j][i] |
||
532 | ws[col + str(current_row_number)].border = f_border |
||
533 | |||
534 | current_col_number += 1 |
||
535 | col = format_cell.get_column_letter(current_col_number) |
||
536 | ws[col + str(current_row_number)].font = title_font |
||
537 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
538 | ws[col + str(current_row_number)] = round2(total, 2) |
||
539 | ws[col + str(current_row_number)].border = f_border |
||
540 | |||
541 | current_row_number += 1 |
||
542 | |||
543 | table_end_row_number = current_row_number - 1 |
||
544 | |||
545 | current_col_number = 2 |
||
546 | col = format_cell.get_column_letter(current_col_number) |
||
547 | |||
548 | ws[col + str(current_row_number)].font = title_font |
||
549 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
550 | ws[col + str(current_row_number)] = _('Subtotal') |
||
551 | ws[col + str(current_row_number)].border = f_border |
||
552 | |||
553 | subtotals = Decimal(0.0) |
||
554 | |||
555 | for i in range(0, ca_len): |
||
556 | current_col_number += 1 |
||
557 | col = format_cell.get_column_letter(current_col_number) |
||
558 | ws[col + str(current_row_number)].font = title_font |
||
559 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
560 | ws[col + str(current_row_number)] = round2(reporting_period_data['subtotals'][i], 2) |
||
561 | subtotals += reporting_period_data['subtotals'][i] |
||
562 | ws[col + str(current_row_number)].border = f_border |
||
563 | |||
564 | # line |
||
565 | line = LineChart() |
||
566 | line.title = _('Reporting Period Carbon Dioxide Emissions') + ' - ' \ |
||
567 | + reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
568 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
569 | line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, |
||
570 | max_row=table_end_row_number) |
||
571 | line.add_data(line_data, titles_from_data=True) |
||
572 | line.set_categories(labels) |
||
573 | line_data = line.series[0] |
||
574 | line_data.marker.symbol = "auto" |
||
575 | line_data.smooth = True |
||
576 | line.x_axis.crosses = 'min' |
||
577 | line.height = 8.25 |
||
578 | line.width = 24 |
||
579 | chart_col = 'B' |
||
580 | chart_cell = chart_col + str(table_start_draw_flag + 6 * i) |
||
581 | ws.add_chart(line, chart_cell) |
||
582 | |||
583 | current_col_number += 1 |
||
584 | col = format_cell.get_column_letter(current_col_number) |
||
585 | ws[col + str(current_row_number)].font = title_font |
||
586 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
587 | ws[col + str(current_row_number)] = round2(subtotals, 2) |
||
588 | ws[col + str(current_row_number)].border = f_border |
||
589 | |||
590 | current_row_number += 2 |
||
591 | else: |
||
592 | base_period_data = report['base_period'] |
||
593 | reporting_period_data = report['reporting_period'] |
||
594 | base_period_timestamps = base_period_data['timestamps'] |
||
595 | reporting_period_timestamps = reporting_period_data['timestamps'] |
||
596 | # Tip: |
||
597 | # base_period_data['names'] == reporting_period_data['names'] |
||
598 | # base_period_data['units'] == reporting_period_data['units'] |
||
599 | base_period_data_ca_len = len(base_period_data['names']) |
||
600 | reporting_period_data_ca_len = len(reporting_period_data['names']) |
||
601 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
||
602 | ws['B' + str(current_row_number)].font = title_font |
||
603 | ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data') |
||
604 | |||
605 | current_row_number += 1 |
||
606 | # 1: Stand for blank line 2: Stand for title |
||
607 | current_row_number += reporting_period_data_ca_len * 6 + real_timestamps_len * 6 + 1 + 2 |
||
608 | table_start_row_number = current_row_number |
||
609 | |||
610 | has_data = False |
||
611 | |||
612 | if len(base_period_timestamps[0]) or len(reporting_period_timestamps[0]) > 0: |
||
613 | has_data = True |
||
614 | |||
615 | if has_data: |
||
616 | ws.row_dimensions[current_row_number].height = 60 |
||
617 | current_col_number = 2 |
||
618 | col = format_cell.get_column_letter(current_col_number) |
||
619 | ws[col + str(current_row_number)].fill = table_fill |
||
620 | ws[col + str(current_row_number)].font = title_font |
||
621 | ws[col + str(current_row_number)].border = f_border |
||
622 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
623 | ws[col + str(current_row_number)] = _('Base Period') + " - " + _('Datetime') |
||
624 | |||
625 | for i in range(0, base_period_data_ca_len): |
||
626 | current_col_number += 1 |
||
627 | col = format_cell.get_column_letter(current_col_number) |
||
628 | |||
629 | ws[col + str(current_row_number)].fill = table_fill |
||
630 | ws[col + str(current_row_number)].font = title_font |
||
631 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
632 | ws[col + str(current_row_number)] = _('Base Period') + " - " + base_period_data['names'][i] + \ |
||
633 | " (" + base_period_data['units'][i] + ")" |
||
634 | ws[col + str(current_row_number)].border = f_border |
||
635 | |||
636 | current_col_number += 1 |
||
637 | col = format_cell.get_column_letter(current_col_number) |
||
638 | ws[col + str(current_row_number)].fill = table_fill |
||
639 | ws[col + str(current_row_number)].font = title_font |
||
640 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
641 | ws[col + str(current_row_number)] = _('Base Period') + " - " \ |
||
642 | + _('Total') + '(' + report['reporting_period']['total_unit'] + ')' |
||
643 | ws[col + str(current_row_number)].border = f_border |
||
644 | |||
645 | current_col_number += 1 |
||
646 | col = format_cell.get_column_letter(current_col_number) |
||
647 | |||
648 | ws[col + str(current_row_number)].fill = table_fill |
||
649 | ws[col + str(current_row_number)].font = title_font |
||
650 | ws[col + str(current_row_number)].border = f_border |
||
651 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
652 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " + _('Datetime') |
||
653 | |||
654 | for i in range(0, reporting_period_data_ca_len): |
||
655 | current_col_number += 1 |
||
656 | col = format_cell.get_column_letter(current_col_number) |
||
657 | ws[col + str(current_row_number)].fill = table_fill |
||
658 | ws[col + str(current_row_number)].font = title_font |
||
659 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
660 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " \ |
||
661 | + reporting_period_data['names'][i] + " (" + \ |
||
662 | reporting_period_data['units'][i] + ")" |
||
663 | ws[col + str(current_row_number)].border = f_border |
||
664 | |||
665 | current_col_number += 1 |
||
666 | col = format_cell.get_column_letter(current_col_number) |
||
667 | ws[col + str(current_row_number)].fill = table_fill |
||
668 | ws[col + str(current_row_number)].font = title_font |
||
669 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
670 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " \ |
||
671 | + _('Total') + '(' + report['reporting_period']['total_unit'] + ')' |
||
672 | ws[col + str(current_row_number)].border = f_border |
||
673 | |||
674 | current_row_number += 1 |
||
675 | |||
676 | max_timestamps_len = len(base_period_timestamps[0]) \ |
||
677 | if len(base_period_timestamps[0]) >= len(reporting_period_timestamps[0]) \ |
||
678 | else len(reporting_period_timestamps[0]) |
||
679 | |||
680 | for i in range(0, max_timestamps_len): |
||
681 | current_col_number = 2 |
||
682 | col = format_cell.get_column_letter(current_col_number) |
||
683 | ws[col + str(current_row_number)].font = title_font |
||
684 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
685 | ws[col + str(current_row_number)] = base_period_timestamps[0][i] \ |
||
686 | if i < len(base_period_timestamps[0]) else None |
||
687 | ws[col + str(current_row_number)].border = f_border |
||
688 | |||
689 | base_period_total = Decimal(0.0) |
||
690 | |||
691 | for j in range(0, base_period_data_ca_len): |
||
692 | current_col_number += 1 |
||
693 | col = format_cell.get_column_letter(current_col_number) |
||
694 | |||
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(base_period_data['values'][j][i], 2) \ |
||
698 | if i < len(base_period_data['values'][j]) else None |
||
699 | if i < len(base_period_timestamps[0]): |
||
700 | base_period_total += base_period_data['values'][j][i] |
||
701 | ws[col + str(current_row_number)].border = f_border |
||
702 | |||
703 | current_col_number += 1 |
||
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)] = round2(base_period_total, 2) \ |
||
708 | if i < len(base_period_timestamps[0]) else None |
||
709 | ws[col + str(current_row_number)].border = f_border |
||
710 | |||
711 | current_col_number += 1 |
||
712 | col = format_cell.get_column_letter(current_col_number) |
||
713 | |||
714 | ws[col + str(current_row_number)].font = title_font |
||
715 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
716 | ws[col + str(current_row_number)] = reporting_period_timestamps[0][i] \ |
||
717 | if i < len(reporting_period_timestamps[0]) else None |
||
718 | ws[col + str(current_row_number)].border = f_border |
||
719 | |||
720 | reporting_period_total = Decimal(0.0) |
||
721 | |||
722 | for j in range(0, reporting_period_data_ca_len): |
||
723 | current_col_number += 1 |
||
724 | col = format_cell.get_column_letter(current_col_number) |
||
725 | |||
726 | ws[col + str(current_row_number)].font = title_font |
||
727 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
728 | ws[col + str(current_row_number)] = round2(reporting_period_data['values'][j][i], 2) \ |
||
729 | if i < len(reporting_period_data['values'][j]) else None |
||
730 | if i < len(reporting_period_timestamps[0]): |
||
731 | reporting_period_total += reporting_period_data['values'][j][i] |
||
732 | ws[col + str(current_row_number)].border = f_border |
||
733 | |||
734 | current_col_number += 1 |
||
735 | col = format_cell.get_column_letter(current_col_number) |
||
736 | ws[col + str(current_row_number)].font = title_font |
||
737 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
738 | ws[col + str(current_row_number)] = round2(reporting_period_total, 2) \ |
||
739 | if i < len(reporting_period_timestamps[0]) else None |
||
740 | ws[col + str(current_row_number)].border = f_border |
||
741 | |||
742 | current_row_number += 1 |
||
743 | |||
744 | current_col_number = 2 |
||
745 | col = format_cell.get_column_letter(current_col_number) |
||
746 | ws[col + str(current_row_number)].font = title_font |
||
747 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
748 | ws[col + str(current_row_number)] = _('Subtotal') |
||
749 | ws[col + str(current_row_number)].border = f_border |
||
750 | |||
751 | base_period_subtotals = Decimal(0.0) |
||
752 | |||
753 | for i in range(0, base_period_data_ca_len): |
||
754 | current_col_number += 1 |
||
755 | col = format_cell.get_column_letter(current_col_number) |
||
756 | ws[col + str(current_row_number)].font = title_font |
||
757 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
758 | ws[col + str(current_row_number)] = round2(base_period_data['subtotals'][i], 2) |
||
759 | base_period_subtotals += base_period_data['subtotals'][i] |
||
760 | ws[col + str(current_row_number)].border = f_border |
||
761 | |||
762 | current_col_number += 1 |
||
763 | col = format_cell.get_column_letter(current_col_number) |
||
764 | ws[col + str(current_row_number)].font = title_font |
||
765 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
766 | ws[col + str(current_row_number)] = round2(base_period_subtotals, 2) |
||
767 | ws[col + str(current_row_number)].border = f_border |
||
768 | |||
769 | current_col_number += 1 |
||
770 | col = format_cell.get_column_letter(current_col_number) |
||
771 | |||
772 | ws[col + str(current_row_number)].font = title_font |
||
773 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
774 | ws[col + str(current_row_number)] = _('Subtotal') |
||
775 | ws[col + str(current_row_number)].border = f_border |
||
776 | |||
777 | reporting_period_subtotals = Decimal(0.0) |
||
778 | |||
779 | for i in range(0, reporting_period_data_ca_len): |
||
780 | current_col_number += 1 |
||
781 | col = format_cell.get_column_letter(current_col_number) |
||
782 | ws[col + str(current_row_number)].font = title_font |
||
783 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
784 | ws[col + str(current_row_number)] = round2(reporting_period_data['subtotals'][i], 2) |
||
785 | reporting_period_subtotals += reporting_period_data['subtotals'][i] |
||
786 | ws[col + str(current_row_number)].border = f_border |
||
787 | |||
788 | current_col_number += 1 |
||
789 | col = format_cell.get_column_letter(current_col_number) |
||
790 | ws[col + str(current_row_number)].font = title_font |
||
791 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
792 | ws[col + str(current_row_number)] = round2(reporting_period_subtotals, 2) |
||
793 | ws[col + str(current_row_number)].border = f_border |
||
794 | |||
795 | for i in range(0, reporting_period_data_ca_len): |
||
796 | # line |
||
797 | line = LineChart() |
||
798 | line.title = _('Base Period Carbon Dioxide Emissions') + " / " \ |
||
799 | + _('Reporting Period Carbon Dioxide Emissions') + ' - ' \ |
||
800 | + reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
801 | labels = Reference(ws, min_col=2 + base_period_data_ca_len + 1 + 1, |
||
802 | min_row=table_start_row_number + 1, |
||
803 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
804 | base_line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, |
||
805 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
806 | reporting_line_data = Reference(ws, min_col=3 + base_period_data_ca_len + 1 + 1 + i, |
||
807 | min_row=table_start_row_number, |
||
808 | max_row=table_start_row_number |
||
809 | + len(reporting_period_timestamps[0])) |
||
810 | line.add_data(base_line_data, titles_from_data=True) |
||
811 | line.add_data(reporting_line_data, titles_from_data=True) |
||
812 | line.set_categories(labels) |
||
813 | for j in range(len(line.series)): |
||
814 | line.series[j].marker.symbol = "auto" |
||
815 | line.series[j].smooth = True |
||
816 | line.x_axis.crosses = 'min' |
||
817 | line.height = 8.25 |
||
818 | line.width = 24 |
||
819 | chart_col = 'B' |
||
820 | chart_cell = chart_col + str(table_start_draw_flag + 6 * i) |
||
821 | ws.add_chart(line, chart_cell) |
||
822 | |||
823 | current_row_number += 2 |
||
824 | |||
825 | #################################################################################################################### |
||
826 | |||
827 | if "associated_equipment" not in report.keys() or \ |
||
828 | "energy_category_names" not in report['associated_equipment'].keys() or \ |
||
829 | len(report['associated_equipment']["energy_category_names"]) == 0 \ |
||
830 | or 'associated_equipment_names_array' not in report['associated_equipment'].keys() \ |
||
831 | or report['associated_equipment']['associated_equipment_names_array'] is None \ |
||
832 | or len(report['associated_equipment']['associated_equipment_names_array']) == 0 \ |
||
833 | or len(report['associated_equipment']['associated_equipment_names_array'][0]) == 0: |
||
834 | pass |
||
835 | else: |
||
836 | associated_equipment = report['associated_equipment'] |
||
837 | current_row_number += 1 |
||
838 | |||
839 | ws['B' + str(current_row_number)].font = title_font |
||
840 | ws['B' + str(current_row_number)] = name + ' ' + _('Associated Equipment Data') |
||
841 | |||
842 | current_row_number += 1 |
||
843 | |||
844 | ws.row_dimensions[current_row_number].height = 60 |
||
845 | ws['B' + str(current_row_number)].fill = table_fill |
||
846 | ws['B' + str(current_row_number)].font = name_font |
||
847 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
848 | ws['B' + str(current_row_number)].border = f_border |
||
849 | ws['B' + str(current_row_number)] = _('Associated Equipment') |
||
850 | ca_len = len(associated_equipment['energy_category_names']) |
||
851 | |||
852 | for i in range(0, ca_len): |
||
853 | col = chr(ord('C') + i) |
||
854 | ws[col + str(current_row_number)].fill = table_fill |
||
855 | ws[col + str(current_row_number)].font = name_font |
||
856 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
857 | ws[col + str(current_row_number)].border = f_border |
||
858 | ws[col + str(current_row_number)] = \ |
||
859 | associated_equipment['energy_category_names'][i] + " (" + associated_equipment['units'][i] + ")" |
||
860 | |||
861 | end_col = chr(ord('B') + ca_len + 1) |
||
862 | ws[end_col + str(current_row_number)].fill = table_fill |
||
863 | ws[end_col + str(current_row_number)].font = name_font |
||
864 | ws[end_col + str(current_row_number)].alignment = c_c_alignment |
||
865 | ws[end_col + str(current_row_number)].border = f_border |
||
866 | ws[end_col + str(current_row_number)] = _("Total") + " (" + reporting_period_data['units'][i] + ")" |
||
0 ignored issues
–
show
|
|||
867 | |||
868 | associated_equipment_len = len(associated_equipment['associated_equipment_names_array'][0]) |
||
869 | |||
870 | for i in range(0, associated_equipment_len): |
||
871 | current_row_number += 1 |
||
872 | row = str(current_row_number) |
||
873 | value = Decimal(0.0) |
||
874 | |||
875 | ws['B' + row].font = title_font |
||
876 | ws['B' + row].alignment = c_c_alignment |
||
877 | ws['B' + row] = associated_equipment['associated_equipment_names_array'][0][i] |
||
878 | ws['B' + row].border = f_border |
||
879 | |||
880 | for j in range(0, ca_len): |
||
881 | col = chr(ord('C') + j) |
||
882 | ws[col + row].font = title_font |
||
883 | ws[col + row].alignment = c_c_alignment |
||
884 | ws[col + row] = round2(associated_equipment['subtotals_array'][j][i], 2) |
||
885 | value += round(associated_equipment['subtotals_array'][j][i], 2) |
||
886 | ws[col + row].border = f_border |
||
887 | |||
888 | end_col = chr(ord(col) + 1) |
||
0 ignored issues
–
show
|
|||
889 | ws[end_col + row].font = title_font |
||
890 | ws[end_col + row].alignment = c_c_alignment |
||
891 | ws[end_col + row] = round2(value, 2) |
||
892 | ws[end_col + row].border = f_border |
||
893 | |||
894 | #################################################################################################################### |
||
895 | current_sheet_parameters_row_number = table_start_draw_flag + len(reporting_period_data['names']) * 6 + 1 |
||
896 | if 'parameters' not in report.keys() or \ |
||
897 | report['parameters'] is None or \ |
||
898 | 'names' not in report['parameters'].keys() or \ |
||
899 | report['parameters']['names'] is None or \ |
||
900 | len(report['parameters']['names']) == 0 or \ |
||
901 | 'timestamps' not in report['parameters'].keys() or \ |
||
902 | report['parameters']['timestamps'] is None or \ |
||
903 | len(report['parameters']['timestamps']) == 0 or \ |
||
904 | 'values' not in report['parameters'].keys() or \ |
||
905 | report['parameters']['values'] is None or \ |
||
906 | len(report['parameters']['values']) == 0 or \ |
||
907 | timestamps_data_all_equal_0(report['parameters']['timestamps']): |
||
908 | pass |
||
909 | else: |
||
910 | ################################################################################################################ |
||
911 | # new worksheet |
||
912 | ################################################################################################################ |
||
913 | |||
914 | parameters_data = report['parameters'] |
||
915 | parameters_names_len = len(parameters_data['names']) |
||
916 | |||
917 | file_name = (re.sub(r'[^A-Z]', '', ws.title)) + '_' |
||
918 | parameters_ws = wb.create_sheet(file_name + _('Parameters')) |
||
919 | |||
920 | parameters_timestamps_data_max_len = \ |
||
921 | get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
||
922 | |||
923 | # Row height |
||
924 | parameters_ws.row_dimensions[1].height = 102 |
||
925 | for i in range(2, 7 + 1): |
||
926 | parameters_ws.row_dimensions[i].height = 42 |
||
927 | |||
928 | for i in range(8, parameters_timestamps_data_max_len + 10): |
||
929 | parameters_ws.row_dimensions[i].height = 60 |
||
930 | |||
931 | # Col width |
||
932 | parameters_ws.column_dimensions['A'].width = 1.5 |
||
933 | |||
934 | parameters_ws.column_dimensions['B'].width = 25.0 |
||
935 | |||
936 | for i in range(3, 12 + parameters_names_len * 3): |
||
937 | parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
||
938 | |||
939 | # Img |
||
940 | img = Image("excelexporters/myems.png") |
||
941 | parameters_ws.add_image(img, 'A1') |
||
942 | |||
943 | # Title |
||
944 | parameters_ws['B3'].alignment = b_r_alignment |
||
945 | parameters_ws['B3'] = _('Name') + ':' |
||
946 | parameters_ws['C3'].border = b_border |
||
947 | parameters_ws['C3'].alignment = b_c_alignment |
||
948 | parameters_ws['C3'] = name |
||
949 | |||
950 | parameters_ws['D3'].alignment = b_r_alignment |
||
951 | parameters_ws['D3'] = _('Period Type') + ':' |
||
952 | parameters_ws['E3'].border = b_border |
||
953 | parameters_ws['E3'].alignment = b_c_alignment |
||
954 | parameters_ws['E3'] = period_type |
||
955 | |||
956 | parameters_ws['B4'].alignment = b_r_alignment |
||
957 | parameters_ws['B4'] = _('Reporting Start Datetime') + ':' |
||
958 | parameters_ws['C4'].border = b_border |
||
959 | parameters_ws['C4'].alignment = b_c_alignment |
||
960 | parameters_ws['C4'] = reporting_start_datetime_local |
||
961 | |||
962 | parameters_ws['D4'].alignment = b_r_alignment |
||
963 | parameters_ws['D4'] = _('Reporting End Datetime') + ':' |
||
964 | parameters_ws['E4'].border = b_border |
||
965 | parameters_ws['E4'].alignment = b_c_alignment |
||
966 | parameters_ws['E4'] = reporting_end_datetime_local |
||
967 | |||
968 | parameters_ws_current_row_number = 6 |
||
969 | |||
970 | parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
||
971 | parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' ' + _('Parameters') |
||
972 | |||
973 | parameters_ws_current_row_number += 1 |
||
974 | |||
975 | parameters_table_start_row_number = parameters_ws_current_row_number |
||
976 | |||
977 | parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
||
978 | |||
979 | parameters_ws_current_row_number += 1 |
||
980 | |||
981 | table_current_col_number = 2 |
||
982 | |||
983 | for i in range(0, parameters_names_len): |
||
984 | |||
985 | if len(parameters_data['timestamps'][i]) == 0: |
||
986 | continue |
||
987 | |||
988 | col = format_cell.get_column_letter(table_current_col_number) |
||
989 | |||
990 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
991 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
992 | |||
993 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
994 | |||
995 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
996 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
997 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = name_font |
||
998 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = c_c_alignment |
||
999 | parameters_ws[col + str(parameters_ws_current_row_number - 1)] = parameters_data['names'][i] |
||
1000 | |||
1001 | table_current_row_number = parameters_ws_current_row_number |
||
1002 | |||
1003 | for j, value in enumerate(list(parameters_data['timestamps'][i])): |
||
1004 | col = format_cell.get_column_letter(table_current_col_number) |
||
1005 | |||
1006 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
1007 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
1008 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
1009 | parameters_ws[col + str(table_current_row_number)] = value |
||
1010 | |||
1011 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
1012 | |||
1013 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
1014 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
1015 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
1016 | parameters_ws[col + str(table_current_row_number)] = round2(parameters_data['values'][i][j], 2) |
||
1017 | |||
1018 | table_current_row_number += 1 |
||
1019 | |||
1020 | table_current_col_number = table_current_col_number + 3 |
||
1021 | |||
1022 | ################################################################################################################ |
||
1023 | # parameters chart and parameters table |
||
1024 | ################################################################################################################ |
||
1025 | |||
1026 | ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
||
1027 | ws['B' + str(current_sheet_parameters_row_number)] = name + ' ' + _('Parameters') |
||
1028 | |||
1029 | current_sheet_parameters_row_number += 1 |
||
1030 | |||
1031 | chart_start_row_number = current_sheet_parameters_row_number |
||
1032 | |||
1033 | col_index = 0 |
||
1034 | |||
1035 | for i in range(0, parameters_names_len): |
||
1036 | |||
1037 | if len(parameters_data['timestamps'][i]) == 0: |
||
1038 | continue |
||
1039 | |||
1040 | line = LineChart() |
||
1041 | data_col = 3 + col_index * 3 |
||
1042 | labels_col = 2 + col_index * 3 |
||
1043 | col_index += 1 |
||
1044 | line.title = _('Parameters') + ' - ' + \ |
||
1045 | parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
||
1046 | labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
||
1047 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
1048 | line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
||
1049 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
1050 | line.add_data(line_data, titles_from_data=True) |
||
1051 | line.set_categories(labels) |
||
1052 | line_data = line.series[0] |
||
1053 | line_data.marker.symbol = "auto" |
||
1054 | line_data.smooth = True |
||
1055 | line.x_axis.crosses = 'min' |
||
1056 | line.height = 8.25 |
||
1057 | line.width = 24 |
||
1058 | chart_col = 'B' |
||
1059 | chart_cell = chart_col + str(chart_start_row_number) |
||
1060 | chart_start_row_number += 6 |
||
1061 | ws.add_chart(line, chart_cell) |
||
1062 | |||
1063 | current_sheet_parameters_row_number = chart_start_row_number |
||
1064 | |||
1065 | current_sheet_parameters_row_number += 1 |
||
1066 | #################################################################################################################### |
||
1067 | filename = str(uuid.uuid4()) + '.xlsx' |
||
1068 | wb.save(filename) |
||
1069 | |||
1070 | return filename |
||
1071 | |||
1072 | |||
1073 | def timestamps_data_all_equal_0(lists): |
||
1074 | for i, value in enumerate(list(lists)): |
||
1075 | if len(value) > 0: |
||
1076 | return False |
||
1077 | |||
1078 | return True |
||
1079 | |||
1080 | |||
1081 | def get_parameters_timestamps_lists_max_len(parameters_timestamps_lists): |
||
1082 | max_len = 0 |
||
1083 | for i, value in enumerate(list(parameters_timestamps_lists)): |
||
1084 | if len(value) > max_len: |
||
1085 | max_len = len(value) |
||
1086 | |||
1087 | return max_len |
||
1088 | |||
1089 | |||
1090 | def timestamps_data_not_equal_0(lists): |
||
1091 | number = 0 |
||
1092 | for i, value in enumerate(list(lists)): |
||
1093 | if len(value) > 0: |
||
1094 | number += 1 |
||
1095 | return number |
||
1096 | |||
1097 | |||
1098 | View Code Duplication | def is_base_period_timestamp_exists(base_period_data): |
|
0 ignored issues
–
show
|
|||
1099 | timestamps = base_period_data['timestamps'] |
||
1100 | |||
1101 | if len(timestamps) == 0: |
||
1102 | return False |
||
1103 | |||
1104 | for timestamp in timestamps: |
||
1105 | if len(timestamp) > 0: |
||
1106 | return True |
||
1107 | |||
1108 | return False |
||
1109 |