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