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 bytes 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 | |||
35 | #################################################################################################################### |
||
36 | # Step 2: Generate excel file from the report data |
||
37 | #################################################################################################################### |
||
38 | filename = generate_excel(report, |
||
39 | name, |
||
40 | base_period_start_datetime_local, |
||
41 | base_period_end_datetime_local, |
||
42 | reporting_start_datetime_local, |
||
43 | reporting_end_datetime_local, |
||
44 | period_type, |
||
45 | language) |
||
46 | #################################################################################################################### |
||
47 | # Step 3: Encode the excel file to Base64 |
||
48 | #################################################################################################################### |
||
49 | binary_file_data = b'' |
||
50 | try: |
||
51 | with open(filename, 'rb') as binary_file: |
||
52 | binary_file_data = binary_file.read() |
||
53 | except IOError as ex: |
||
54 | print(str(ex)) |
||
55 | |||
56 | # Base64 encode the bytes |
||
57 | base64_encoded_data = base64.b64encode(binary_file_data) |
||
58 | # get the Base64 encoded data using human-readable characters. |
||
59 | base64_message = base64_encoded_data.decode('utf-8') |
||
60 | # delete the file from server |
||
61 | try: |
||
62 | os.remove(filename) |
||
63 | except NotImplementedError as ex: |
||
64 | print(str(ex)) |
||
65 | return base64_message |
||
66 | |||
67 | |||
68 | def generate_excel(report, |
||
69 | name, |
||
70 | base_period_start_datetime_local, |
||
71 | base_period_end_datetime_local, |
||
72 | reporting_start_datetime_local, |
||
73 | reporting_end_datetime_local, |
||
74 | period_type, |
||
75 | language): |
||
76 | |||
77 | trans = get_translation(language) |
||
78 | trans.install() |
||
79 | _ = trans.gettext |
||
80 | |||
81 | wb = Workbook() |
||
82 | ws = wb.active |
||
83 | ws.title = "CombinedEquipmentEfficiency" |
||
84 | |||
85 | # Row height |
||
86 | ws.row_dimensions[1].height = 102 |
||
87 | for i in range(2, 2000 + 1): |
||
88 | ws.row_dimensions[i].height = 42 |
||
89 | |||
90 | # Col width |
||
91 | ws.column_dimensions['A'].width = 1.5 |
||
92 | |||
93 | ws.column_dimensions['B'].width = 25.0 |
||
94 | |||
95 | for i in range(ord('C'), ord('Z')): |
||
96 | ws.column_dimensions[chr(i)].width = 15.0 |
||
97 | |||
98 | # Font |
||
99 | name_font = Font(name='Arial', size=15, bold=True) |
||
100 | title_font = Font(name='Arial', size=15, bold=True) |
||
101 | |||
102 | table_fill = PatternFill(fill_type='solid', fgColor='90ee90') |
||
103 | f_border = Border(left=Side(border_style='medium'), |
||
104 | right=Side(border_style='medium'), |
||
105 | bottom=Side(border_style='medium'), |
||
106 | top=Side(border_style='medium') |
||
107 | ) |
||
108 | b_border = Border( |
||
109 | bottom=Side(border_style='medium'), |
||
110 | ) |
||
111 | |||
112 | b_c_alignment = Alignment(vertical='bottom', |
||
113 | horizontal='center', |
||
114 | text_rotation=0, |
||
115 | wrap_text=True, |
||
116 | shrink_to_fit=False, |
||
117 | indent=0) |
||
118 | c_c_alignment = Alignment(vertical='center', |
||
119 | horizontal='center', |
||
120 | text_rotation=0, |
||
121 | wrap_text=True, |
||
122 | shrink_to_fit=False, |
||
123 | indent=0) |
||
124 | b_r_alignment = Alignment(vertical='bottom', |
||
125 | horizontal='right', |
||
126 | text_rotation=0, |
||
127 | wrap_text=True, |
||
128 | shrink_to_fit=False, |
||
129 | indent=0) |
||
130 | # Img |
||
131 | img = Image("excelexporters/myems.png") |
||
132 | ws.add_image(img, 'A1') |
||
133 | |||
134 | # Title |
||
135 | ws['B3'].alignment = b_r_alignment |
||
136 | ws['B3'] = _('Name') + ':' |
||
137 | ws['C3'].border = b_border |
||
138 | ws['C3'].alignment = b_c_alignment |
||
139 | ws['C3'] = name |
||
140 | |||
141 | ws['D3'].alignment = b_r_alignment |
||
142 | ws['D3'] = _('Period Type') + ':' |
||
143 | ws['E3'].border = b_border |
||
144 | ws['E3'].alignment = b_c_alignment |
||
145 | ws['E3'] = period_type |
||
146 | |||
147 | ws['B4'].alignment = b_r_alignment |
||
148 | ws['B4'] = _('Reporting Start Datetime') + ':' |
||
149 | ws['C4'].border = b_border |
||
150 | ws['C4'].alignment = b_c_alignment |
||
151 | ws['C4'] = reporting_start_datetime_local |
||
152 | |||
153 | ws['D4'].alignment = b_r_alignment |
||
154 | ws['D4'] = _('Reporting End Datetime') + ':' |
||
155 | ws['E4'].border = b_border |
||
156 | ws['E4'].alignment = b_c_alignment |
||
157 | ws['E4'] = reporting_end_datetime_local |
||
158 | |||
159 | is_base_period_timestamp_exists_flag = is_base_period_timestamp_exists(report['base_period_efficiency']) |
||
160 | |||
161 | if is_base_period_timestamp_exists_flag: |
||
162 | ws['B5'].alignment = b_r_alignment |
||
163 | ws['B5'] = _('Base Period Start Datetime') + ':' |
||
164 | ws['C5'].border = b_border |
||
165 | ws['C5'].alignment = b_c_alignment |
||
166 | ws['C5'] = base_period_start_datetime_local |
||
167 | |||
168 | ws['D5'].alignment = b_r_alignment |
||
169 | ws['D5'] = _('Base Period End Datetime') + ':' |
||
170 | ws['E5'].border = b_border |
||
171 | ws['E5'].alignment = b_c_alignment |
||
172 | ws['E5'] = base_period_end_datetime_local |
||
173 | |||
174 | if "reporting_period_efficiency" not in report.keys() or \ |
||
175 | "names" not in report['reporting_period_efficiency'].keys() or len( |
||
176 | report['reporting_period_efficiency']['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 | reporting_period_data = report['reporting_period_efficiency'] |
||
186 | if "names" not in reporting_period_data.keys() or \ |
||
187 | reporting_period_data['names'] is None or \ |
||
188 | len(reporting_period_data['names']) == 0: |
||
189 | pass |
||
190 | else: |
||
191 | ws['B' + str(current_row_number)].font = title_font |
||
192 | ws['B' + str(current_row_number)] = name + ' ' + _('Reporting Period Cumulative Efficiency') |
||
193 | |||
194 | current_row_number += 1 |
||
195 | |||
196 | category = reporting_period_data['names'] |
||
197 | ca_len = len(category) |
||
198 | |||
199 | ws.row_dimensions[current_row_number].height = 80 |
||
200 | ws['B' + str(current_row_number)].fill = table_fill |
||
201 | ws['B' + str(current_row_number)].border = f_border |
||
202 | |||
203 | col = 'C' |
||
204 | |||
205 | for i in range(0, ca_len): |
||
206 | ws[col + str(current_row_number)].fill = table_fill |
||
207 | ws[col + str(current_row_number)].font = name_font |
||
208 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
209 | ws[col + str(current_row_number)].border = f_border |
||
210 | ws[col + str(current_row_number)] = \ |
||
211 | reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
212 | |||
213 | col = chr(ord(col) + 1) |
||
214 | |||
215 | ws[col + str(current_row_number)].fill = table_fill |
||
216 | ws[col + str(current_row_number)].font = name_font |
||
217 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
218 | ws[col + str(current_row_number)].border = f_border |
||
219 | ws[col + str(current_row_number)] = \ |
||
220 | reporting_period_data['names'][i] + '-' + reporting_period_data['numerator_names'][i] + " (" + \ |
||
221 | reporting_period_data['numerator_units'][i] + ")" |
||
222 | |||
223 | col = chr(ord(col) + 1) |
||
224 | |||
225 | ws[col + str(current_row_number)].fill = table_fill |
||
226 | ws[col + str(current_row_number)].font = name_font |
||
227 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
228 | ws[col + str(current_row_number)].border = f_border |
||
229 | ws[col + str(current_row_number)] = \ |
||
230 | reporting_period_data['names'][i] + '-' + reporting_period_data['denominator_names'][i] + " (" + \ |
||
231 | reporting_period_data['denominator_units'][i] + ")" |
||
232 | |||
233 | col = chr(ord(col) + 1) |
||
234 | |||
235 | current_row_number += 1 |
||
236 | |||
237 | ws['B' + str(current_row_number)].font = title_font |
||
238 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
239 | ws['B' + str(current_row_number)].border = f_border |
||
240 | ws['B' + str(current_row_number)] = _('Cumulative Efficiency') |
||
241 | |||
242 | col = 'C' |
||
243 | |||
244 | for i in range(0, ca_len): |
||
245 | ws[col + str(current_row_number)].font = name_font |
||
246 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
247 | ws[col + str(current_row_number)].border = f_border |
||
248 | ws[col + str(current_row_number)] = round2(reporting_period_data['cumulations'][i], 2) |
||
249 | |||
250 | col = chr(ord(col) + 1) |
||
251 | |||
252 | ws[col + str(current_row_number)].font = name_font |
||
253 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
254 | ws[col + str(current_row_number)].border = f_border |
||
255 | ws[col + str(current_row_number)] = round2(reporting_period_data['numerator_cumulations'][i], 2) |
||
256 | |||
257 | col = chr(ord(col) + 1) |
||
258 | |||
259 | ws[col + str(current_row_number)].font = name_font |
||
260 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
261 | ws[col + str(current_row_number)].border = f_border |
||
262 | ws[col + str(current_row_number)] = round2(reporting_period_data['denominator_cumulations'][i], 2) |
||
263 | |||
264 | col = chr(ord(col) + 1) |
||
265 | |||
266 | current_row_number += 1 |
||
267 | |||
268 | ws['B' + str(current_row_number)].font = title_font |
||
269 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
270 | ws['B' + str(current_row_number)].border = f_border |
||
271 | ws['B' + str(current_row_number)] = _('Increment Rate') |
||
272 | |||
273 | col = 'C' |
||
274 | |||
275 | for i in range(0, ca_len): |
||
276 | ws[col + str(current_row_number)].font = name_font |
||
277 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
278 | ws[col + str(current_row_number)].border = f_border |
||
279 | ws[col + str(current_row_number)] = str( |
||
280 | round2(reporting_period_data['increment_rates'][i] * 100, 2)) + '%' \ |
||
281 | if reporting_period_data['increment_rates'][i] is not None else '-' |
||
282 | |||
283 | col = chr(ord(col) + 1) |
||
284 | |||
285 | ws[col + str(current_row_number)].font = name_font |
||
286 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
287 | ws[col + str(current_row_number)].border = f_border |
||
288 | ws[col + str(current_row_number)] = str( |
||
289 | round2(reporting_period_data['increment_rates_num'][i] * 100, 2)) + '%' \ |
||
290 | if reporting_period_data['increment_rates_num'][i] is not None else '-' |
||
291 | |||
292 | col = chr(ord(col) + 1) |
||
293 | |||
294 | ws[col + str(current_row_number)].font = name_font |
||
295 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
296 | ws[col + str(current_row_number)].border = f_border |
||
297 | ws[col + str(current_row_number)] = str( |
||
298 | round2(reporting_period_data['increment_rates_den'][i] * 100, 2)) + '%' \ |
||
299 | if reporting_period_data['increment_rates_den'][i] is not None else '-' |
||
300 | |||
301 | col = chr(ord(col) + 1) |
||
302 | |||
303 | current_row_number += 2 |
||
304 | |||
305 | #################################################################################################################### |
||
306 | |||
307 | has_parameters_names_and_timestamps_and_values_data = True |
||
308 | current_sheet_parameters_row_number = current_row_number |
||
309 | |||
310 | View Code Duplication | if 'parameters' not in report.keys() or \ |
|
0 ignored issues
–
show
|
|||
311 | report['parameters'] is None or \ |
||
312 | 'names' not in report['parameters'].keys() or \ |
||
313 | report['parameters']['names'] is None or \ |
||
314 | len(report['parameters']['names']) == 0 or \ |
||
315 | 'timestamps' not in report['parameters'].keys() or \ |
||
316 | report['parameters']['timestamps'] is None or \ |
||
317 | len(report['parameters']['timestamps']) == 0 or \ |
||
318 | 'values' not in report['parameters'].keys() or \ |
||
319 | report['parameters']['values'] is None or \ |
||
320 | len(report['parameters']['values']) == 0 or \ |
||
321 | timestamps_data_all_equal_0(report['parameters']['timestamps']): |
||
322 | |||
323 | has_parameters_names_and_timestamps_and_values_data = False |
||
324 | |||
325 | #################################################################################################################### |
||
326 | has_values_data = True |
||
327 | has_timestamps_data = True |
||
328 | |||
329 | if 'values' not in reporting_period_data.keys() or \ |
||
330 | reporting_period_data['values'] is None or \ |
||
331 | len(reporting_period_data['values']) == 0: |
||
332 | has_values_data = False |
||
333 | |||
334 | if 'timestamps' not in reporting_period_data.keys() or \ |
||
335 | reporting_period_data['timestamps'] is None or \ |
||
336 | len(reporting_period_data['timestamps']) == 0 or \ |
||
337 | len(reporting_period_data['timestamps'][0]) == 0: |
||
338 | has_timestamps_data = False |
||
339 | |||
340 | if not is_base_period_timestamp_exists_flag: |
||
341 | if has_values_data and has_timestamps_data: |
||
342 | ca_len = len(reporting_period_data['names']) |
||
343 | time = reporting_period_data['timestamps'][0] |
||
344 | |||
345 | ws['B' + str(current_row_number)].font = title_font |
||
346 | ws['B' + str(current_row_number)] = name + ' ' + _('Reporting Period Cumulative Efficiency') |
||
347 | |||
348 | current_row_number += 1 |
||
349 | |||
350 | chart_start_row_number = current_row_number |
||
351 | |||
352 | current_row_number += ca_len * 3 * 6 + 1 |
||
353 | |||
354 | if has_parameters_names_and_timestamps_and_values_data: |
||
355 | current_sheet_parameters_row_number = current_row_number |
||
356 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
||
357 | current_row_number += 6 * real_timestamps_len + 2 |
||
358 | |||
359 | ws['B' + str(current_row_number)].font = title_font |
||
360 | ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data') |
||
361 | |||
362 | current_row_number += 1 |
||
363 | |||
364 | table_start_row_number = current_row_number |
||
365 | |||
366 | ws.row_dimensions[current_row_number].height = 85 |
||
367 | current_col_number = 2 |
||
368 | col = format_cell.get_column_letter(current_col_number) |
||
369 | ws[col + str(current_row_number)].fill = table_fill |
||
370 | ws[col + str(current_row_number)].font = title_font |
||
371 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
372 | ws[col + str(current_row_number)].border = f_border |
||
373 | ws[col + str(current_row_number)] = _('Datetime') |
||
374 | |||
375 | for i in range(0, ca_len): |
||
376 | current_col_number += 1 |
||
377 | col = format_cell.get_column_letter(current_col_number) |
||
378 | |||
379 | ws[col + str(current_row_number)].fill = table_fill |
||
380 | ws[col + str(current_row_number)].font = title_font |
||
381 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
382 | ws[col + str(current_row_number)].border = f_border |
||
383 | ws[col + str(current_row_number)] = \ |
||
384 | reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
385 | |||
386 | current_col_number += 1 |
||
387 | col = format_cell.get_column_letter(current_col_number) |
||
388 | |||
389 | ws[col + str(current_row_number)].fill = table_fill |
||
390 | ws[col + str(current_row_number)].font = title_font |
||
391 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
392 | ws[col + str(current_row_number)].border = f_border |
||
393 | ws[col + str(current_row_number)] = \ |
||
394 | reporting_period_data['names'][i] + "-" + \ |
||
395 | reporting_period_data['numerator_names'][i] + " (" + reporting_period_data['numerator_units'][ |
||
396 | i] + ")" |
||
397 | |||
398 | current_col_number += 1 |
||
399 | col = format_cell.get_column_letter(current_col_number) |
||
400 | |||
401 | ws[col + str(current_row_number)].fill = table_fill |
||
402 | ws[col + str(current_row_number)].font = title_font |
||
403 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
404 | ws[col + str(current_row_number)].border = f_border |
||
405 | ws[col + str(current_row_number)] = \ |
||
406 | reporting_period_data['names'][i] + "-" + \ |
||
407 | reporting_period_data['denominator_names'][i] + " (" + reporting_period_data['denominator_units'][ |
||
408 | i] + ")" |
||
409 | |||
410 | current_row_number += 1 |
||
411 | |||
412 | for i in range(0, len(time)): |
||
413 | current_col_number = 2 |
||
414 | col = format_cell.get_column_letter(current_col_number) |
||
415 | ws[col + str(current_row_number)].font = title_font |
||
416 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
417 | ws[col + str(current_row_number)].border = f_border |
||
418 | ws[col + str(current_row_number)] = time[i] |
||
419 | |||
420 | for j in range(0, ca_len): |
||
421 | current_col_number += 1 |
||
422 | col = format_cell.get_column_letter(current_col_number) |
||
423 | |||
424 | ws[col + str(current_row_number)].font = title_font |
||
425 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
426 | ws[col + str(current_row_number)].border = f_border |
||
427 | ws[col + str(current_row_number)] = round2(reporting_period_data['values'][j][i], 2) \ |
||
428 | if reporting_period_data['values'][j][i] is not None else None |
||
429 | |||
430 | current_col_number += 1 |
||
431 | col = format_cell.get_column_letter(current_col_number) |
||
432 | |||
433 | ws[col + str(current_row_number)].font = title_font |
||
434 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
435 | ws[col + str(current_row_number)].border = f_border |
||
436 | ws[col + str(current_row_number)] = round2(reporting_period_data['numerator_values'][j][i], 2) \ |
||
437 | if reporting_period_data['numerator_values'][j][i] is not None else None |
||
438 | |||
439 | current_col_number += 1 |
||
440 | col = format_cell.get_column_letter(current_col_number) |
||
441 | |||
442 | ws[col + str(current_row_number)].font = title_font |
||
443 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
444 | ws[col + str(current_row_number)].border = f_border |
||
445 | ws[col + str(current_row_number)] = round2(reporting_period_data['denominator_values'][j][i], 2) \ |
||
446 | if reporting_period_data['denominator_values'][j][i] is not None else None |
||
447 | |||
448 | current_row_number += 1 |
||
449 | |||
450 | table_end_row_number = current_row_number - 1 |
||
451 | |||
452 | current_col_number = 2 |
||
453 | col = format_cell.get_column_letter(current_col_number) |
||
454 | ws[col + str(current_row_number)].font = title_font |
||
455 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
456 | ws[col + str(current_row_number)].border = f_border |
||
457 | ws[col + str(current_row_number)] = _('Subtotal') |
||
458 | |||
459 | for i in range(0, ca_len): |
||
460 | current_col_number += 1 |
||
461 | col = format_cell.get_column_letter(current_col_number) |
||
462 | |||
463 | ws[col + str(current_row_number)].font = title_font |
||
464 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
465 | ws[col + str(current_row_number)].border = f_border |
||
466 | ws[col + str(current_row_number)] = round2(reporting_period_data['cumulations'][i], 2) \ |
||
467 | if reporting_period_data['cumulations'][i] is not None else None |
||
468 | |||
469 | current_col_number += 1 |
||
470 | col = format_cell.get_column_letter(current_col_number) |
||
471 | |||
472 | ws[col + str(current_row_number)].font = title_font |
||
473 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
474 | ws[col + str(current_row_number)].border = f_border |
||
475 | ws[col + str(current_row_number)] = round2(reporting_period_data['numerator_cumulations'][i], 2) \ |
||
476 | if reporting_period_data['numerator_cumulations'][i] is not None else None |
||
477 | |||
478 | current_col_number += 1 |
||
479 | col = format_cell.get_column_letter(current_col_number) |
||
480 | |||
481 | ws[col + str(current_row_number)].font = title_font |
||
482 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
483 | ws[col + str(current_row_number)].border = f_border |
||
484 | ws[col + str(current_row_number)] = round2(reporting_period_data['denominator_cumulations'][i], 2) \ |
||
485 | if reporting_period_data['denominator_cumulations'][i] is not None else None |
||
486 | |||
487 | current_row_number += 2 |
||
488 | |||
489 | for i in range(0, ca_len): |
||
490 | line = LineChart() |
||
491 | line.title = _('Reporting Period Cumulative Efficiency') + ' - ' + \ |
||
492 | reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
493 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
494 | line_data = Reference(ws, min_col=3 + i * 3, min_row=table_start_row_number, |
||
495 | max_row=table_end_row_number) |
||
496 | line.add_data(line_data, titles_from_data=True) |
||
497 | line.set_categories(labels) |
||
498 | line_data = line.series[0] |
||
499 | line_data.marker.symbol = "auto" |
||
500 | line_data.smooth = True |
||
501 | line.x_axis.crosses = 'min' |
||
502 | line.height = 8.25 |
||
503 | line.width = 24 |
||
504 | chart_col = 'B' |
||
505 | chart_cell = chart_col + str(chart_start_row_number) |
||
506 | chart_start_row_number += 6 |
||
507 | ws.add_chart(line, chart_cell) |
||
508 | |||
509 | line = LineChart() |
||
510 | line.title = _('Reporting Period Cumulative Efficiency') + ' - ' + \ |
||
511 | reporting_period_data['names'][i] + "-" + \ |
||
512 | reporting_period_data['numerator_names'][i] \ |
||
513 | + " (" + reporting_period_data['numerator_units'][i] + ")" |
||
514 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
515 | line_data = Reference(ws, min_col=4 + i * 3, min_row=table_start_row_number, |
||
516 | max_row=table_end_row_number) |
||
517 | line.add_data(line_data, titles_from_data=True) |
||
518 | line.set_categories(labels) |
||
519 | line_data = line.series[0] |
||
520 | line_data.marker.symbol = "auto" |
||
521 | line_data.smooth = True |
||
522 | line.x_axis.crosses = 'min' |
||
523 | line.height = 8.25 |
||
524 | line.width = 24 |
||
525 | chart_col = 'B' |
||
526 | chart_cell = chart_col + str(chart_start_row_number) |
||
527 | chart_start_row_number += 6 |
||
528 | ws.add_chart(line, chart_cell) |
||
529 | |||
530 | line = LineChart() |
||
531 | line.title = _('Reporting Period Cumulative Efficiency') + ' - ' + \ |
||
532 | reporting_period_data['names'][i] + "-" + \ |
||
533 | reporting_period_data['denominator_names'][i] \ |
||
534 | + " (" + reporting_period_data['denominator_units'][i] + ")" |
||
535 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
536 | line_data = Reference(ws, min_col=5 + i * 3, min_row=table_start_row_number, |
||
537 | max_row=table_end_row_number) |
||
538 | line.add_data(line_data, titles_from_data=True) |
||
539 | line.set_categories(labels) |
||
540 | line_data = line.series[0] |
||
541 | line_data.marker.symbol = "auto" |
||
542 | line_data.smooth = True |
||
543 | line.x_axis.crosses = 'min' |
||
544 | line.height = 8.25 |
||
545 | line.width = 24 |
||
546 | chart_col = 'B' |
||
547 | chart_cell = chart_col + str(chart_start_row_number) |
||
548 | chart_start_row_number += 6 |
||
549 | ws.add_chart(line, chart_cell) |
||
550 | else: |
||
551 | if has_values_data and has_timestamps_data: |
||
552 | base_period_data = report['base_period_efficiency'] |
||
553 | reporting_period_data = report['reporting_period_efficiency'] |
||
554 | base_period_timestamps = base_period_data['timestamps'] |
||
555 | reporting_period_timestamps = reporting_period_data['timestamps'] |
||
556 | # Tip: |
||
557 | # base_period_data['names'] == reporting_period_data['names'] |
||
558 | # base_period_data['units'] == reporting_period_data['units'] |
||
559 | reporting_period_data_ca_len = len(reporting_period_data['names']) |
||
560 | base_period_data_ca_len = reporting_period_data_ca_len |
||
561 | ws['B' + str(current_row_number)].font = title_font |
||
562 | ws['B' + str(current_row_number)] = name + ' ' + _('Reporting Period Cumulative Efficiency') |
||
563 | |||
564 | current_row_number += 1 |
||
565 | |||
566 | chart_start_row_number = current_row_number |
||
567 | |||
568 | current_row_number += reporting_period_data_ca_len * 3 * 6 + 1 |
||
569 | |||
570 | if has_parameters_names_and_timestamps_and_values_data: |
||
571 | current_sheet_parameters_row_number = current_row_number |
||
572 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
||
573 | current_row_number += 6 * real_timestamps_len + 2 |
||
574 | |||
575 | ws['B' + str(current_row_number)].font = title_font |
||
576 | ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data') |
||
577 | |||
578 | current_row_number += 1 |
||
579 | |||
580 | table_start_row_number = current_row_number |
||
581 | |||
582 | ws.row_dimensions[current_row_number].height = 85 |
||
583 | current_col_number = 2 |
||
584 | col = format_cell.get_column_letter(current_col_number) |
||
585 | ws[col + str(current_row_number)].fill = table_fill |
||
586 | ws[col + str(current_row_number)].font = title_font |
||
587 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
588 | ws[col + str(current_row_number)].border = f_border |
||
589 | ws[col + str(current_row_number)] = _('Base Period') + " - " + _('Datetime') |
||
590 | |||
591 | for i in range(0, base_period_data_ca_len): |
||
592 | current_col_number += 1 |
||
593 | col = format_cell.get_column_letter(current_col_number) |
||
594 | |||
595 | ws[col + str(current_row_number)].fill = table_fill |
||
596 | ws[col + str(current_row_number)].font = title_font |
||
597 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
598 | ws[col + str(current_row_number)] = _('Base Period') + " - " + reporting_period_data['names'][i] + \ |
||
599 | " (" + reporting_period_data['units'][i] + ")" |
||
600 | ws[col + str(current_row_number)].border = f_border |
||
601 | |||
602 | current_col_number += 1 |
||
603 | col = format_cell.get_column_letter(current_col_number) |
||
604 | |||
605 | ws[col + str(current_row_number)].fill = table_fill |
||
606 | ws[col + str(current_row_number)].font = title_font |
||
607 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
608 | ws[col + str(current_row_number)].border = f_border |
||
609 | ws[col + str(current_row_number)] = _('Base Period') + " - " + \ |
||
610 | reporting_period_data['names'][i] + "-" + \ |
||
611 | reporting_period_data['numerator_names'][i] + " (" + \ |
||
612 | reporting_period_data['numerator_units'][ |
||
613 | i] + ")" |
||
614 | |||
615 | current_col_number += 1 |
||
616 | col = format_cell.get_column_letter(current_col_number) |
||
617 | |||
618 | ws[col + str(current_row_number)].fill = table_fill |
||
619 | ws[col + str(current_row_number)].font = title_font |
||
620 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
621 | ws[col + str(current_row_number)].border = f_border |
||
622 | ws[col + str(current_row_number)] = _('Base Period') + " - " + \ |
||
623 | reporting_period_data['names'][i] + "-" + \ |
||
624 | reporting_period_data['denominator_names'][i] + " (" + \ |
||
625 | reporting_period_data['denominator_units'][ |
||
626 | i] + ")" |
||
627 | |||
628 | current_col_number += 1 |
||
629 | col = format_cell.get_column_letter(current_col_number) |
||
630 | |||
631 | ws[col + str(current_row_number)].fill = table_fill |
||
632 | ws[col + str(current_row_number)].font = title_font |
||
633 | ws[col + str(current_row_number)].border = f_border |
||
634 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
635 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " + _('Datetime') |
||
636 | |||
637 | for i in range(0, reporting_period_data_ca_len): |
||
638 | current_col_number += 1 |
||
639 | col = format_cell.get_column_letter(current_col_number) |
||
640 | ws[col + str(current_row_number)].fill = table_fill |
||
641 | ws[col + str(current_row_number)].font = title_font |
||
642 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
643 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " \ |
||
644 | + reporting_period_data['names'][i] + " (" + \ |
||
645 | reporting_period_data['units'][i] + ")" |
||
646 | ws[col + str(current_row_number)].border = f_border |
||
647 | |||
648 | current_col_number += 1 |
||
649 | col = format_cell.get_column_letter(current_col_number) |
||
650 | |||
651 | ws[col + str(current_row_number)].fill = table_fill |
||
652 | ws[col + str(current_row_number)].font = title_font |
||
653 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
654 | ws[col + str(current_row_number)].border = f_border |
||
655 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " + \ |
||
656 | reporting_period_data['names'][i] + "-" + \ |
||
657 | reporting_period_data['numerator_names'][i] + " (" + \ |
||
658 | reporting_period_data['numerator_units'][i] + ")" |
||
659 | |||
660 | current_col_number += 1 |
||
661 | col = format_cell.get_column_letter(current_col_number) |
||
662 | |||
663 | ws[col + str(current_row_number)].fill = table_fill |
||
664 | ws[col + str(current_row_number)].font = title_font |
||
665 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
666 | ws[col + str(current_row_number)].border = f_border |
||
667 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " + \ |
||
668 | reporting_period_data['names'][i] + "-" + \ |
||
669 | reporting_period_data['denominator_names'][i] + " (" + \ |
||
670 | reporting_period_data['denominator_units'][i] + ")" |
||
671 | |||
672 | current_row_number += 1 |
||
673 | |||
674 | max_timestamps_len = len(base_period_timestamps[0]) \ |
||
675 | if len(base_period_timestamps[0]) >= len(reporting_period_timestamps[0]) \ |
||
676 | else len(reporting_period_timestamps[0]) |
||
677 | |||
678 | for i in range(0, max_timestamps_len): |
||
679 | current_col_number = 2 |
||
680 | col = format_cell.get_column_letter(current_col_number) |
||
681 | ws[col + str(current_row_number)].font = title_font |
||
682 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
683 | ws[col + str(current_row_number)] = base_period_timestamps[0][i] \ |
||
684 | if i < len(base_period_timestamps[0]) else None |
||
685 | ws[col + str(current_row_number)].border = f_border |
||
686 | |||
687 | for j in range(0, base_period_data_ca_len): |
||
688 | current_col_number += 1 |
||
689 | col = format_cell.get_column_letter(current_col_number) |
||
690 | |||
691 | ws[col + str(current_row_number)].font = title_font |
||
692 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
693 | ws[col + str(current_row_number)] = round2(base_period_data['values'][j][i], 2) \ |
||
694 | if i < len(base_period_data['values'][j]) \ |
||
695 | and base_period_data['values'][j][i] is not None else None |
||
696 | ws[col + str(current_row_number)].border = f_border |
||
697 | |||
698 | current_col_number += 1 |
||
699 | col = format_cell.get_column_letter(current_col_number) |
||
700 | |||
701 | ws[col + str(current_row_number)].font = title_font |
||
702 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
703 | ws[col + str(current_row_number)] = round2(base_period_data['numerator_values'][j][i], 2) \ |
||
704 | if i < len(base_period_data['numerator_values'][j]) \ |
||
705 | and base_period_data['numerator_values'][j][i] is not None else None |
||
706 | ws[col + str(current_row_number)].border = f_border |
||
707 | |||
708 | current_col_number += 1 |
||
709 | col = format_cell.get_column_letter(current_col_number) |
||
710 | |||
711 | ws[col + str(current_row_number)].font = title_font |
||
712 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
713 | ws[col + str(current_row_number)] = round2(base_period_data['denominator_values'][j][i], 2) \ |
||
714 | if i < len(base_period_data['denominator_values'][j]) \ |
||
715 | and base_period_data['denominator_values'][j][i] is not None else None |
||
716 | ws[col + str(current_row_number)].border = f_border |
||
717 | current_col_number += 1 |
||
718 | col = format_cell.get_column_letter(current_col_number) |
||
719 | |||
720 | ws[col + str(current_row_number)].font = title_font |
||
721 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
722 | ws[col + str(current_row_number)] = reporting_period_timestamps[0][i] \ |
||
723 | if i < len(reporting_period_timestamps[0]) else None |
||
724 | ws[col + str(current_row_number)].border = f_border |
||
725 | |||
726 | for j in range(0, reporting_period_data_ca_len): |
||
727 | current_col_number += 1 |
||
728 | col = format_cell.get_column_letter(current_col_number) |
||
729 | |||
730 | ws[col + str(current_row_number)].font = title_font |
||
731 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
732 | ws[col + str(current_row_number)] = round2(reporting_period_data['values'][j][i], 2) \ |
||
733 | if i < len(reporting_period_data['values'][j]) \ |
||
734 | and reporting_period_data['values'][j][i] is not None else None |
||
735 | ws[col + str(current_row_number)].border = f_border |
||
736 | |||
737 | current_col_number += 1 |
||
738 | col = format_cell.get_column_letter(current_col_number) |
||
739 | |||
740 | ws[col + str(current_row_number)].font = title_font |
||
741 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
742 | ws[col + str(current_row_number)] = round2(reporting_period_data['numerator_values'][j][i], 2) \ |
||
743 | if i < len(reporting_period_data['numerator_values'][j]) \ |
||
744 | and reporting_period_data['numerator_values'][j][i] is not None else None |
||
745 | ws[col + str(current_row_number)].border = f_border |
||
746 | |||
747 | current_col_number += 1 |
||
748 | col = format_cell.get_column_letter(current_col_number) |
||
749 | |||
750 | ws[col + str(current_row_number)].font = title_font |
||
751 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
752 | ws[col + str(current_row_number)] = round2(reporting_period_data['denominator_values'][j][i], 2) \ |
||
753 | if i < len(reporting_period_data['denominator_values'][j]) \ |
||
754 | and reporting_period_data['denominator_values'][j][i] is not None else None |
||
755 | ws[col + str(current_row_number)].border = f_border |
||
756 | |||
757 | current_row_number += 1 |
||
758 | |||
759 | current_col_number = 2 |
||
760 | col = format_cell.get_column_letter(current_col_number) |
||
761 | |||
762 | ws[col + str(current_row_number)].font = title_font |
||
763 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
764 | ws[col + str(current_row_number)] = _('Subtotal') |
||
765 | ws[col + str(current_row_number)].border = f_border |
||
766 | |||
767 | for i in range(0, base_period_data_ca_len): |
||
768 | current_col_number += 1 |
||
769 | col = format_cell.get_column_letter(current_col_number) |
||
770 | ws[col + str(current_row_number)].font = title_font |
||
771 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
772 | ws[col + str(current_row_number)] = round2(base_period_data['cumulations'][i], 2) \ |
||
773 | if base_period_data['cumulations'][i] is not None else None |
||
774 | ws[col + str(current_row_number)].border = f_border |
||
775 | |||
776 | current_col_number += 1 |
||
777 | col = format_cell.get_column_letter(current_col_number) |
||
778 | ws[col + str(current_row_number)].font = title_font |
||
779 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
780 | ws[col + str(current_row_number)] = round2(base_period_data['numerator_cumulations'][i], 2) \ |
||
781 | if base_period_data['numerator_cumulations'][i] is not None else None |
||
782 | ws[col + str(current_row_number)].border = f_border |
||
783 | |||
784 | current_col_number += 1 |
||
785 | col = format_cell.get_column_letter(current_col_number) |
||
786 | ws[col + str(current_row_number)].font = title_font |
||
787 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
788 | ws[col + str(current_row_number)] = round2(base_period_data['denominator_cumulations'][i], 2) \ |
||
789 | if base_period_data['denominator_cumulations'][i] is not None else None |
||
790 | ws[col + str(current_row_number)].border = f_border |
||
791 | |||
792 | current_col_number += 1 |
||
793 | col = format_cell.get_column_letter(current_col_number) |
||
794 | |||
795 | ws[col + str(current_row_number)].font = title_font |
||
796 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
797 | ws[col + str(current_row_number)] = _('Subtotal') |
||
798 | ws[col + str(current_row_number)].border = f_border |
||
799 | |||
800 | for i in range(0, reporting_period_data_ca_len): |
||
801 | current_col_number += 1 |
||
802 | col = format_cell.get_column_letter(current_col_number) |
||
803 | ws[col + str(current_row_number)].font = title_font |
||
804 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
805 | ws[col + str(current_row_number)] = round2(reporting_period_data['cumulations'][i], 2) \ |
||
806 | if reporting_period_data['cumulations'][i] is not None else None |
||
807 | ws[col + str(current_row_number)].border = f_border |
||
808 | |||
809 | current_col_number += 1 |
||
810 | col = format_cell.get_column_letter(current_col_number) |
||
811 | ws[col + str(current_row_number)].font = title_font |
||
812 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
813 | ws[col + str(current_row_number)] = round2(reporting_period_data['numerator_cumulations'][i], 2) \ |
||
814 | if reporting_period_data['numerator_cumulations'][i] is not None else None |
||
815 | ws[col + str(current_row_number)].border = f_border |
||
816 | |||
817 | current_col_number += 1 |
||
818 | col = format_cell.get_column_letter(current_col_number) |
||
819 | ws[col + str(current_row_number)].font = title_font |
||
820 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
821 | ws[col + str(current_row_number)] = round2(reporting_period_data['denominator_cumulations'][i], 2) \ |
||
822 | if reporting_period_data['denominator_cumulations'][i] is not None else None |
||
823 | ws[col + str(current_row_number)].border = f_border |
||
824 | |||
825 | current_row_number += 2 |
||
826 | |||
827 | for i in range(0, reporting_period_data_ca_len): |
||
828 | line = LineChart() |
||
829 | line.title = _('Base Period Cumulative Efficiency') + ' / ' \ |
||
830 | + _('Reporting Period Cumulative Efficiency') + ' - ' \ |
||
831 | + reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
832 | labels = Reference(ws, min_col=2 + base_period_data_ca_len * 3 + 1, |
||
833 | min_row=table_start_row_number + 1, |
||
834 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
835 | base_line_data = Reference(ws, min_col=3 + i * 3, |
||
836 | min_row=table_start_row_number, |
||
837 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
838 | reporting_line_data = Reference(ws, min_col=3 + base_period_data_ca_len * 3 + 1 + i * 3, |
||
839 | min_row=table_start_row_number, |
||
840 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
841 | line.add_data(base_line_data, titles_from_data=True) |
||
842 | line.add_data(reporting_line_data, titles_from_data=True) |
||
843 | line.set_categories(labels) |
||
844 | for j in range(len(line.series)): |
||
845 | line.series[j].marker.symbol = "auto" |
||
846 | line.series[j].smooth = True |
||
847 | line.x_axis.crosses = 'min' |
||
848 | line.height = 8.25 |
||
849 | line.width = 24 |
||
850 | chart_col = 'B' |
||
851 | chart_cell = chart_col + str(chart_start_row_number) |
||
852 | chart_start_row_number += 6 |
||
853 | ws.add_chart(line, chart_cell) |
||
854 | |||
855 | line = LineChart() |
||
856 | line.title = _('Base Period Cumulative Efficiency') + ' / ' \ |
||
857 | + _('Reporting Period Cumulative Efficiency') + ' - ' \ |
||
858 | + reporting_period_data['names'][i] + "-" \ |
||
859 | + reporting_period_data['numerator_names'][i] \ |
||
860 | + " (" + reporting_period_data['numerator_units'][i] + ")" |
||
861 | labels = Reference(ws, min_col=2 + base_period_data_ca_len * 3 + 1, |
||
862 | min_row=table_start_row_number + 1, |
||
863 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
864 | base_line_data = Reference(ws, min_col=4 + i * 3, |
||
865 | min_row=table_start_row_number, |
||
866 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
867 | reporting_line_data = Reference(ws, min_col=4 + base_period_data_ca_len * 3 + 1 + i * 3, |
||
868 | min_row=table_start_row_number, |
||
869 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
870 | line.add_data(base_line_data, titles_from_data=True) |
||
871 | line.add_data(reporting_line_data, titles_from_data=True) |
||
872 | line.set_categories(labels) |
||
873 | for j in range(len(line.series)): |
||
874 | line.series[j].marker.symbol = "auto" |
||
875 | line.series[j].smooth = True |
||
876 | line.x_axis.crosses = 'min' |
||
877 | line.height = 8.25 |
||
878 | line.width = 24 |
||
879 | chart_col = 'B' |
||
880 | chart_cell = chart_col + str(chart_start_row_number) |
||
881 | chart_start_row_number += 6 |
||
882 | ws.add_chart(line, chart_cell) |
||
883 | |||
884 | line = LineChart() |
||
885 | line.title = _('Base Period Cumulative Efficiency') + ' / ' \ |
||
886 | + _('Reporting Period Cumulative Efficiency') + ' - ' \ |
||
887 | + reporting_period_data['names'][i] + "-" \ |
||
888 | + reporting_period_data['denominator_names'][i] \ |
||
889 | + " (" + reporting_period_data['denominator_units'][i] + ")" |
||
890 | labels = Reference(ws, min_col=2 + base_period_data_ca_len * 3 + 1, |
||
891 | min_row=table_start_row_number + 1, |
||
892 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
893 | base_line_data = Reference(ws, min_col=5 + i * 3, |
||
894 | min_row=table_start_row_number, |
||
895 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
896 | reporting_line_data = Reference(ws, min_col=5 + base_period_data_ca_len * 3 + 1 + i * 3, |
||
897 | min_row=table_start_row_number, |
||
898 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
899 | line.add_data(base_line_data, titles_from_data=True) |
||
900 | line.add_data(reporting_line_data, titles_from_data=True) |
||
901 | line.set_categories(labels) |
||
902 | for j in range(len(line.series)): |
||
903 | line.series[j].marker.symbol = "auto" |
||
904 | line.series[j].smooth = True |
||
905 | line.x_axis.crosses = 'min' |
||
906 | line.height = 8.25 |
||
907 | line.width = 24 |
||
908 | chart_col = 'B' |
||
909 | chart_cell = chart_col + str(chart_start_row_number) |
||
910 | chart_start_row_number += 6 |
||
911 | ws.add_chart(line, chart_cell) |
||
912 | |||
913 | #################################################################################################################### |
||
914 | has_associated_equipment_flag = True |
||
915 | |||
916 | if "associated_equipment" not in report.keys() or \ |
||
917 | "energy_category_names" not in report['associated_equipment'].keys() or \ |
||
918 | len(report['associated_equipment']["energy_category_names"]) == 0 \ |
||
919 | or 'associated_equipment_names_array' not in report['associated_equipment'].keys() \ |
||
920 | or report['associated_equipment']['associated_equipment_names_array'] is None \ |
||
921 | or len(report['associated_equipment']['associated_equipment_names_array']) == 0 \ |
||
922 | or len(report['associated_equipment']['associated_equipment_names_array'][0]) == 0: |
||
923 | has_associated_equipment_flag = False |
||
924 | |||
925 | if has_associated_equipment_flag: |
||
926 | associated_equipment = report['associated_equipment'] |
||
927 | |||
928 | ws['B' + str(current_row_number)].font = title_font |
||
929 | ws['B' + str(current_row_number)] = name + ' ' + _('Associated Equipment Data') |
||
930 | |||
931 | current_row_number += 1 |
||
932 | |||
933 | ws.row_dimensions[current_row_number].height = 60 |
||
934 | ws['B' + str(current_row_number)].fill = table_fill |
||
935 | ws['B' + str(current_row_number)].font = name_font |
||
936 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
937 | ws['B' + str(current_row_number)].border = f_border |
||
938 | ws['B' + str(current_row_number)] = _('Associated Equipment') |
||
939 | ca_len = len(associated_equipment['energy_category_names']) |
||
940 | |||
941 | for i in range(0, ca_len): |
||
942 | row = chr(ord('C') + i) |
||
943 | ws[row + str(current_row_number)].fill = table_fill |
||
944 | ws[row + str(current_row_number)].font = name_font |
||
945 | ws[row + str(current_row_number)].alignment = c_c_alignment |
||
946 | ws[row + str(current_row_number)].border = f_border |
||
947 | ws[row + str(current_row_number)] = \ |
||
948 | associated_equipment['energy_category_names'][i] + " (" + associated_equipment['units'][i] + ")" |
||
949 | |||
950 | associated_equipment_len = len(associated_equipment['associated_equipment_names_array'][0]) |
||
951 | |||
952 | for i in range(0, associated_equipment_len): |
||
953 | current_row_number += 1 |
||
954 | row = str(current_row_number) |
||
955 | |||
956 | ws['B' + row].font = title_font |
||
957 | ws['B' + row].alignment = c_c_alignment |
||
958 | ws['B' + row] = associated_equipment['associated_equipment_names_array'][0][i] |
||
959 | ws['B' + row].border = f_border |
||
960 | |||
961 | for j in range(0, ca_len): |
||
962 | col = chr(ord('C') + j) |
||
963 | ws[col + row].font = title_font |
||
964 | ws[col + row].alignment = c_c_alignment |
||
965 | ws[col + row] = round2(associated_equipment['subtotals_array'][j][i], 2) |
||
966 | ws[col + row].border = f_border |
||
967 | #################################################################################################################### |
||
968 | |||
969 | View Code Duplication | if has_parameters_names_and_timestamps_and_values_data: |
|
0 ignored issues
–
show
|
|||
970 | |||
971 | ################################################################################################################ |
||
972 | # new worksheet |
||
973 | ################################################################################################################ |
||
974 | |||
975 | parameters_data = report['parameters'] |
||
976 | parameters_names_len = len(parameters_data['names']) |
||
977 | |||
978 | file_name = (re.sub(r'[^A-Z]', '', ws.title))+'_' |
||
979 | parameters_ws = wb.create_sheet(file_name + _('Parameters')) |
||
980 | |||
981 | parameters_timestamps_data_max_len = \ |
||
982 | get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
||
983 | |||
984 | # Row height |
||
985 | parameters_ws.row_dimensions[1].height = 102 |
||
986 | for i in range(2, 7 + 1): |
||
987 | parameters_ws.row_dimensions[i].height = 42 |
||
988 | |||
989 | for i in range(8, parameters_timestamps_data_max_len + 10): |
||
990 | parameters_ws.row_dimensions[i].height = 60 |
||
991 | |||
992 | # Col width |
||
993 | parameters_ws.column_dimensions['A'].width = 1.5 |
||
994 | |||
995 | parameters_ws.column_dimensions['B'].width = 25.0 |
||
996 | |||
997 | for i in range(3, 12+parameters_names_len*3): |
||
998 | parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
||
999 | |||
1000 | # Img |
||
1001 | img = Image("excelexporters/myems.png") |
||
1002 | parameters_ws.add_image(img, 'A1') |
||
1003 | |||
1004 | # Title |
||
1005 | parameters_ws['B3'].alignment = b_r_alignment |
||
1006 | parameters_ws['B3'] = _('Name') + ':' |
||
1007 | parameters_ws['C3'].border = b_border |
||
1008 | parameters_ws['C3'].alignment = b_c_alignment |
||
1009 | parameters_ws['C3'] = name |
||
1010 | |||
1011 | parameters_ws['D3'].alignment = b_r_alignment |
||
1012 | parameters_ws['D3'] = _('Period Type') + ':' |
||
1013 | parameters_ws['E3'].border = b_border |
||
1014 | parameters_ws['E3'].alignment = b_c_alignment |
||
1015 | parameters_ws['E3'] = period_type |
||
1016 | |||
1017 | parameters_ws['B4'].alignment = b_r_alignment |
||
1018 | parameters_ws['B4'] = _('Reporting Start Datetime') + ':' |
||
1019 | parameters_ws['C4'].border = b_border |
||
1020 | parameters_ws['C4'].alignment = b_c_alignment |
||
1021 | parameters_ws['C4'] = reporting_start_datetime_local |
||
1022 | |||
1023 | parameters_ws['D4'].alignment = b_r_alignment |
||
1024 | parameters_ws['D4'] = _('Reporting End Datetime') + ':' |
||
1025 | parameters_ws['E4'].border = b_border |
||
1026 | parameters_ws['E4'].alignment = b_c_alignment |
||
1027 | parameters_ws['E4'] = reporting_end_datetime_local |
||
1028 | |||
1029 | parameters_ws_current_row_number = 6 |
||
1030 | |||
1031 | parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
||
1032 | parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' ' + _('Parameters') |
||
1033 | |||
1034 | parameters_ws_current_row_number += 1 |
||
1035 | |||
1036 | parameters_table_start_row_number = parameters_ws_current_row_number |
||
1037 | |||
1038 | parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
||
1039 | |||
1040 | parameters_ws_current_row_number += 1 |
||
1041 | |||
1042 | table_current_col_number = 2 |
||
1043 | |||
1044 | for i in range(0, parameters_names_len): |
||
1045 | |||
1046 | if len(parameters_data['timestamps'][i]) == 0: |
||
1047 | continue |
||
1048 | |||
1049 | col = format_cell.get_column_letter(table_current_col_number) |
||
1050 | |||
1051 | parameters_ws[col + str(parameters_ws_current_row_number-1)].fill = table_fill |
||
1052 | parameters_ws[col + str(parameters_ws_current_row_number-1)].border = f_border |
||
1053 | |||
1054 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
1055 | |||
1056 | parameters_ws[col + str(parameters_ws_current_row_number-1)].fill = table_fill |
||
1057 | parameters_ws[col + str(parameters_ws_current_row_number-1)].border = f_border |
||
1058 | parameters_ws[col + str(parameters_ws_current_row_number-1)].font = name_font |
||
1059 | parameters_ws[col + str(parameters_ws_current_row_number-1)].alignment = c_c_alignment |
||
1060 | parameters_ws[col + str(parameters_ws_current_row_number-1)] = parameters_data['names'][i] |
||
1061 | |||
1062 | table_current_row_number = parameters_ws_current_row_number |
||
1063 | |||
1064 | for j, value in enumerate(list(parameters_data['timestamps'][i])): |
||
1065 | col = format_cell.get_column_letter(table_current_col_number) |
||
1066 | |||
1067 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
1068 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
1069 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
1070 | parameters_ws[col + str(table_current_row_number)] = value |
||
1071 | |||
1072 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
1073 | |||
1074 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
1075 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
1076 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
1077 | parameters_ws[col + str(table_current_row_number)] = round2(parameters_data['values'][i][j], 2) |
||
1078 | |||
1079 | table_current_row_number += 1 |
||
1080 | |||
1081 | table_current_col_number = table_current_col_number + 3 |
||
1082 | |||
1083 | ################################################################################################################ |
||
1084 | # parameters chart and parameters table |
||
1085 | ################################################################################################################ |
||
1086 | |||
1087 | ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
||
1088 | ws['B' + str(current_sheet_parameters_row_number)] = name + ' ' + _('Parameters') |
||
1089 | |||
1090 | current_sheet_parameters_row_number += 1 |
||
1091 | |||
1092 | chart_start_row_number = current_sheet_parameters_row_number |
||
1093 | |||
1094 | col_index = 0 |
||
1095 | |||
1096 | for i in range(0, parameters_names_len): |
||
1097 | |||
1098 | if len(parameters_data['timestamps'][i]) == 0: |
||
1099 | continue |
||
1100 | |||
1101 | line = LineChart() |
||
1102 | data_col = 3+col_index*3 |
||
1103 | labels_col = 2+col_index*3 |
||
1104 | col_index += 1 |
||
1105 | line.title = _('Parameters') + ' - ' + \ |
||
1106 | parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
||
1107 | labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
||
1108 | max_row=(len(parameters_data['timestamps'][i])+parameters_table_start_row_number)) |
||
1109 | line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
||
1110 | max_row=(len(parameters_data['timestamps'][i])+parameters_table_start_row_number)) |
||
1111 | line.add_data(line_data, titles_from_data=True) |
||
1112 | line.set_categories(labels) |
||
1113 | line_data = line.series[0] |
||
1114 | line_data.marker.symbol = "auto" |
||
1115 | line_data.smooth = True |
||
1116 | line.x_axis.crosses = 'min' |
||
1117 | line.height = 8.25 |
||
1118 | line.width = 24 |
||
1119 | chart_col = 'B' |
||
1120 | chart_cell = chart_col + str(chart_start_row_number) |
||
1121 | chart_start_row_number += 6 |
||
1122 | ws.add_chart(line, chart_cell) |
||
1123 | |||
1124 | current_sheet_parameters_row_number = chart_start_row_number |
||
1125 | |||
1126 | current_sheet_parameters_row_number += 1 |
||
1127 | |||
1128 | filename = str(uuid.uuid4()) + '.xlsx' |
||
1129 | wb.save(filename) |
||
1130 | |||
1131 | return filename |
||
1132 | |||
1133 | |||
1134 | def get_parameters_timestamps_lists_max_len(parameters_timestamps_lists): |
||
1135 | max_len = 0 |
||
1136 | for i, value in enumerate(list(parameters_timestamps_lists)): |
||
1137 | if len(value) > max_len: |
||
1138 | max_len = len(value) |
||
1139 | |||
1140 | return max_len |
||
1141 | |||
1142 | |||
1143 | def timestamps_data_all_equal_0(lists): |
||
1144 | for i, value in enumerate(list(lists)): |
||
1145 | if len(value) > 0: |
||
1146 | return False |
||
1147 | |||
1148 | return True |
||
1149 | |||
1150 | |||
1151 | def timestamps_data_not_equal_0(lists): |
||
1152 | number = 0 |
||
1153 | for i, value in enumerate(list(lists)): |
||
1154 | if len(value) > 0: |
||
1155 | number += 1 |
||
1156 | return number |
||
1157 | |||
1158 | |||
1159 | View Code Duplication | def is_base_period_timestamp_exists(base_period_data): |
|
0 ignored issues
–
show
|
|||
1160 | timestamps = base_period_data['timestamps'] |
||
1161 | |||
1162 | if len(timestamps) == 0: |
||
1163 | return False |
||
1164 | |||
1165 | for timestamp in timestamps: |
||
1166 | if len(timestamp) > 0: |
||
1167 | return True |
||
1168 | |||
1169 | return False |
||
1170 |