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