1 | import base64 |
||
2 | from core.utilities import get_translation |
||
3 | import os |
||
4 | import re |
||
5 | import uuid |
||
6 | import openpyxl.utils.cell as format_cell |
||
7 | from openpyxl import Workbook |
||
8 | from openpyxl.chart import LineChart, Reference |
||
9 | from openpyxl.drawing.image import Image |
||
10 | from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
||
11 | from core.utilities import round2 |
||
12 | |||
13 | ######################################################################################################################## |
||
14 | # PROCEDURES |
||
15 | # Step 1: Validate the report data |
||
16 | # Step 2: Generate excel file |
||
17 | # Step 3: Encode the excel file to Base64 |
||
18 | ######################################################################################################################## |
||
19 | |||
20 | |||
21 | View Code Duplication | def export(report, |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
22 | name, |
||
23 | base_period_start_datetime_local, |
||
24 | base_period_end_datetime_local, |
||
25 | reporting_start_datetime_local, |
||
26 | reporting_end_datetime_local, |
||
27 | period_type, |
||
28 | language): |
||
29 | #################################################################################################################### |
||
30 | # Step 1: Validate the report data |
||
31 | #################################################################################################################### |
||
32 | if report is None: |
||
33 | return None |
||
34 | print(report) |
||
35 | |||
36 | #################################################################################################################### |
||
37 | # Step 2: Generate excel file from the report data |
||
38 | #################################################################################################################### |
||
39 | filename = generate_excel(report, |
||
40 | name, |
||
41 | base_period_start_datetime_local, |
||
42 | base_period_end_datetime_local, |
||
43 | reporting_start_datetime_local, |
||
44 | reporting_end_datetime_local, |
||
45 | period_type, |
||
46 | language) |
||
47 | #################################################################################################################### |
||
48 | # Step 3: Encode the excel file to Base64 |
||
49 | #################################################################################################################### |
||
50 | binary_file_data = b'' |
||
51 | try: |
||
52 | with open(filename, 'rb') as binary_file: |
||
53 | binary_file_data = binary_file.read() |
||
54 | except IOError as ex: |
||
55 | print(str(ex)) |
||
56 | |||
57 | # Base64 encode the bytes |
||
58 | base64_encoded_data = base64.b64encode(binary_file_data) |
||
59 | # get the Base64 encoded data using human-readable characters. |
||
60 | base64_message = base64_encoded_data.decode('utf-8') |
||
61 | # delete the file from server |
||
62 | try: |
||
63 | os.remove(filename) |
||
64 | except NotImplementedError as ex: |
||
65 | print(str(ex)) |
||
66 | return base64_message |
||
67 | |||
68 | |||
69 | def generate_excel(report, |
||
70 | name, |
||
71 | base_period_start_datetime_local, |
||
72 | base_period_end_datetime_local, |
||
73 | reporting_start_datetime_local, |
||
74 | reporting_end_datetime_local, |
||
75 | period_type, |
||
76 | language): |
||
77 | |||
78 | trans = get_translation(language) |
||
79 | trans.install() |
||
80 | _ = trans.gettext |
||
81 | |||
82 | wb = Workbook() |
||
83 | ws = wb.active |
||
84 | ws.title = "CombinedEquipmentOutput" |
||
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 | # Img |
||
132 | img = Image("excelexporters/myems.png") |
||
133 | ws.add_image(img, 'A1') |
||
134 | |||
135 | # Title |
||
136 | ws['B3'].alignment = b_r_alignment |
||
137 | ws['B3'] = _('Name') + ':' |
||
138 | ws['C3'].border = b_border |
||
139 | ws['C3'].alignment = b_c_alignment |
||
140 | ws['C3'] = name |
||
141 | |||
142 | ws['D3'].alignment = b_r_alignment |
||
143 | ws['D3'] = _('Period Type') + ':' |
||
144 | ws['E3'].border = b_border |
||
145 | ws['E3'].alignment = b_c_alignment |
||
146 | ws['E3'] = period_type |
||
147 | |||
148 | ws['B4'].alignment = b_r_alignment |
||
149 | ws['B4'] = _('Reporting Start Datetime') + ':' |
||
150 | ws['C4'].border = b_border |
||
151 | ws['C4'].alignment = b_c_alignment |
||
152 | ws['C4'] = reporting_start_datetime_local |
||
153 | |||
154 | ws['D4'].alignment = b_r_alignment |
||
155 | ws['D4'] = _('Reporting End Datetime') + ':' |
||
156 | ws['E4'].border = b_border |
||
157 | ws['E4'].alignment = b_c_alignment |
||
158 | ws['E4'] = reporting_end_datetime_local |
||
159 | |||
160 | is_base_period_timestamp_exists_flag = is_base_period_timestamp_exists(report['base_period']) |
||
161 | |||
162 | if is_base_period_timestamp_exists_flag: |
||
163 | ws['B5'].alignment = b_r_alignment |
||
164 | ws['B5'] = _('Base Period Start Datetime') + ':' |
||
165 | ws['C5'].border = b_border |
||
166 | ws['C5'].alignment = b_c_alignment |
||
167 | ws['C5'] = base_period_start_datetime_local |
||
168 | |||
169 | ws['D5'].alignment = b_r_alignment |
||
170 | ws['D5'] = _('Base Period End Datetime') + ':' |
||
171 | ws['E5'].border = b_border |
||
172 | ws['E5'].alignment = b_c_alignment |
||
173 | ws['E5'] = base_period_end_datetime_local |
||
174 | |||
175 | if "reporting_period" not in report.keys() or \ |
||
176 | "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
||
177 | filename = str(uuid.uuid4()) + '.xlsx' |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
178 | wb.save(filename) |
||
179 | |||
180 | return filename |
||
181 | |||
182 | #################################################################################################################### |
||
183 | |||
184 | current_row_number = 7 |
||
185 | |||
186 | reporting_period_data = report['reporting_period'] |
||
187 | |||
188 | View Code Duplication | if "names" not in reporting_period_data.keys() or \ |
|
0 ignored issues
–
show
|
|||
189 | reporting_period_data['names'] is None or \ |
||
190 | len(reporting_period_data['names']) == 0: |
||
191 | pass |
||
192 | else: |
||
193 | ws['B' + str(current_row_number)].font = title_font |
||
194 | ws['B' + str(current_row_number)] = name + ' ' + _('Reporting Period Output') |
||
195 | |||
196 | current_row_number += 1 |
||
197 | |||
198 | category = reporting_period_data['names'] |
||
199 | ca_len = len(category) |
||
200 | |||
201 | ws.row_dimensions[current_row_number].height = 60 |
||
202 | ws['B' + str(current_row_number)].fill = table_fill |
||
203 | ws['B' + str(current_row_number)].border = f_border |
||
204 | |||
205 | col = 'C' |
||
206 | |||
207 | for i in range(0, ca_len): |
||
208 | ws[col + str(current_row_number)].fill = table_fill |
||
209 | ws[col + str(current_row_number)].font = name_font |
||
210 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
211 | ws[col + str(current_row_number)].border = f_border |
||
212 | ws[col + str(current_row_number)] = \ |
||
213 | reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
214 | |||
215 | col = chr(ord(col) + 1) |
||
216 | |||
217 | current_row_number += 1 |
||
218 | |||
219 | ws['B' + str(current_row_number)].font = title_font |
||
220 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
221 | ws['B' + str(current_row_number)].border = f_border |
||
222 | ws['B' + str(current_row_number)] = _('Output') |
||
223 | |||
224 | col = 'C' |
||
225 | |||
226 | for i in range(0, ca_len): |
||
227 | ws[col + str(current_row_number)].font = name_font |
||
228 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
229 | ws[col + str(current_row_number)].border = f_border |
||
230 | ws[col + str(current_row_number)] = round2(reporting_period_data['subtotals'][i], 2) |
||
231 | |||
232 | col = chr(ord(col) + 1) |
||
233 | |||
234 | current_row_number += 1 |
||
235 | |||
236 | ws['B' + str(current_row_number)].font = title_font |
||
237 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
238 | ws['B' + str(current_row_number)].border = f_border |
||
239 | ws['B' + str(current_row_number)] = _('Increment Rate') |
||
240 | |||
241 | col = 'C' |
||
242 | |||
243 | for i in range(0, ca_len): |
||
244 | ws[col + str(current_row_number)].font = name_font |
||
245 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
246 | ws[col + str(current_row_number)].border = f_border |
||
247 | ws[col + str(current_row_number)] = str( |
||
248 | round2(reporting_period_data['increment_rates'][i] * 100, 2)) + '%' \ |
||
249 | if reporting_period_data['increment_rates'][i] is not None else '-' |
||
250 | |||
251 | col = chr(ord(col) + 1) |
||
252 | |||
253 | current_row_number += 2 |
||
254 | |||
255 | #################################################################################################################### |
||
256 | table_start_draw_flag = current_row_number + 1 |
||
257 | |||
258 | if 'values' not in reporting_period_data.keys() or \ |
||
259 | reporting_period_data['values'] is None or \ |
||
260 | len(reporting_period_data['values']) == 0 or \ |
||
261 | 'timestamps' not in reporting_period_data.keys() or \ |
||
262 | reporting_period_data['timestamps'] is None or \ |
||
263 | len(reporting_period_data['timestamps']) == 0 or \ |
||
264 | len(reporting_period_data['timestamps'][0]) == 0: |
||
265 | pass |
||
266 | else: |
||
267 | |||
268 | if not is_base_period_timestamp_exists_flag: |
||
269 | reporting_period_data = report['reporting_period'] |
||
270 | times = reporting_period_data['timestamps'] |
||
271 | ca_len = len(report['reporting_period']['names']) |
||
272 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
||
273 | ws['B' + str(current_row_number)].font = title_font |
||
274 | ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data') |
||
275 | |||
276 | current_row_number += 1 |
||
277 | # 1: Stand for blank line 2: Stand for title |
||
278 | current_row_number += ca_len * 6 + real_timestamps_len * 6 + 1 + 2 |
||
279 | table_start_row_number = current_row_number |
||
280 | |||
281 | time = times[0] |
||
282 | has_data = False |
||
283 | |||
284 | if len(time) > 0: |
||
285 | has_data = True |
||
286 | |||
287 | if has_data: |
||
288 | |||
289 | ws.row_dimensions[current_row_number].height = 60 |
||
290 | current_col_number = 2 |
||
291 | col = format_cell.get_column_letter(current_col_number) |
||
292 | ws[col + str(current_row_number)].fill = table_fill |
||
293 | ws[col + str(current_row_number)].font = title_font |
||
294 | ws[col + str(current_row_number)].border = f_border |
||
295 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
296 | ws[col + str(current_row_number)] = _('Datetime') |
||
297 | |||
298 | for i in range(0, ca_len): |
||
299 | current_col_number += 1 |
||
300 | col = format_cell.get_column_letter(current_col_number) |
||
301 | |||
302 | ws[col + str(current_row_number)].fill = table_fill |
||
303 | ws[col + str(current_row_number)].font = title_font |
||
304 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
305 | ws[col + str(current_row_number)] = reporting_period_data['names'][i] + \ |
||
306 | " (" + reporting_period_data['units'][i] + ")" |
||
307 | ws[col + str(current_row_number)].border = f_border |
||
308 | |||
309 | current_row_number += 1 |
||
310 | |||
311 | for i in range(0, len(time)): |
||
312 | current_col_number = 2 |
||
313 | col = format_cell.get_column_letter(current_col_number) |
||
314 | ws[col + str(current_row_number)].font = title_font |
||
315 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
316 | ws[col + str(current_row_number)] = time[i] |
||
317 | ws[col + str(current_row_number)].border = f_border |
||
318 | |||
319 | for j in range(0, ca_len): |
||
320 | current_col_number += 1 |
||
321 | col = format_cell.get_column_letter(current_col_number) |
||
322 | |||
323 | ws[col + str(current_row_number)].font = title_font |
||
324 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
325 | ws[col + str(current_row_number)] = round2(reporting_period_data['values'][j][i], 2) |
||
326 | ws[col + str(current_row_number)].border = f_border |
||
327 | |||
328 | current_row_number += 1 |
||
329 | |||
330 | table_end_row_number = current_row_number - 1 |
||
331 | |||
332 | current_col_number = 2 |
||
333 | col = format_cell.get_column_letter(current_col_number) |
||
334 | ws[col + str(current_row_number)].font = title_font |
||
335 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
336 | ws[col + str(current_row_number)] = _('Subtotal') |
||
337 | ws[col + str(current_row_number)].border = f_border |
||
338 | |||
339 | for i in range(0, ca_len): |
||
340 | current_col_number += 1 |
||
341 | col = format_cell.get_column_letter(current_col_number) |
||
342 | |||
343 | ws[col + str(current_row_number)].font = title_font |
||
344 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
345 | ws[col + str(current_row_number)] = round2(reporting_period_data['subtotals'][i], 2) |
||
346 | ws[col + str(current_row_number)].border = f_border |
||
347 | |||
348 | # line |
||
349 | line = LineChart() |
||
350 | line.title = _('Reporting Period Output') + ' - ' \ |
||
351 | + reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
352 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
353 | line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, |
||
354 | max_row=table_end_row_number) |
||
355 | line.add_data(line_data, titles_from_data=True) |
||
356 | line.set_categories(labels) |
||
357 | line_data = line.series[0] |
||
358 | line_data.marker.symbol = "auto" |
||
359 | line_data.smooth = True |
||
360 | line.x_axis.crosses = 'min' |
||
361 | line.height = 8.25 |
||
362 | line.width = 24 |
||
363 | chart_col = 'B' |
||
364 | chart_cell = chart_col + str(table_start_draw_flag + 6 * i) |
||
365 | ws.add_chart(line, chart_cell) |
||
366 | |||
367 | current_row_number += 2 |
||
368 | else: |
||
369 | base_period_data = report['base_period'] |
||
370 | reporting_period_data = report['reporting_period'] |
||
371 | base_period_timestamps = base_period_data['timestamps'] |
||
372 | reporting_period_timestamps = reporting_period_data['timestamps'] |
||
373 | # Tip: |
||
374 | # base_period_data['names'] == reporting_period_data['names'] |
||
375 | # base_period_data['units'] == reporting_period_data['units'] |
||
376 | base_period_data_ca_len = len(base_period_data['names']) |
||
377 | reporting_period_data_ca_len = len(reporting_period_data['names']) |
||
378 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
||
379 | ws['B' + str(current_row_number)].font = title_font |
||
380 | ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data') |
||
381 | |||
382 | current_row_number += 1 |
||
383 | # 1: Stand for blank line 2: Stand for title |
||
384 | current_row_number += reporting_period_data_ca_len * 6 + real_timestamps_len * 6 + 1 + 2 |
||
385 | table_start_row_number = current_row_number |
||
386 | |||
387 | has_data = False |
||
388 | |||
389 | if len(base_period_timestamps[0]) or len(reporting_period_timestamps[0]) > 0: |
||
390 | has_data = True |
||
391 | |||
392 | if has_data: |
||
393 | ws.row_dimensions[current_row_number].height = 60 |
||
394 | current_col_number = 2 |
||
395 | col = format_cell.get_column_letter(current_col_number) |
||
396 | ws[col + str(current_row_number)].fill = table_fill |
||
397 | ws[col + str(current_row_number)].font = title_font |
||
398 | ws[col + str(current_row_number)].border = f_border |
||
399 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
400 | ws[col + str(current_row_number)] = _('Base Period') + " - " + _('Datetime') |
||
401 | |||
402 | for i in range(0, base_period_data_ca_len): |
||
403 | current_col_number += 1 |
||
404 | col = format_cell.get_column_letter(current_col_number) |
||
405 | |||
406 | ws[col + str(current_row_number)].fill = table_fill |
||
407 | ws[col + str(current_row_number)].font = title_font |
||
408 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
409 | ws[col + str(current_row_number)] = _('Base Period') + " - " + base_period_data['names'][i] + \ |
||
410 | " (" + base_period_data['units'][i] + ")" |
||
411 | ws[col + str(current_row_number)].border = f_border |
||
412 | current_col_number += 1 |
||
413 | col = format_cell.get_column_letter(current_col_number) |
||
414 | |||
415 | ws[col + str(current_row_number)].fill = table_fill |
||
416 | ws[col + str(current_row_number)].font = title_font |
||
417 | ws[col + str(current_row_number)].border = f_border |
||
418 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
419 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " + _('Datetime') |
||
420 | |||
421 | for i in range(0, reporting_period_data_ca_len): |
||
422 | current_col_number += 1 |
||
423 | col = format_cell.get_column_letter(current_col_number) |
||
424 | ws[col + str(current_row_number)].fill = table_fill |
||
425 | ws[col + str(current_row_number)].font = title_font |
||
426 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
427 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " \ |
||
428 | + reporting_period_data['names'][i] + " (" + \ |
||
429 | reporting_period_data['units'][i] + ")" |
||
430 | ws[col + str(current_row_number)].border = f_border |
||
431 | |||
432 | current_row_number += 1 |
||
433 | |||
434 | max_timestamps_len = len(base_period_timestamps[0]) \ |
||
435 | if len(base_period_timestamps[0]) >= len(reporting_period_timestamps[0]) \ |
||
436 | else len(reporting_period_timestamps[0]) |
||
437 | |||
438 | for i in range(0, max_timestamps_len): |
||
439 | current_col_number = 2 |
||
440 | col = format_cell.get_column_letter(current_col_number) |
||
441 | ws[col + str(current_row_number)].font = title_font |
||
442 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
443 | ws[col + str(current_row_number)] = base_period_timestamps[0][i] \ |
||
444 | if i < len(base_period_timestamps[0]) else None |
||
445 | ws[col + str(current_row_number)].border = f_border |
||
446 | |||
447 | for j in range(0, base_period_data_ca_len): |
||
448 | current_col_number += 1 |
||
449 | col = format_cell.get_column_letter(current_col_number) |
||
450 | |||
451 | ws[col + str(current_row_number)].font = title_font |
||
452 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
453 | ws[col + str(current_row_number)] = round2(base_period_data['values'][j][i], 2) \ |
||
454 | if i < len(base_period_data['values'][j]) else None |
||
455 | ws[col + str(current_row_number)].border = f_border |
||
456 | current_col_number += 1 |
||
457 | col = format_cell.get_column_letter(current_col_number) |
||
458 | |||
459 | ws[col + str(current_row_number)].font = title_font |
||
460 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
461 | ws[col + str(current_row_number)] = reporting_period_timestamps[0][i] \ |
||
462 | if i < len(reporting_period_timestamps[0]) else None |
||
463 | ws[col + str(current_row_number)].border = f_border |
||
464 | |||
465 | for j in range(0, reporting_period_data_ca_len): |
||
466 | current_col_number += 1 |
||
467 | col = format_cell.get_column_letter(current_col_number) |
||
468 | |||
469 | ws[col + str(current_row_number)].font = title_font |
||
470 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
471 | ws[col + str(current_row_number)] = round2(reporting_period_data['values'][j][i], 2) \ |
||
472 | if i < len(reporting_period_data['values'][j]) else None |
||
473 | ws[col + str(current_row_number)].border = f_border |
||
474 | |||
475 | current_row_number += 1 |
||
476 | |||
477 | current_col_number = 2 |
||
478 | col = format_cell.get_column_letter(current_col_number) |
||
479 | ws[col + str(current_row_number)].font = title_font |
||
480 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
481 | ws[col + str(current_row_number)] = _('Subtotal') |
||
482 | ws[col + str(current_row_number)].border = f_border |
||
483 | |||
484 | for i in range(0, base_period_data_ca_len): |
||
485 | current_col_number += 1 |
||
486 | col = format_cell.get_column_letter(current_col_number) |
||
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(base_period_data['subtotals'][i], 2) |
||
490 | ws[col + str(current_row_number)].border = f_border |
||
491 | |||
492 | current_col_number += 1 |
||
493 | col = format_cell.get_column_letter(current_col_number) |
||
494 | |||
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)] = _('Subtotal') |
||
498 | ws[col + str(current_row_number)].border = f_border |
||
499 | |||
500 | for i in range(0, reporting_period_data_ca_len): |
||
501 | current_col_number += 1 |
||
502 | col = format_cell.get_column_letter(current_col_number) |
||
503 | ws[col + str(current_row_number)].font = title_font |
||
504 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
505 | ws[col + str(current_row_number)] = round2(reporting_period_data['subtotals'][i], 2) |
||
506 | ws[col + str(current_row_number)].border = f_border |
||
507 | |||
508 | for i in range(0, reporting_period_data_ca_len): |
||
509 | # line |
||
510 | line = LineChart() |
||
511 | line.title = _('Base Period Output') + ' / ' \ |
||
512 | + _('Reporting Period Output') + ' - ' \ |
||
513 | + reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
514 | labels = Reference(ws, min_col=2 + base_period_data_ca_len + 1, |
||
515 | min_row=table_start_row_number + 1, |
||
516 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
517 | base_line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, |
||
518 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
519 | reporting_line_data = Reference(ws, min_col=3 + base_period_data_ca_len + 1 + i, |
||
520 | min_row=table_start_row_number, |
||
521 | max_row=table_start_row_number |
||
522 | + len(reporting_period_timestamps[0])) |
||
523 | line.add_data(base_line_data, titles_from_data=True) |
||
524 | line.add_data(reporting_line_data, titles_from_data=True) |
||
525 | line.set_categories(labels) |
||
526 | for j in range(len(line.series)): |
||
527 | line.series[j].marker.symbol = "auto" |
||
528 | line.series[j].smooth = True |
||
529 | line.x_axis.crosses = 'min' |
||
530 | line.height = 8.25 |
||
531 | line.width = 24 |
||
532 | chart_col = 'B' |
||
533 | chart_cell = chart_col + str(table_start_draw_flag + 6 * i) |
||
534 | ws.add_chart(line, chart_cell) |
||
535 | |||
536 | current_row_number += 2 |
||
537 | |||
538 | #################################################################################################################### |
||
539 | if "associated_equipment" not in report.keys() or \ |
||
540 | "energy_category_names" not in report['associated_equipment'].keys() or \ |
||
541 | len(report['associated_equipment']["energy_category_names"]) == 0 \ |
||
542 | or 'associated_equipment_names_array' not in report['associated_equipment'].keys() \ |
||
543 | or report['associated_equipment']['associated_equipment_names_array'] is None \ |
||
544 | or len(report['associated_equipment']['associated_equipment_names_array']) == 0 \ |
||
545 | or len(report['associated_equipment']['associated_equipment_names_array'][0]) == 0: |
||
546 | pass |
||
547 | else: |
||
548 | associated_equipment = report['associated_equipment'] |
||
549 | |||
550 | ws['B' + str(current_row_number)].font = title_font |
||
551 | ws['B' + str(current_row_number)] = name + ' ' + _('Associated Equipment Data') |
||
552 | |||
553 | current_row_number += 1 |
||
554 | |||
555 | ws.row_dimensions[current_row_number].height = 60 |
||
556 | ws['B' + str(current_row_number)].fill = table_fill |
||
557 | ws['B' + str(current_row_number)].font = name_font |
||
558 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
559 | ws['B' + str(current_row_number)].border = f_border |
||
560 | ws['B' + str(current_row_number)] = _('Associated Equipment') |
||
561 | ca_len = len(associated_equipment['energy_category_names']) |
||
562 | |||
563 | for i in range(0, ca_len): |
||
564 | row = chr(ord('C') + i) |
||
565 | ws[row + str(current_row_number)].fill = table_fill |
||
566 | ws[row + str(current_row_number)].font = name_font |
||
567 | ws[row + str(current_row_number)].alignment = c_c_alignment |
||
568 | ws[row + str(current_row_number)].border = f_border |
||
569 | ws[row + str(current_row_number)] = \ |
||
570 | associated_equipment['energy_category_names'][i] + " (" + associated_equipment['units'][i] + ")" |
||
571 | |||
572 | associated_equipment_len = len(associated_equipment['associated_equipment_names_array'][0]) |
||
573 | |||
574 | for i in range(0, associated_equipment_len): |
||
575 | current_row_number += 1 |
||
576 | row = str(current_row_number) |
||
577 | |||
578 | ws['B' + row].font = title_font |
||
579 | ws['B' + row].alignment = c_c_alignment |
||
580 | ws['B' + row] = associated_equipment['associated_equipment_names_array'][0][i] |
||
581 | ws['B' + row].border = f_border |
||
582 | |||
583 | for j in range(0, ca_len): |
||
584 | col = chr(ord('C') + j) |
||
585 | ws[col + row].font = title_font |
||
586 | ws[col + row].alignment = c_c_alignment |
||
587 | ws[col + row] = round2(associated_equipment['subtotals_array'][j][i], 2) |
||
588 | ws[col + row].border = f_border |
||
589 | #################################################################################################################### |
||
590 | current_sheet_parameters_row_number = table_start_draw_flag + len(reporting_period_data['names']) * 6 + 1 |
||
591 | if 'parameters' not in report.keys() or \ |
||
592 | report['parameters'] is None or \ |
||
593 | 'names' not in report['parameters'].keys() or \ |
||
594 | report['parameters']['names'] is None or \ |
||
595 | len(report['parameters']['names']) == 0 or \ |
||
596 | 'timestamps' not in report['parameters'].keys() or \ |
||
597 | report['parameters']['timestamps'] is None or \ |
||
598 | len(report['parameters']['timestamps']) == 0 or \ |
||
599 | 'values' not in report['parameters'].keys() or \ |
||
600 | report['parameters']['values'] is None or \ |
||
601 | len(report['parameters']['values']) == 0 or \ |
||
602 | timestamps_data_all_equal_0(report['parameters']['timestamps']): |
||
603 | pass |
||
604 | else: |
||
605 | ################################################################################################################ |
||
606 | # new worksheet |
||
607 | ################################################################################################################ |
||
608 | |||
609 | parameters_data = report['parameters'] |
||
610 | parameters_names_len = len(parameters_data['names']) |
||
611 | |||
612 | file_name = (re.sub(r'[^A-Z]', '', ws.title))+'_' |
||
613 | parameters_ws = wb.create_sheet(file_name + _('Parameters')) |
||
614 | |||
615 | parameters_timestamps_data_max_len = \ |
||
616 | get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
||
617 | |||
618 | # Row height |
||
619 | parameters_ws.row_dimensions[1].height = 102 |
||
620 | for i in range(2, 7 + 1): |
||
621 | parameters_ws.row_dimensions[i].height = 42 |
||
622 | |||
623 | for i in range(8, parameters_timestamps_data_max_len + 10): |
||
624 | parameters_ws.row_dimensions[i].height = 60 |
||
625 | |||
626 | # Col width |
||
627 | parameters_ws.column_dimensions['A'].width = 1.5 |
||
628 | |||
629 | parameters_ws.column_dimensions['B'].width = 25.0 |
||
630 | |||
631 | for i in range(3, 12+parameters_names_len*3): |
||
632 | parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
||
633 | |||
634 | # Img |
||
635 | img = Image("excelexporters/myems.png") |
||
636 | parameters_ws.add_image(img, 'A1') |
||
637 | |||
638 | # Title |
||
639 | parameters_ws['B3'].alignment = b_r_alignment |
||
640 | parameters_ws['B3'] = _('Name') + ':' |
||
641 | parameters_ws['C3'].border = b_border |
||
642 | parameters_ws['C3'].alignment = b_c_alignment |
||
643 | parameters_ws['C3'] = name |
||
644 | |||
645 | parameters_ws['D3'].alignment = b_r_alignment |
||
646 | parameters_ws['D3'] = _('Period Type') + ':' |
||
647 | parameters_ws['E3'].border = b_border |
||
648 | parameters_ws['E3'].alignment = b_c_alignment |
||
649 | parameters_ws['E3'] = period_type |
||
650 | |||
651 | parameters_ws['B4'].alignment = b_r_alignment |
||
652 | parameters_ws['B4'] = _('Reporting Start Datetime') + ':' |
||
653 | parameters_ws['C4'].border = b_border |
||
654 | parameters_ws['C4'].alignment = b_c_alignment |
||
655 | parameters_ws['C4'] = reporting_start_datetime_local |
||
656 | |||
657 | parameters_ws['D4'].alignment = b_r_alignment |
||
658 | parameters_ws['D4'] = _('Reporting End Datetime') + ':' |
||
659 | parameters_ws['E4'].border = b_border |
||
660 | parameters_ws['E4'].alignment = b_c_alignment |
||
661 | parameters_ws['E4'] = reporting_end_datetime_local |
||
662 | |||
663 | parameters_ws_current_row_number = 6 |
||
664 | |||
665 | parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
||
666 | parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' ' + _('Parameters') |
||
667 | |||
668 | parameters_ws_current_row_number += 1 |
||
669 | |||
670 | parameters_table_start_row_number = parameters_ws_current_row_number |
||
671 | |||
672 | parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
||
673 | |||
674 | parameters_ws_current_row_number += 1 |
||
675 | |||
676 | table_current_col_number = 2 |
||
677 | |||
678 | for i in range(0, parameters_names_len): |
||
679 | |||
680 | if len(parameters_data['timestamps'][i]) == 0: |
||
681 | continue |
||
682 | |||
683 | col = format_cell.get_column_letter(table_current_col_number) |
||
684 | |||
685 | parameters_ws[col + str(parameters_ws_current_row_number-1)].fill = table_fill |
||
686 | parameters_ws[col + str(parameters_ws_current_row_number-1)].border = f_border |
||
687 | |||
688 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
689 | |||
690 | parameters_ws[col + str(parameters_ws_current_row_number-1)].fill = table_fill |
||
691 | parameters_ws[col + str(parameters_ws_current_row_number-1)].border = f_border |
||
692 | parameters_ws[col + str(parameters_ws_current_row_number-1)].font = name_font |
||
693 | parameters_ws[col + str(parameters_ws_current_row_number-1)].alignment = c_c_alignment |
||
694 | parameters_ws[col + str(parameters_ws_current_row_number-1)] = parameters_data['names'][i] |
||
695 | |||
696 | table_current_row_number = parameters_ws_current_row_number |
||
697 | |||
698 | for j, value in enumerate(list(parameters_data['timestamps'][i])): |
||
699 | col = format_cell.get_column_letter(table_current_col_number) |
||
700 | |||
701 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
702 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
703 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
704 | parameters_ws[col + str(table_current_row_number)] = value |
||
705 | |||
706 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
707 | |||
708 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
709 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
710 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
711 | parameters_ws[col + str(table_current_row_number)] = round2(parameters_data['values'][i][j], 2) |
||
712 | |||
713 | table_current_row_number += 1 |
||
714 | |||
715 | table_current_col_number = table_current_col_number + 3 |
||
716 | |||
717 | ################################################################################################################ |
||
718 | # parameters chart and parameters table |
||
719 | ################################################################################################################ |
||
720 | |||
721 | ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
||
722 | ws['B' + str(current_sheet_parameters_row_number)] = name + ' ' + _('Parameters') |
||
723 | |||
724 | current_sheet_parameters_row_number += 1 |
||
725 | |||
726 | chart_start_row_number = current_sheet_parameters_row_number |
||
727 | |||
728 | col_index = 0 |
||
729 | |||
730 | for i in range(0, parameters_names_len): |
||
731 | |||
732 | if len(parameters_data['timestamps'][i]) == 0: |
||
733 | continue |
||
734 | |||
735 | line = LineChart() |
||
736 | data_col = 3+col_index*3 |
||
737 | labels_col = 2+col_index*3 |
||
738 | col_index += 1 |
||
739 | line.title = _('Parameters') + ' - ' + \ |
||
740 | parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
||
741 | labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
||
742 | max_row=(len(parameters_data['timestamps'][i])+parameters_table_start_row_number)) |
||
743 | line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
||
744 | max_row=(len(parameters_data['timestamps'][i])+parameters_table_start_row_number)) |
||
745 | line.add_data(line_data, titles_from_data=True) |
||
746 | line.set_categories(labels) |
||
747 | line_data = line.series[0] |
||
748 | line_data.marker.symbol = "auto" |
||
749 | line_data.smooth = True |
||
750 | line.x_axis.crosses = 'min' |
||
751 | line.height = 8.25 |
||
752 | line.width = 24 |
||
753 | chart_col = 'B' |
||
754 | chart_cell = chart_col + str(chart_start_row_number) |
||
755 | chart_start_row_number += 6 |
||
756 | ws.add_chart(line, chart_cell) |
||
757 | |||
758 | current_sheet_parameters_row_number = chart_start_row_number |
||
759 | |||
760 | current_sheet_parameters_row_number += 1 |
||
761 | #################################################################################################################### |
||
762 | filename = str(uuid.uuid4()) + '.xlsx' |
||
763 | wb.save(filename) |
||
764 | |||
765 | return filename |
||
766 | |||
767 | |||
768 | def get_parameters_timestamps_lists_max_len(parameters_timestamps_lists): |
||
769 | max_len = 0 |
||
770 | for i, value in enumerate(list(parameters_timestamps_lists)): |
||
771 | if len(value) > max_len: |
||
772 | max_len = len(value) |
||
773 | |||
774 | return max_len |
||
775 | |||
776 | |||
777 | def timestamps_data_all_equal_0(lists): |
||
778 | for i, value in enumerate(list(lists)): |
||
779 | if len(value) > 0: |
||
780 | return False |
||
781 | |||
782 | return True |
||
783 | |||
784 | |||
785 | def timestamps_data_not_equal_0(lists): |
||
786 | number = 0 |
||
787 | for i, value in enumerate(list(lists)): |
||
788 | if len(value) > 0: |
||
789 | number += 1 |
||
790 | return number |
||
791 | |||
792 | |||
793 | View Code Duplication | def is_base_period_timestamp_exists(base_period_data): |
|
0 ignored issues
–
show
|
|||
794 | timestamps = base_period_data['timestamps'] |
||
795 | |||
796 | if len(timestamps) == 0: |
||
797 | return False |
||
798 | |||
799 | for timestamp in timestamps: |
||
800 | if len(timestamp) > 0: |
||
801 | return True |
||
802 | |||
803 | return False |
||
804 |