Total Complexity | 72 |
Total Lines | 745 |
Duplicated Lines | 93.42 % |
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.tenantcarbon 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 uuid |
||
3 | import os |
||
4 | import re |
||
5 | from decimal import Decimal |
||
6 | from openpyxl.chart import PieChart, LineChart, Reference |
||
7 | from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
||
8 | from openpyxl.drawing.image import Image |
||
9 | from openpyxl import Workbook |
||
10 | from openpyxl.chart.label import DataLabelList |
||
11 | import openpyxl.utils.cell as format_cell |
||
12 | |||
13 | |||
14 | ######################################################################################################################## |
||
15 | # PROCEDURES |
||
16 | # Step 1: Validate the report data |
||
17 | # Step 2: Generate excel file |
||
18 | # Step 3: Encode the excel file bytes to Base64 |
||
19 | ######################################################################################################################## |
||
20 | |||
21 | |||
22 | View Code Duplication | def export(report, |
|
|
|||
23 | name, |
||
24 | reporting_start_datetime_local, |
||
25 | reporting_end_datetime_local, |
||
26 | period_type): |
||
27 | #################################################################################################################### |
||
28 | # Step 1: Validate the report data |
||
29 | #################################################################################################################### |
||
30 | if report is None: |
||
31 | return None |
||
32 | |||
33 | #################################################################################################################### |
||
34 | # Step 2: Generate excel file from the report data |
||
35 | #################################################################################################################### |
||
36 | filename = generate_excel(report, |
||
37 | name, |
||
38 | reporting_start_datetime_local, |
||
39 | reporting_end_datetime_local, |
||
40 | period_type) |
||
41 | #################################################################################################################### |
||
42 | # Step 3: Encode the excel file to Base64 |
||
43 | #################################################################################################################### |
||
44 | binary_file_data = b'' |
||
45 | try: |
||
46 | with open(filename, 'rb') as binary_file: |
||
47 | binary_file_data = binary_file.read() |
||
48 | except IOError as ex: |
||
49 | pass |
||
50 | |||
51 | # Base64 encode the bytes |
||
52 | base64_encoded_data = base64.b64encode(binary_file_data) |
||
53 | # get the Base64 encoded data using human-readable characters. |
||
54 | base64_message = base64_encoded_data.decode('utf-8') |
||
55 | # delete the file from server |
||
56 | try: |
||
57 | os.remove(filename) |
||
58 | except NotImplementedError as ex: |
||
59 | pass |
||
60 | return base64_message |
||
61 | |||
62 | |||
63 | View Code Duplication | def generate_excel(report, |
|
64 | name, |
||
65 | reporting_start_datetime_local, |
||
66 | reporting_end_datetime_local, |
||
67 | period_type): |
||
68 | |||
69 | wb = Workbook() |
||
70 | ws = wb.active |
||
71 | ws.title = "TenantCarbon" |
||
72 | |||
73 | # Row height |
||
74 | ws.row_dimensions[1].height = 102 |
||
75 | for i in range(2, 2000 + 1): |
||
76 | ws.row_dimensions[i].height = 42 |
||
77 | |||
78 | # Col width |
||
79 | ws.column_dimensions['A'].width = 1.5 |
||
80 | |||
81 | ws.column_dimensions['B'].width = 25.0 |
||
82 | |||
83 | for i in range(ord('C'), ord('L')): |
||
84 | ws.column_dimensions[chr(i)].width = 15.0 |
||
85 | |||
86 | # Font |
||
87 | name_font = Font(name='Arial', size=15, bold=True) |
||
88 | title_font = Font(name='Arial', size=15, bold=True) |
||
89 | |||
90 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
||
91 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
||
92 | right=Side(border_style='medium', color='00000000'), |
||
93 | bottom=Side(border_style='medium', color='00000000'), |
||
94 | top=Side(border_style='medium', color='00000000') |
||
95 | ) |
||
96 | b_border = Border( |
||
97 | bottom=Side(border_style='medium', color='00000000'), |
||
98 | ) |
||
99 | |||
100 | b_c_alignment = Alignment(vertical='bottom', |
||
101 | horizontal='center', |
||
102 | text_rotation=0, |
||
103 | wrap_text=True, |
||
104 | shrink_to_fit=False, |
||
105 | indent=0) |
||
106 | c_c_alignment = Alignment(vertical='center', |
||
107 | horizontal='center', |
||
108 | text_rotation=0, |
||
109 | wrap_text=True, |
||
110 | shrink_to_fit=False, |
||
111 | indent=0) |
||
112 | b_r_alignment = Alignment(vertical='bottom', |
||
113 | horizontal='right', |
||
114 | text_rotation=0, |
||
115 | wrap_text=True, |
||
116 | shrink_to_fit=False, |
||
117 | indent=0) |
||
118 | |||
119 | # Img |
||
120 | img = Image("excelexporters/myems.png") |
||
121 | ws.add_image(img, 'A1') |
||
122 | |||
123 | # Title |
||
124 | ws['B3'].alignment = b_r_alignment |
||
125 | ws['B3'] = 'Name:' |
||
126 | ws['C3'].border = b_border |
||
127 | ws['C3'].alignment = b_c_alignment |
||
128 | ws['C3'] = name |
||
129 | |||
130 | ws['D3'].alignment = b_r_alignment |
||
131 | ws['D3'] = 'Period:' |
||
132 | ws['E3'].border = b_border |
||
133 | ws['E3'].alignment = b_c_alignment |
||
134 | ws['E3'] = period_type |
||
135 | |||
136 | ws['B4'].alignment = b_r_alignment |
||
137 | ws['B4'] = 'Reporting Start Datetime:' |
||
138 | ws['C4'].border = b_border |
||
139 | ws['C4'].alignment = b_c_alignment |
||
140 | ws['C4'] = reporting_start_datetime_local |
||
141 | |||
142 | ws['D4'].alignment = b_r_alignment |
||
143 | ws['D4'] = 'Reporting End Datetime:' |
||
144 | ws['E4'].border = b_border |
||
145 | ws['E4'].alignment = b_c_alignment |
||
146 | ws['E4'] = reporting_end_datetime_local |
||
147 | |||
148 | if "reporting_period" not in report.keys() or \ |
||
149 | "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
||
150 | filename = str(uuid.uuid4()) + '.xlsx' |
||
151 | wb.save(filename) |
||
152 | |||
153 | return filename |
||
154 | |||
155 | #################################################################################################################### |
||
156 | |||
157 | reporting_period_data = report['reporting_period'] |
||
158 | |||
159 | has_energy_data_flag = True |
||
160 | if "names" not in reporting_period_data.keys() or \ |
||
161 | reporting_period_data['names'] is None or \ |
||
162 | len(reporting_period_data['names']) == 0: |
||
163 | has_energy_data_flag = False |
||
164 | |||
165 | if has_energy_data_flag: |
||
166 | ws['B6'].font = title_font |
||
167 | ws['B6'] = name + ' ' + 'Reporting Period Carbon Dioxide Emissions' |
||
168 | |||
169 | category = reporting_period_data['names'] |
||
170 | ca_len = len(category) |
||
171 | |||
172 | ws.row_dimensions[7].height = 60 |
||
173 | ws['B7'].fill = table_fill |
||
174 | ws['B7'].border = f_border |
||
175 | |||
176 | ws['B8'].font = title_font |
||
177 | ws['B8'].alignment = c_c_alignment |
||
178 | ws['B8'] = 'Carbon Dioxide Emissions' |
||
179 | ws['B8'].border = f_border |
||
180 | |||
181 | ws['B9'].font = title_font |
||
182 | ws['B9'].alignment = c_c_alignment |
||
183 | ws['B9'] = 'Per Unit Area' |
||
184 | ws['B9'].border = f_border |
||
185 | |||
186 | ws['B10'].font = title_font |
||
187 | ws['B10'].alignment = c_c_alignment |
||
188 | ws['B10'] = 'Increment Rate' |
||
189 | ws['B10'].border = f_border |
||
190 | |||
191 | col = 'B' |
||
192 | |||
193 | for i in range(0, ca_len): |
||
194 | col = chr(ord('C') + i) |
||
195 | ws[col + '7'].fill = table_fill |
||
196 | ws[col + '7'].font = name_font |
||
197 | ws[col + '7'].alignment = c_c_alignment |
||
198 | ws[col + '7'] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
199 | ws[col + '7'].border = f_border |
||
200 | |||
201 | ws[col + '8'].font = name_font |
||
202 | ws[col + '8'].alignment = c_c_alignment |
||
203 | ws[col + '8'] = round(reporting_period_data['subtotals'][i], 2) |
||
204 | ws[col + '8'].border = f_border |
||
205 | |||
206 | ws[col + '9'].font = name_font |
||
207 | ws[col + '9'].alignment = c_c_alignment |
||
208 | ws[col + '9'] = round(reporting_period_data['subtotals_per_unit_area'][i], 2) |
||
209 | ws[col + '9'].border = f_border |
||
210 | |||
211 | ws[col + '10'].font = name_font |
||
212 | ws[col + '10'].alignment = c_c_alignment |
||
213 | ws[col + '10'] = str(round(reporting_period_data['increment_rates'][i] * 100, 2)) + "%" \ |
||
214 | if reporting_period_data['increment_rates'][i] is not None else "-" |
||
215 | ws[col + '10'].border = f_border |
||
216 | |||
217 | end_col = chr(ord(col)+1) |
||
218 | ws[end_col + '7'].fill = table_fill |
||
219 | ws[end_col + '7'].font = name_font |
||
220 | ws[end_col + '7'].alignment = c_c_alignment |
||
221 | ws[end_col + '7'] = "Total (" + reporting_period_data['total_unit'] + ")" |
||
222 | ws[end_col + '7'].border = f_border |
||
223 | |||
224 | ws[end_col + '8'].font = name_font |
||
225 | ws[end_col + '8'].alignment = c_c_alignment |
||
226 | ws[end_col + '8'] = round(reporting_period_data['total'], 2) |
||
227 | ws[end_col + '8'].border = f_border |
||
228 | |||
229 | ws[end_col + '9'].font = name_font |
||
230 | ws[end_col + '9'].alignment = c_c_alignment |
||
231 | ws[end_col + '9'] = round(reporting_period_data['total_per_unit_area'], 2) |
||
232 | ws[end_col + '9'].border = f_border |
||
233 | |||
234 | ws[end_col + '10'].font = name_font |
||
235 | ws[end_col + '10'].alignment = c_c_alignment |
||
236 | ws[end_col + '10'] = str(round(reporting_period_data['total_increment_rate'] * 100, 2)) + "%" \ |
||
237 | if reporting_period_data['total_increment_rate'] is not None else "-" |
||
238 | ws[end_col + '10'].border = f_border |
||
239 | |||
240 | else: |
||
241 | for i in range(6, 10 + 1): |
||
242 | ws.row_dimensions[i].height = 0.1 |
||
243 | |||
244 | #################################################################################################################### |
||
245 | |||
246 | has_ele_peak_flag = True |
||
247 | if "toppeaks" not in reporting_period_data.keys() or \ |
||
248 | reporting_period_data['toppeaks'] is None or \ |
||
249 | len(reporting_period_data['toppeaks']) == 0: |
||
250 | has_ele_peak_flag = False |
||
251 | |||
252 | if has_ele_peak_flag: |
||
253 | ws['B12'].font = title_font |
||
254 | ws['B12'] = name + ' ' + 'Electricity Carbon Dioxide Emissions by Time-Of-Use' |
||
255 | |||
256 | ws['B13'].fill = table_fill |
||
257 | ws['B13'].font = name_font |
||
258 | ws['B13'].alignment = c_c_alignment |
||
259 | ws['B13'].border = f_border |
||
260 | |||
261 | ws['C13'].fill = table_fill |
||
262 | ws['C13'].font = name_font |
||
263 | ws['C13'].alignment = c_c_alignment |
||
264 | ws['C13'].border = f_border |
||
265 | ws['C13'] = 'Electricity Carbon Dioxide Emissions by Time-Of-Use' |
||
266 | |||
267 | ws['B14'].font = title_font |
||
268 | ws['B14'].alignment = c_c_alignment |
||
269 | ws['B14'] = 'TopPeak' |
||
270 | ws['B14'].border = f_border |
||
271 | |||
272 | ws['C14'].font = title_font |
||
273 | ws['C14'].alignment = c_c_alignment |
||
274 | ws['C14'].border = f_border |
||
275 | ws['C14'] = round(reporting_period_data['toppeaks'][0], 2) |
||
276 | |||
277 | ws['B15'].font = title_font |
||
278 | ws['B15'].alignment = c_c_alignment |
||
279 | ws['B15'] = 'OnPeak' |
||
280 | ws['B15'].border = f_border |
||
281 | |||
282 | ws['C15'].font = title_font |
||
283 | ws['C15'].alignment = c_c_alignment |
||
284 | ws['C15'].border = f_border |
||
285 | ws['C15'] = round(reporting_period_data['onpeaks'][0], 2) |
||
286 | |||
287 | ws['B16'].font = title_font |
||
288 | ws['B16'].alignment = c_c_alignment |
||
289 | ws['B16'] = 'MidPeak' |
||
290 | ws['B16'].border = f_border |
||
291 | |||
292 | ws['C16'].font = title_font |
||
293 | ws['C16'].alignment = c_c_alignment |
||
294 | ws['C16'].border = f_border |
||
295 | ws['C16'] = round(reporting_period_data['midpeaks'][0], 2) |
||
296 | |||
297 | ws['B17'].font = title_font |
||
298 | ws['B17'].alignment = c_c_alignment |
||
299 | ws['B17'] = 'OffPeak' |
||
300 | ws['B17'].border = f_border |
||
301 | |||
302 | ws['C17'].font = title_font |
||
303 | ws['C17'].alignment = c_c_alignment |
||
304 | ws['C17'].border = f_border |
||
305 | ws['C17'] = round(reporting_period_data['offpeaks'][0], 2) |
||
306 | |||
307 | pie = PieChart() |
||
308 | pie.title = name + ' ' + 'Electricity Carbon Dioxide Emissions by Time-Of-Use' |
||
309 | labels = Reference(ws, min_col=2, min_row=14, max_row=17) |
||
310 | pie_data = Reference(ws, min_col=3, min_row=13, max_row=17) |
||
311 | pie.add_data(pie_data, titles_from_data=True) |
||
312 | pie.set_categories(labels) |
||
313 | pie.height = 6.6 |
||
314 | pie.width = 9 |
||
315 | s1 = pie.series[0] |
||
316 | s1.dLbls = DataLabelList() |
||
317 | s1.dLbls.showCatName = False |
||
318 | s1.dLbls.showVal = True |
||
319 | s1.dLbls.showPercent = True |
||
320 | ws.add_chart(pie, "D13") |
||
321 | |||
322 | else: |
||
323 | for i in range(12, 18 + 1): |
||
324 | ws.row_dimensions[i].height = 0.1 |
||
325 | |||
326 | #################################################################################################################### |
||
327 | |||
328 | current_row_number = 19 |
||
329 | |||
330 | has_subtotals_data_flag = True |
||
331 | if "subtotals" not in reporting_period_data.keys() or \ |
||
332 | reporting_period_data['subtotals'] is None or \ |
||
333 | len(reporting_period_data['subtotals']) == 0: |
||
334 | has_subtotals_data_flag = False |
||
335 | |||
336 | if has_subtotals_data_flag: |
||
337 | ws['B' + str(current_row_number)].font = title_font |
||
338 | ws['B' + str(current_row_number)] = name + ' ' + 'Carbon Dioxide Emissions Proportion' |
||
339 | |||
340 | current_row_number += 1 |
||
341 | |||
342 | table_start_row_number = current_row_number |
||
343 | |||
344 | ws['B' + str(current_row_number)].fill = table_fill |
||
345 | ws['B' + str(current_row_number)].font = name_font |
||
346 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
347 | ws['B' + str(current_row_number)].border = f_border |
||
348 | |||
349 | ws['C' + str(current_row_number)].fill = table_fill |
||
350 | ws['C' + str(current_row_number)].font = name_font |
||
351 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
352 | ws['C' + str(current_row_number)].border = f_border |
||
353 | ws['C' + str(current_row_number)] = 'Carbon Dioxide Emissions Proportion' |
||
354 | |||
355 | current_row_number += 1 |
||
356 | |||
357 | ca_len = len(reporting_period_data['names']) |
||
358 | |||
359 | for i in range(0, ca_len): |
||
360 | ws['B' + str(current_row_number)].font = title_font |
||
361 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
362 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
||
363 | ws['B' + str(current_row_number)].border = f_border |
||
364 | |||
365 | ws['C' + str(current_row_number)].font = title_font |
||
366 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
367 | ws['C' + str(current_row_number)].border = f_border |
||
368 | ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) |
||
369 | |||
370 | current_row_number += 1 |
||
371 | |||
372 | table_end_row_number = current_row_number - 1 |
||
373 | |||
374 | pie = PieChart() |
||
375 | pie.title = name + ' ' + 'Carbon Dioxide Emissions Proportion' |
||
376 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
377 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
||
378 | pie.add_data(pie_data, titles_from_data=True) |
||
379 | pie.set_categories(labels) |
||
380 | pie.height = 6.6 |
||
381 | pie.width = 9 |
||
382 | s1 = pie.series[0] |
||
383 | s1.dLbls = DataLabelList() |
||
384 | s1.dLbls.showCatName = False |
||
385 | s1.dLbls.showVal = True |
||
386 | s1.dLbls.showPercent = True |
||
387 | table_cell = 'D' + str(table_start_row_number) |
||
388 | ws.add_chart(pie, table_cell) |
||
389 | |||
390 | if ca_len < 4: |
||
391 | current_row_number = current_row_number - ca_len + 4 |
||
392 | |||
393 | else: |
||
394 | for i in range(21, 29 + 1): |
||
395 | current_row_number = 30 |
||
396 | ws.row_dimensions[i].height = 0.1 |
||
397 | |||
398 | #################################################################################################################### |
||
399 | |||
400 | current_row_number += 1 |
||
401 | |||
402 | has_detail_data_flag = True |
||
403 | |||
404 | table_start_draw_flag = current_row_number + 1 |
||
405 | |||
406 | if "timestamps" not in reporting_period_data.keys() or \ |
||
407 | reporting_period_data['timestamps'] is None or \ |
||
408 | len(reporting_period_data['timestamps']) == 0: |
||
409 | has_detail_data_flag = False |
||
410 | |||
411 | if has_detail_data_flag: |
||
412 | reporting_period_data = report['reporting_period'] |
||
413 | times = reporting_period_data['timestamps'] |
||
414 | ca_len = len(report['reporting_period']['names']) |
||
415 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
||
416 | ws['B' + str(current_row_number)].font = title_font |
||
417 | ws['B' + str(current_row_number)] = name + ' ' + 'Detailed Data' |
||
418 | |||
419 | table_start_row_number = (current_row_number + 1) + ca_len * 6 + real_timestamps_len * 7 |
||
420 | current_row_number = table_start_row_number |
||
421 | |||
422 | time = times[0] |
||
423 | has_data = False |
||
424 | |||
425 | if len(time) > 0: |
||
426 | has_data = True |
||
427 | |||
428 | if has_data: |
||
429 | |||
430 | ws.row_dimensions[current_row_number].height = 60 |
||
431 | ws['B' + str(current_row_number)].fill = table_fill |
||
432 | ws['B' + str(current_row_number)].border = f_border |
||
433 | ws['B' + str(current_row_number)].font = title_font |
||
434 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
435 | ws['B' + str(current_row_number)] = 'Datetime' |
||
436 | |||
437 | col = 'B' |
||
438 | |||
439 | for i in range(0, ca_len): |
||
440 | col = chr(ord('C') + i) |
||
441 | |||
442 | ws[col + str(current_row_number)].fill = table_fill |
||
443 | ws[col + str(current_row_number)].font = title_font |
||
444 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
445 | ws[col + str(current_row_number)] = reporting_period_data['names'][i] + \ |
||
446 | " (" + reporting_period_data['units'][i] + ")" |
||
447 | ws[col + str(current_row_number)].border = f_border |
||
448 | |||
449 | end_col = chr(ord(col) + 1) |
||
450 | |||
451 | ws[end_col + str(current_row_number)].fill = table_fill |
||
452 | ws[end_col + str(current_row_number)].font = title_font |
||
453 | ws[end_col + str(current_row_number)].alignment = c_c_alignment |
||
454 | ws[end_col + str(current_row_number)] = "Total (" + reporting_period_data['total_unit'] + ")" |
||
455 | ws[end_col + str(current_row_number)].border = f_border |
||
456 | |||
457 | current_row_number += 1 |
||
458 | |||
459 | for i in range(0, len(time)): |
||
460 | ws['B' + str(current_row_number)].font = title_font |
||
461 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
462 | ws['B' + str(current_row_number)] = time[i] |
||
463 | ws['B' + str(current_row_number)].border = f_border |
||
464 | |||
465 | col = 'B' |
||
466 | |||
467 | periodic_sum = Decimal(0.0) |
||
468 | |||
469 | for j in range(0, ca_len): |
||
470 | col = chr(ord('C') + j) |
||
471 | |||
472 | ws[col + str(current_row_number)].font = title_font |
||
473 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
474 | value = round(reporting_period_data['values'][j][i], 2) |
||
475 | periodic_sum += value |
||
476 | ws[col + str(current_row_number)] = value |
||
477 | ws[col + str(current_row_number)].border = f_border |
||
478 | |||
479 | end_col = chr(ord(col) + 1) |
||
480 | ws[end_col + str(current_row_number)].font = title_font |
||
481 | ws[end_col + str(current_row_number)].alignment = c_c_alignment |
||
482 | ws[end_col + str(current_row_number)] = round(periodic_sum, 2) |
||
483 | ws[end_col + str(current_row_number)].border = f_border |
||
484 | |||
485 | current_row_number += 1 |
||
486 | |||
487 | table_end_row_number = current_row_number - 1 |
||
488 | |||
489 | ws['B' + str(current_row_number)].font = title_font |
||
490 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
491 | ws['B' + str(current_row_number)] = 'Subtotal' |
||
492 | ws['B' + str(current_row_number)].border = f_border |
||
493 | |||
494 | col = 'B' |
||
495 | |||
496 | for i in range(0, ca_len): |
||
497 | col = chr(ord('C') + i) |
||
498 | ws[col + str(current_row_number)].font = title_font |
||
499 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
500 | ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) |
||
501 | ws[col + str(current_row_number)].border = f_border |
||
502 | |||
503 | # line |
||
504 | line = LineChart() |
||
505 | line.title = 'Reporting Period Carbon Dioxide Emissions - ' + \ |
||
506 | ws.cell(column=3+i, row=table_start_row_number).value |
||
507 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
508 | line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number) |
||
509 | line.add_data(line_data, titles_from_data=True) |
||
510 | line.set_categories(labels) |
||
511 | line_data = line.series[0] |
||
512 | line_data.marker.symbol = "circle" |
||
513 | line_data.smooth = True |
||
514 | line.x_axis.crosses = 'min' |
||
515 | line.height = 8.25 |
||
516 | line.width = 24 |
||
517 | line.dLbls = DataLabelList() |
||
518 | line.dLbls.dLblPos = 't' |
||
519 | line.dLbls.showVal = True |
||
520 | line.dLbls.showPercent = False |
||
521 | chart_col = 'B' |
||
522 | chart_cell = chart_col + str(table_start_draw_flag + 6 * i) |
||
523 | ws.add_chart(line, chart_cell) |
||
524 | |||
525 | end_col = chr(ord(col) + 1) |
||
526 | ws[end_col + str(current_row_number)].font = title_font |
||
527 | ws[end_col + str(current_row_number)].alignment = c_c_alignment |
||
528 | ws[end_col + str(current_row_number)] = round(reporting_period_data['total'], 2) |
||
529 | ws[end_col + str(current_row_number)].border = f_border |
||
530 | |||
531 | current_row_number += 1 |
||
532 | |||
533 | else: |
||
534 | for i in range(30, 69 + 1): |
||
535 | current_row_number = 70 |
||
536 | ws.row_dimensions[i].height = 0.1 |
||
537 | #################################################################################################################### |
||
538 | current_sheet_parameters_row_number = table_start_draw_flag + ca_len * 6 + 1 |
||
539 | has_parameters_names_and_timestamps_and_values_data = True |
||
540 | if 'parameters' not in report.keys() or \ |
||
541 | report['parameters'] is None or \ |
||
542 | 'names' not in report['parameters'].keys() or \ |
||
543 | report['parameters']['names'] is None or \ |
||
544 | len(report['parameters']['names']) == 0 or \ |
||
545 | 'timestamps' not in report['parameters'].keys() or \ |
||
546 | report['parameters']['timestamps'] is None or \ |
||
547 | len(report['parameters']['timestamps']) == 0 or \ |
||
548 | 'values' not in report['parameters'].keys() or \ |
||
549 | report['parameters']['values'] is None or \ |
||
550 | len(report['parameters']['values']) == 0 or \ |
||
551 | timestamps_data_all_equal_0(report['parameters']['timestamps']): |
||
552 | has_parameters_names_and_timestamps_and_values_data = False |
||
553 | if has_parameters_names_and_timestamps_and_values_data: |
||
554 | |||
555 | ################################################################################################################ |
||
556 | # new worksheet |
||
557 | ################################################################################################################ |
||
558 | |||
559 | parameters_data = report['parameters'] |
||
560 | parameters_names_len = len(parameters_data['names']) |
||
561 | |||
562 | file_name = (re.sub(r'[^A-Z]', '', ws.title)) + '_' |
||
563 | parameters_ws = wb.create_sheet(file_name + 'Parameters') |
||
564 | |||
565 | parameters_timestamps_data_max_len = \ |
||
566 | get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
||
567 | |||
568 | # Row height |
||
569 | parameters_ws.row_dimensions[1].height = 102 |
||
570 | for i in range(2, 7 + 1): |
||
571 | parameters_ws.row_dimensions[i].height = 42 |
||
572 | |||
573 | for i in range(8, parameters_timestamps_data_max_len + 10): |
||
574 | parameters_ws.row_dimensions[i].height = 60 |
||
575 | |||
576 | # Col width |
||
577 | parameters_ws.column_dimensions['A'].width = 1.5 |
||
578 | |||
579 | parameters_ws.column_dimensions['B'].width = 25.0 |
||
580 | |||
581 | for i in range(3, 12 + parameters_names_len * 3): |
||
582 | parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
||
583 | |||
584 | # Img |
||
585 | img = Image("excelexporters/myems.png") |
||
586 | parameters_ws.add_image(img, 'A1') |
||
587 | |||
588 | # Title |
||
589 | parameters_ws['B3'].alignment = b_r_alignment |
||
590 | parameters_ws['B3'] = 'Name:' |
||
591 | parameters_ws['C3'].border = b_border |
||
592 | parameters_ws['C3'].alignment = b_c_alignment |
||
593 | parameters_ws['C3'] = name |
||
594 | |||
595 | parameters_ws['D3'].alignment = b_r_alignment |
||
596 | parameters_ws['D3'] = 'Period:' |
||
597 | parameters_ws['E3'].border = b_border |
||
598 | parameters_ws['E3'].alignment = b_c_alignment |
||
599 | parameters_ws['E3'] = period_type |
||
600 | |||
601 | parameters_ws['B4'].alignment = b_r_alignment |
||
602 | parameters_ws['B4'] = 'Reporting Start Datetime:' |
||
603 | parameters_ws['C4'].border = b_border |
||
604 | parameters_ws['C4'].alignment = b_c_alignment |
||
605 | parameters_ws['C4'] = reporting_start_datetime_local |
||
606 | |||
607 | parameters_ws['D4'].alignment = b_r_alignment |
||
608 | parameters_ws['D4'] = 'Reporting End Datetime:' |
||
609 | parameters_ws['E4'].border = b_border |
||
610 | parameters_ws['E4'].alignment = b_c_alignment |
||
611 | parameters_ws['E4'] = reporting_end_datetime_local |
||
612 | |||
613 | parameters_ws_current_row_number = 6 |
||
614 | |||
615 | parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
||
616 | parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' ' + 'Parameters' |
||
617 | |||
618 | parameters_ws_current_row_number += 1 |
||
619 | |||
620 | parameters_table_start_row_number = parameters_ws_current_row_number |
||
621 | |||
622 | parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
||
623 | |||
624 | parameters_ws_current_row_number += 1 |
||
625 | |||
626 | table_current_col_number = 2 |
||
627 | |||
628 | for i in range(0, parameters_names_len): |
||
629 | |||
630 | if len(parameters_data['timestamps'][i]) == 0: |
||
631 | continue |
||
632 | |||
633 | col = format_cell.get_column_letter(table_current_col_number) |
||
634 | |||
635 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
636 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
637 | |||
638 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
639 | |||
640 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
641 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
642 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = name_font |
||
643 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = c_c_alignment |
||
644 | parameters_ws[col + str(parameters_ws_current_row_number - 1)] = parameters_data['names'][i] |
||
645 | |||
646 | table_current_row_number = parameters_ws_current_row_number |
||
647 | |||
648 | for j, value in enumerate(list(parameters_data['timestamps'][i])): |
||
649 | col = format_cell.get_column_letter(table_current_col_number) |
||
650 | |||
651 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
652 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
653 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
654 | parameters_ws[col + str(table_current_row_number)] = value |
||
655 | |||
656 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
657 | |||
658 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
659 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
660 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
661 | parameters_ws[col + str(table_current_row_number)] = round(parameters_data['values'][i][j], 2) |
||
662 | |||
663 | table_current_row_number += 1 |
||
664 | |||
665 | table_current_col_number = table_current_col_number + 3 |
||
666 | |||
667 | ################################################################################################################ |
||
668 | # parameters chart and parameters table |
||
669 | ################################################################################################################ |
||
670 | |||
671 | ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
||
672 | ws['B' + str(current_sheet_parameters_row_number)] = name + ' ' + 'Parameters' |
||
673 | |||
674 | current_sheet_parameters_row_number += 1 |
||
675 | |||
676 | chart_start_row_number = current_sheet_parameters_row_number |
||
677 | |||
678 | col_index = 0 |
||
679 | |||
680 | for i in range(0, parameters_names_len): |
||
681 | |||
682 | if len(parameters_data['timestamps'][i]) == 0: |
||
683 | continue |
||
684 | |||
685 | line = LineChart() |
||
686 | data_col = 3 + col_index * 3 |
||
687 | labels_col = 2 + col_index * 3 |
||
688 | col_index += 1 |
||
689 | line.title = 'Parameters - ' + \ |
||
690 | parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
||
691 | labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
||
692 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
693 | line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
||
694 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
695 | line.add_data(line_data, titles_from_data=True) |
||
696 | line.set_categories(labels) |
||
697 | line_data = line.series[0] |
||
698 | line_data.marker.symbol = "circle" |
||
699 | line_data.smooth = True |
||
700 | line.x_axis.crosses = 'min' |
||
701 | line.height = 8.25 |
||
702 | line.width = 24 |
||
703 | line.dLbls = DataLabelList() |
||
704 | line.dLbls.dLblPos = 't' |
||
705 | line.dLbls.showVal = False |
||
706 | line.dLbls.showPercent = False |
||
707 | chart_col = 'B' |
||
708 | chart_cell = chart_col + str(chart_start_row_number) |
||
709 | chart_start_row_number += 6 |
||
710 | ws.add_chart(line, chart_cell) |
||
711 | |||
712 | current_sheet_parameters_row_number = chart_start_row_number |
||
713 | |||
714 | current_sheet_parameters_row_number += 1 |
||
715 | #################################################################################################################### |
||
716 | filename = str(uuid.uuid4()) + '.xlsx' |
||
717 | wb.save(filename) |
||
718 | |||
719 | return filename |
||
720 | |||
721 | |||
722 | def timestamps_data_all_equal_0(lists): |
||
723 | for i, value in enumerate(list(lists)): |
||
724 | if len(value) > 0: |
||
725 | return False |
||
726 | |||
727 | return True |
||
728 | |||
729 | |||
730 | def get_parameters_timestamps_lists_max_len(parameters_timestamps_lists): |
||
731 | max_len = 0 |
||
732 | for i, value in enumerate(list(parameters_timestamps_lists)): |
||
733 | if len(value) > max_len: |
||
734 | max_len = len(value) |
||
735 | |||
736 | return max_len |
||
737 | |||
738 | |||
739 | def timestamps_data_not_equal_0(lists): |
||
740 | number = 0 |
||
741 | for i, value in enumerate(list(lists)): |
||
742 | if len(value) > 0: |
||
743 | number += 1 |
||
744 | return number |
||
745 |