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