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