Conditions | 101 |
Total Lines | 1019 |
Code Lines | 797 |
Lines | 1019 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like excelexporters.combinedequipmentcarbon.generate_excel() 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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | import base64 |
||
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 = "CombinedEquipmentCarbon" |
||
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 \ |
||
188 | 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 | |||
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, 10 + 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[7].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'] = _('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 | else: |
||
267 | electricity_index = -1 |
||
268 | for i in range(len(reporting_period_data['energy_category_ids'])): |
||
269 | if reporting_period_data['energy_category_ids'][i] == 1: |
||
270 | electricity_index = i |
||
271 | break |
||
272 | |||
273 | ws['B12'].font = title_font |
||
274 | ws['B12'] = name + _('Electricity Carbon Dioxide Emissions by Time-Of-Use') |
||
275 | |||
276 | ws['B13'].fill = table_fill |
||
277 | ws['B13'].font = name_font |
||
278 | ws['B13'].alignment = c_c_alignment |
||
279 | ws['B13'].border = f_border |
||
280 | |||
281 | ws['C13'].fill = table_fill |
||
282 | ws['C13'].font = name_font |
||
283 | ws['C13'].alignment = c_c_alignment |
||
284 | ws['C13'].border = f_border |
||
285 | ws['C13'] = _('Electricity Carbon Dioxide Emissions by Time-Of-Use') |
||
286 | |||
287 | ws['D13'].fill = table_fill |
||
288 | ws['D13'].font = name_font |
||
289 | ws['D13'].alignment = c_c_alignment |
||
290 | ws['D13'].border = f_border |
||
291 | ws['D13'] = _('Electricity Carbon Dioxide Emissions Proportion by Time-Of-Use') |
||
292 | |||
293 | carbonsum = None |
||
294 | |||
295 | if electricity_index >= 0: |
||
296 | carbonsum = round(reporting_period_data['toppeaks'][electricity_index], 2) + \ |
||
297 | round(reporting_period_data['onpeaks'][electricity_index], 2) + \ |
||
298 | round(reporting_period_data['midpeaks'][electricity_index], 2) + \ |
||
299 | round(reporting_period_data['offpeaks'][electricity_index], 2) |
||
300 | |||
301 | ws['B14'].font = title_font |
||
302 | ws['B14'].alignment = c_c_alignment |
||
303 | ws['B14'] = _('TopPeak') |
||
304 | ws['B14'].border = f_border |
||
305 | |||
306 | ws['C14'].font = title_font |
||
307 | ws['C14'].alignment = c_c_alignment |
||
308 | ws['C14'].border = f_border |
||
309 | ws['C14'] = round(reporting_period_data['toppeaks'][electricity_index], 2) if electricity_index >= 0 else "-" |
||
310 | |||
311 | ws['D14'].font = title_font |
||
312 | ws['D14'].alignment = c_c_alignment |
||
313 | ws['D14'].border = f_border |
||
314 | ws['D14'] = '{:.2%}'.format(round(reporting_period_data['toppeaks'][electricity_index], 2) / carbonsum) \ |
||
315 | if carbonsum is not None and carbonsum != Decimal(0.0) else " " |
||
316 | |||
317 | ws['B15'].font = title_font |
||
318 | ws['B15'].alignment = c_c_alignment |
||
319 | ws['B15'] = _('OnPeak') |
||
320 | ws['B15'].border = f_border |
||
321 | |||
322 | ws['C15'].font = title_font |
||
323 | ws['C15'].alignment = c_c_alignment |
||
324 | ws['C15'].border = f_border |
||
325 | ws['C15'] = round(reporting_period_data['onpeaks'][electricity_index], 2) if electricity_index >= 0 else "-" |
||
326 | |||
327 | ws['D15'].font = title_font |
||
328 | ws['D15'].alignment = c_c_alignment |
||
329 | ws['D15'].border = f_border |
||
330 | ws['D15'] = '{:.2%}'.format(round(reporting_period_data['onpeaks'][electricity_index], 2) / carbonsum) \ |
||
331 | if carbonsum is not None and carbonsum != Decimal(0.0) else " " |
||
332 | |||
333 | ws['B16'].font = title_font |
||
334 | ws['B16'].alignment = c_c_alignment |
||
335 | ws['B16'] = _('MidPeak') |
||
336 | ws['B16'].border = f_border |
||
337 | |||
338 | ws['C16'].font = title_font |
||
339 | ws['C16'].alignment = c_c_alignment |
||
340 | ws['C16'].border = f_border |
||
341 | ws['C16'] = round(reporting_period_data['midpeaks'][electricity_index], 2) if electricity_index >= 0 else "-" |
||
342 | |||
343 | ws['D16'].font = title_font |
||
344 | ws['D16'].alignment = c_c_alignment |
||
345 | ws['D16'].border = f_border |
||
346 | ws['D16'] = '{:.2%}'.format(round(reporting_period_data['midpeaks'][electricity_index], 2) / carbonsum) \ |
||
347 | if carbonsum is not None and carbonsum != Decimal(0.0) else " " |
||
348 | |||
349 | ws['B17'].font = title_font |
||
350 | ws['B17'].alignment = c_c_alignment |
||
351 | ws['B17'] = _('OffPeak') |
||
352 | ws['B17'].border = f_border |
||
353 | |||
354 | ws['C17'].font = title_font |
||
355 | ws['C17'].alignment = c_c_alignment |
||
356 | ws['C17'].border = f_border |
||
357 | ws['C17'] = round(reporting_period_data['offpeaks'][electricity_index], 2) if electricity_index >= 0 else "-" |
||
358 | |||
359 | ws['D17'].font = title_font |
||
360 | ws['D17'].alignment = c_c_alignment |
||
361 | ws['D17'].border = f_border |
||
362 | ws['D17'] = '{:.2%}'.format(round(reporting_period_data['offpeaks'][electricity_index], 2) / carbonsum) \ |
||
363 | if carbonsum is not None and carbonsum != Decimal(0.0) else " " |
||
364 | |||
365 | pie = PieChart() |
||
366 | pie.title = name + _('Electricity Carbon Dioxide Emissions by Time-Of-Use') |
||
367 | labels = Reference(ws, min_col=2, min_row=14, max_row=17) |
||
368 | pie_data = Reference(ws, min_col=3, min_row=13, max_row=17) |
||
369 | pie.add_data(pie_data, titles_from_data=True) |
||
370 | pie.set_categories(labels) |
||
371 | pie.height = 6.6 |
||
372 | pie.width = 9 |
||
373 | s1 = pie.series[0] |
||
374 | s1.dLbls = DataLabelList() |
||
375 | s1.dLbls.showCatName = False |
||
376 | s1.dLbls.showVal = True |
||
377 | s1.dLbls.showPercent = True |
||
378 | ws.add_chart(pie, "E13") |
||
379 | |||
380 | ################################################ |
||
381 | |||
382 | current_row_number = 19 |
||
383 | if "subtotals" not in reporting_period_data.keys() or \ |
||
384 | reporting_period_data['subtotals'] is None or \ |
||
385 | len(reporting_period_data['subtotals']) == 0: |
||
386 | for i in range(21, 29 + 1): |
||
387 | current_row_number = 30 |
||
388 | ws.row_dimensions[i].height = 0.1 |
||
389 | else: |
||
390 | ws['B' + str(current_row_number)].font = title_font |
||
391 | ws['B' + str(current_row_number)] = name + ' ' + _('Carbon Dioxide Emissions Proportion') |
||
392 | |||
393 | current_row_number += 1 |
||
394 | |||
395 | table_start_row_number = current_row_number |
||
396 | |||
397 | ws['B' + str(current_row_number)].fill = table_fill |
||
398 | ws['B' + str(current_row_number)].font = name_font |
||
399 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
400 | ws['B' + str(current_row_number)].border = f_border |
||
401 | |||
402 | ws['C' + str(current_row_number)].fill = table_fill |
||
403 | ws['C' + str(current_row_number)].font = name_font |
||
404 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
405 | ws['C' + str(current_row_number)].border = f_border |
||
406 | ws['C' + str(current_row_number)] = _('Carbon Dioxide Emissions') |
||
407 | |||
408 | ws['D' + str(current_row_number)].fill = table_fill |
||
409 | ws['D' + str(current_row_number)].font = name_font |
||
410 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
411 | ws['D' + str(current_row_number)].border = f_border |
||
412 | ws['D' + str(current_row_number)] = _('Carbon Dioxide Emissions Proportion') |
||
413 | |||
414 | current_row_number += 1 |
||
415 | |||
416 | ca_len = len(reporting_period_data['names']) |
||
417 | carbonsum = Decimal(0.0) |
||
418 | for i in range(0, ca_len): |
||
419 | carbonsum = round(reporting_period_data['subtotals'][i], 2) + carbonsum |
||
420 | for i in range(0, ca_len): |
||
421 | ws['B' + str(current_row_number)].font = title_font |
||
422 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
423 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
||
424 | ws['B' + str(current_row_number)].border = f_border |
||
425 | |||
426 | ws['C' + str(current_row_number)].font = title_font |
||
427 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
428 | ws['C' + str(current_row_number)].border = f_border |
||
429 | ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) |
||
430 | |||
431 | ws['D' + str(current_row_number)].font = title_font |
||
432 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
433 | ws['D' + str(current_row_number)].border = f_border |
||
434 | ws['D' + str(current_row_number)] = '{:.2%}'.format(round( |
||
435 | reporting_period_data['subtotals'][i], 2) / carbonsum) if \ |
||
436 | carbonsum is not None and carbonsum != Decimal(0.0) else " " |
||
437 | current_row_number += 1 |
||
438 | |||
439 | table_end_row_number = current_row_number - 1 |
||
440 | |||
441 | pie = PieChart() |
||
442 | pie.title = name + ' ' + _('Carbon Dioxide Emissions Proportion') |
||
443 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
444 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
||
445 | pie.add_data(pie_data, titles_from_data=True) |
||
446 | pie.set_categories(labels) |
||
447 | pie.height = 6.6 |
||
448 | pie.width = 9 |
||
449 | s1 = pie.series[0] |
||
450 | s1.dLbls = DataLabelList() |
||
451 | s1.dLbls.showCatName = False |
||
452 | s1.dLbls.showVal = True |
||
453 | s1.dLbls.showPercent = True |
||
454 | table_cell = 'E' + str(table_start_row_number) |
||
455 | ws.add_chart(pie, table_cell) |
||
456 | |||
457 | if ca_len < 4: |
||
458 | current_row_number = current_row_number - ca_len + 4 |
||
459 | |||
460 | current_row_number += 1 |
||
461 | |||
462 | #################################################################################################################### |
||
463 | table_start_draw_flag = current_row_number + 1 |
||
464 | |||
465 | if "timestamps" not in reporting_period_data.keys() or \ |
||
466 | reporting_period_data['timestamps'] is None or \ |
||
467 | len(reporting_period_data['timestamps']) == 0: |
||
468 | pass |
||
469 | else: |
||
470 | if not is_base_period_timestamp_exists_flag: |
||
471 | reporting_period_data = report['reporting_period'] |
||
472 | times = reporting_period_data['timestamps'] |
||
473 | ca_len = len(report['reporting_period']['names']) |
||
474 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
||
475 | ws['B' + str(current_row_number)].font = title_font |
||
476 | ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data') |
||
477 | |||
478 | current_row_number += 1 |
||
479 | # 1: Stand for blank line 2: Stand for title |
||
480 | current_row_number += ca_len * 6 + real_timestamps_len * 6 + 1 + 2 |
||
481 | table_start_row_number = current_row_number |
||
482 | |||
483 | time = times[0] |
||
484 | has_data = False |
||
485 | |||
486 | if len(time) > 0: |
||
487 | has_data = True |
||
488 | |||
489 | if has_data: |
||
490 | |||
491 | ws.row_dimensions[current_row_number].height = 60 |
||
492 | current_col_number = 2 |
||
493 | col = format_cell.get_column_letter(current_col_number) |
||
494 | ws[col + str(current_row_number)].fill = table_fill |
||
495 | ws[col + str(current_row_number)].font = title_font |
||
496 | ws[col + str(current_row_number)].border = f_border |
||
497 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
498 | ws[col + str(current_row_number)] = _('Datetime') |
||
499 | |||
500 | for i in range(0, ca_len): |
||
501 | current_col_number += 1 |
||
502 | col = format_cell.get_column_letter(current_col_number) |
||
503 | |||
504 | ws[col + str(current_row_number)].fill = table_fill |
||
505 | ws[col + str(current_row_number)].font = title_font |
||
506 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
507 | ws[col + str(current_row_number)] = reporting_period_data['names'][i] + \ |
||
508 | " (" + reporting_period_data['units'][i] + ")" |
||
509 | ws[col + str(current_row_number)].border = f_border |
||
510 | |||
511 | current_col_number += 1 |
||
512 | col = format_cell.get_column_letter(current_col_number) |
||
513 | ws[col + str(current_row_number)].fill = table_fill |
||
514 | ws[col + str(current_row_number)].font = title_font |
||
515 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
516 | ws[col + str(current_row_number)] = _('Total') + '(' + report['reporting_period']['total_unit'] + ')' |
||
517 | ws[col + str(current_row_number)].border = f_border |
||
518 | |||
519 | current_row_number += 1 |
||
520 | |||
521 | for i in range(0, len(time)): |
||
522 | current_col_number = 2 |
||
523 | col = format_cell.get_column_letter(current_col_number) |
||
524 | |||
525 | ws[col + str(current_row_number)].font = title_font |
||
526 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
527 | ws[col + str(current_row_number)] = time[i] |
||
528 | ws[col + str(current_row_number)].border = f_border |
||
529 | |||
530 | total = Decimal(0.0) |
||
531 | |||
532 | for j in range(0, ca_len): |
||
533 | current_col_number += 1 |
||
534 | col = format_cell.get_column_letter(current_col_number) |
||
535 | |||
536 | ws[col + str(current_row_number)].font = title_font |
||
537 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
538 | ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) |
||
539 | total += reporting_period_data['values'][j][i] |
||
540 | ws[col + str(current_row_number)].border = f_border |
||
541 | |||
542 | current_col_number += 1 |
||
543 | col = format_cell.get_column_letter(current_col_number) |
||
544 | ws[col + str(current_row_number)].font = title_font |
||
545 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
546 | ws[col + str(current_row_number)] = round(total, 2) |
||
547 | ws[col + str(current_row_number)].border = f_border |
||
548 | |||
549 | current_row_number += 1 |
||
550 | |||
551 | table_end_row_number = current_row_number - 1 |
||
552 | |||
553 | current_col_number = 2 |
||
554 | col = format_cell.get_column_letter(current_col_number) |
||
555 | |||
556 | ws[col + str(current_row_number)].font = title_font |
||
557 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
558 | ws[col + str(current_row_number)] = _('Subtotal') |
||
559 | ws[col + str(current_row_number)].border = f_border |
||
560 | |||
561 | subtotals = Decimal(0.0) |
||
562 | |||
563 | for i in range(0, ca_len): |
||
564 | current_col_number += 1 |
||
565 | col = format_cell.get_column_letter(current_col_number) |
||
566 | ws[col + str(current_row_number)].font = title_font |
||
567 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
568 | ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) |
||
569 | subtotals += reporting_period_data['subtotals'][i] |
||
570 | ws[col + str(current_row_number)].border = f_border |
||
571 | |||
572 | # line |
||
573 | line = LineChart() |
||
574 | line.title = _('Reporting Period Carbon Dioxide Emissions') + ' - ' \ |
||
575 | + reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
576 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
577 | line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, |
||
578 | max_row=table_end_row_number) |
||
579 | line.add_data(line_data, titles_from_data=True) |
||
580 | line.set_categories(labels) |
||
581 | line_data = line.series[0] |
||
582 | line_data.marker.symbol = "circle" |
||
583 | line_data.smooth = True |
||
584 | line.x_axis.crosses = 'min' |
||
585 | line.height = 8.25 |
||
586 | line.width = 24 |
||
587 | line.dLbls = DataLabelList() |
||
588 | line.dLbls.dLblPos = 't' |
||
589 | line.dLbls.showVal = True |
||
590 | line.dLbls.showPercent = False |
||
591 | chart_col = 'B' |
||
592 | chart_cell = chart_col + str(table_start_draw_flag + 6 * i) |
||
593 | ws.add_chart(line, chart_cell) |
||
594 | |||
595 | current_col_number += 1 |
||
596 | col = format_cell.get_column_letter(current_col_number) |
||
597 | ws[col + str(current_row_number)].font = title_font |
||
598 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
599 | ws[col + str(current_row_number)] = round(subtotals, 2) |
||
600 | ws[col + str(current_row_number)].border = f_border |
||
601 | |||
602 | current_row_number += 2 |
||
603 | else: |
||
604 | base_period_data = report['base_period'] |
||
605 | reporting_period_data = report['reporting_period'] |
||
606 | base_period_timestamps = base_period_data['timestamps'] |
||
607 | reporting_period_timestamps = reporting_period_data['timestamps'] |
||
608 | # Tip: |
||
609 | # base_period_data['names'] == reporting_period_data['names'] |
||
610 | # base_period_data['units'] == reporting_period_data['units'] |
||
611 | base_period_data_ca_len = len(base_period_data['names']) |
||
612 | reporting_period_data_ca_len = len(reporting_period_data['names']) |
||
613 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
||
614 | ws['B' + str(current_row_number)].font = title_font |
||
615 | ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data') |
||
616 | |||
617 | current_row_number += 1 |
||
618 | # 1: Stand for blank line 2: Stand for title |
||
619 | current_row_number += reporting_period_data_ca_len * 6 + real_timestamps_len * 6 + 1 + 2 |
||
620 | table_start_row_number = current_row_number |
||
621 | |||
622 | has_data = False |
||
623 | |||
624 | if len(base_period_timestamps[0]) or len(reporting_period_timestamps[0]) > 0: |
||
625 | has_data = True |
||
626 | |||
627 | if has_data: |
||
628 | ws.row_dimensions[current_row_number].height = 60 |
||
629 | current_col_number = 2 |
||
630 | col = format_cell.get_column_letter(current_col_number) |
||
631 | ws[col + str(current_row_number)].fill = table_fill |
||
632 | ws[col + str(current_row_number)].font = title_font |
||
633 | ws[col + str(current_row_number)].border = f_border |
||
634 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
635 | ws[col + str(current_row_number)] = _('Base Period') + " - " + _('Datetime') |
||
636 | |||
637 | for i in range(0, base_period_data_ca_len): |
||
638 | current_col_number += 1 |
||
639 | col = format_cell.get_column_letter(current_col_number) |
||
640 | |||
641 | ws[col + str(current_row_number)].fill = table_fill |
||
642 | ws[col + str(current_row_number)].font = title_font |
||
643 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
644 | ws[col + str(current_row_number)] = _('Base Period') + " - " + base_period_data['names'][i] + \ |
||
645 | " (" + base_period_data['units'][i] + ")" |
||
646 | ws[col + str(current_row_number)].border = f_border |
||
647 | |||
648 | current_col_number += 1 |
||
649 | col = format_cell.get_column_letter(current_col_number) |
||
650 | ws[col + str(current_row_number)].fill = table_fill |
||
651 | ws[col + str(current_row_number)].font = title_font |
||
652 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
653 | ws[col + str(current_row_number)] = _('Base Period') + " - " \ |
||
654 | + _('Total') + '(' + report['reporting_period']['total_unit'] + ')' |
||
655 | ws[col + str(current_row_number)].border = f_border |
||
656 | |||
657 | current_col_number += 1 |
||
658 | col = format_cell.get_column_letter(current_col_number) |
||
659 | |||
660 | ws[col + str(current_row_number)].fill = table_fill |
||
661 | ws[col + str(current_row_number)].font = title_font |
||
662 | ws[col + str(current_row_number)].border = f_border |
||
663 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
664 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " + _('Datetime') |
||
665 | |||
666 | for i in range(0, reporting_period_data_ca_len): |
||
667 | current_col_number += 1 |
||
668 | col = format_cell.get_column_letter(current_col_number) |
||
669 | ws[col + str(current_row_number)].fill = table_fill |
||
670 | ws[col + str(current_row_number)].font = title_font |
||
671 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
672 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " \ |
||
673 | + reporting_period_data['names'][i] + " (" + \ |
||
674 | reporting_period_data['units'][i] + ")" |
||
675 | ws[col + str(current_row_number)].border = f_border |
||
676 | |||
677 | current_col_number += 1 |
||
678 | col = format_cell.get_column_letter(current_col_number) |
||
679 | ws[col + str(current_row_number)].fill = table_fill |
||
680 | ws[col + str(current_row_number)].font = title_font |
||
681 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
682 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " \ |
||
683 | + _('Total') + '(' + report['reporting_period']['total_unit'] + ')' |
||
684 | ws[col + str(current_row_number)].border = f_border |
||
685 | |||
686 | current_row_number += 1 |
||
687 | |||
688 | max_timestamps_len = len(base_period_timestamps[0]) \ |
||
689 | if len(base_period_timestamps[0]) >= len(reporting_period_timestamps[0]) \ |
||
690 | else len(reporting_period_timestamps[0]) |
||
691 | |||
692 | for i in range(0, max_timestamps_len): |
||
693 | current_col_number = 2 |
||
694 | col = format_cell.get_column_letter(current_col_number) |
||
695 | ws[col + str(current_row_number)].font = title_font |
||
696 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
697 | ws[col + str(current_row_number)] = base_period_timestamps[0][i] \ |
||
698 | if i < len(base_period_timestamps[0]) else None |
||
699 | ws[col + str(current_row_number)].border = f_border |
||
700 | |||
701 | base_period_total = Decimal(0.0) |
||
702 | |||
703 | for j in range(0, base_period_data_ca_len): |
||
704 | current_col_number += 1 |
||
705 | col = format_cell.get_column_letter(current_col_number) |
||
706 | |||
707 | ws[col + str(current_row_number)].font = title_font |
||
708 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
709 | ws[col + str(current_row_number)] = round(base_period_data['values'][j][i], 2) \ |
||
710 | if i < len(base_period_data['values'][j]) else None |
||
711 | if i < len(base_period_timestamps[0]): |
||
712 | base_period_total += base_period_data['values'][j][i] |
||
713 | ws[col + str(current_row_number)].border = f_border |
||
714 | |||
715 | current_col_number += 1 |
||
716 | col = format_cell.get_column_letter(current_col_number) |
||
717 | ws[col + str(current_row_number)].font = title_font |
||
718 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
719 | ws[col + str(current_row_number)] = round(base_period_total, 2) \ |
||
720 | if i < len(base_period_timestamps[0]) else None |
||
721 | ws[col + str(current_row_number)].border = f_border |
||
722 | |||
723 | current_col_number += 1 |
||
724 | col = format_cell.get_column_letter(current_col_number) |
||
725 | |||
726 | ws[col + str(current_row_number)].font = title_font |
||
727 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
728 | ws[col + str(current_row_number)] = reporting_period_timestamps[0][i] \ |
||
729 | if i < len(reporting_period_timestamps[0]) else None |
||
730 | ws[col + str(current_row_number)].border = f_border |
||
731 | |||
732 | reporting_period_total = Decimal(0.0) |
||
733 | |||
734 | for j in range(0, reporting_period_data_ca_len): |
||
735 | current_col_number += 1 |
||
736 | col = format_cell.get_column_letter(current_col_number) |
||
737 | |||
738 | ws[col + str(current_row_number)].font = title_font |
||
739 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
740 | ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \ |
||
741 | if i < len(reporting_period_data['values'][j]) else None |
||
742 | if i < len(reporting_period_timestamps[0]): |
||
743 | reporting_period_total += reporting_period_data['values'][j][i] |
||
744 | ws[col + str(current_row_number)].border = f_border |
||
745 | |||
746 | current_col_number += 1 |
||
747 | col = format_cell.get_column_letter(current_col_number) |
||
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)] = round(reporting_period_total, 2) \ |
||
751 | if i < len(reporting_period_timestamps[0]) else None |
||
752 | ws[col + str(current_row_number)].border = f_border |
||
753 | |||
754 | current_row_number += 1 |
||
755 | |||
756 | current_col_number = 2 |
||
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)] = _('Subtotal') |
||
761 | ws[col + str(current_row_number)].border = f_border |
||
762 | |||
763 | base_period_subtotals = Decimal(0.0) |
||
764 | |||
765 | for i in range(0, base_period_data_ca_len): |
||
766 | current_col_number += 1 |
||
767 | col = format_cell.get_column_letter(current_col_number) |
||
768 | ws[col + str(current_row_number)].font = title_font |
||
769 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
770 | ws[col + str(current_row_number)] = round(base_period_data['subtotals'][i], 2) |
||
771 | base_period_subtotals += base_period_data['subtotals'][i] |
||
772 | ws[col + str(current_row_number)].border = f_border |
||
773 | |||
774 | current_col_number += 1 |
||
775 | col = format_cell.get_column_letter(current_col_number) |
||
776 | ws[col + str(current_row_number)].font = title_font |
||
777 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
778 | ws[col + str(current_row_number)] = round(base_period_subtotals, 2) |
||
779 | ws[col + str(current_row_number)].border = f_border |
||
780 | |||
781 | current_col_number += 1 |
||
782 | col = format_cell.get_column_letter(current_col_number) |
||
783 | |||
784 | ws[col + str(current_row_number)].font = title_font |
||
785 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
786 | ws[col + str(current_row_number)] = _('Subtotal') |
||
787 | ws[col + str(current_row_number)].border = f_border |
||
788 | |||
789 | reporting_period_subtotals = Decimal(0.0) |
||
790 | |||
791 | for i in range(0, reporting_period_data_ca_len): |
||
792 | current_col_number += 1 |
||
793 | col = format_cell.get_column_letter(current_col_number) |
||
794 | ws[col + str(current_row_number)].font = title_font |
||
795 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
796 | ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) |
||
797 | reporting_period_subtotals += reporting_period_data['subtotals'][i] |
||
798 | ws[col + str(current_row_number)].border = f_border |
||
799 | |||
800 | current_col_number += 1 |
||
801 | col = format_cell.get_column_letter(current_col_number) |
||
802 | ws[col + str(current_row_number)].font = title_font |
||
803 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
804 | ws[col + str(current_row_number)] = round(reporting_period_subtotals, 2) |
||
805 | ws[col + str(current_row_number)].border = f_border |
||
806 | |||
807 | for i in range(0, reporting_period_data_ca_len): |
||
808 | # line |
||
809 | line = LineChart() |
||
810 | line.title = _('Base Period Carbon Dioxide Emissions') + " / " \ |
||
811 | + _('Reporting Period Carbon Dioxide Emissions') + ' - ' \ |
||
812 | + reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
813 | labels = Reference(ws, min_col=2 + base_period_data_ca_len + 1 + 1, |
||
814 | min_row=table_start_row_number + 1, |
||
815 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
816 | base_line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, |
||
817 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
818 | reporting_line_data = Reference(ws, min_col=3 + base_period_data_ca_len + 1 + 1 + i, |
||
819 | min_row=table_start_row_number, |
||
820 | max_row=table_start_row_number |
||
821 | + len(reporting_period_timestamps[0])) |
||
822 | line.add_data(base_line_data, titles_from_data=True) |
||
823 | line.add_data(reporting_line_data, titles_from_data=True) |
||
824 | line.set_categories(labels) |
||
825 | for j in range(len(line.series)): |
||
826 | line.series[j].marker.symbol = "circle" |
||
827 | line.series[j].smooth = True |
||
828 | line.x_axis.crosses = 'min' |
||
829 | line.height = 8.25 |
||
830 | line.width = 24 |
||
831 | line.dLbls = DataLabelList() |
||
832 | line.dLbls.dLblPos = 't' |
||
833 | line.dLbls.showVal = True |
||
834 | line.dLbls.showPercent = False |
||
835 | chart_col = 'B' |
||
836 | chart_cell = chart_col + str(table_start_draw_flag + 6 * i) |
||
837 | ws.add_chart(line, chart_cell) |
||
838 | |||
839 | current_row_number += 2 |
||
840 | |||
841 | #################################################################################################################### |
||
842 | |||
843 | if "associated_equipment" not in report.keys() or \ |
||
844 | "energy_category_names" not in report['associated_equipment'].keys() or \ |
||
845 | len(report['associated_equipment']["energy_category_names"]) == 0 \ |
||
846 | or 'associated_equipment_names_array' not in report['associated_equipment'].keys() \ |
||
847 | or report['associated_equipment']['associated_equipment_names_array'] is None \ |
||
848 | or len(report['associated_equipment']['associated_equipment_names_array']) == 0 \ |
||
849 | or len(report['associated_equipment']['associated_equipment_names_array'][0]) == 0: |
||
850 | pass |
||
851 | else: |
||
852 | associated_equipment = report['associated_equipment'] |
||
853 | current_row_number += 1 |
||
854 | |||
855 | ws['B' + str(current_row_number)].font = title_font |
||
856 | ws['B' + str(current_row_number)] = name + ' ' + _('Associated Equipment Data') |
||
857 | |||
858 | current_row_number += 1 |
||
859 | |||
860 | ws.row_dimensions[current_row_number].height = 60 |
||
861 | ws['B' + str(current_row_number)].fill = table_fill |
||
862 | ws['B' + str(current_row_number)].font = name_font |
||
863 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
864 | ws['B' + str(current_row_number)].border = f_border |
||
865 | ws['B' + str(current_row_number)] = _('Associated Equipment') |
||
866 | ca_len = len(associated_equipment['energy_category_names']) |
||
867 | |||
868 | for i in range(0, ca_len): |
||
869 | col = chr(ord('C') + i) |
||
870 | ws[col + str(current_row_number)].fill = table_fill |
||
871 | ws[col + str(current_row_number)].font = name_font |
||
872 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
873 | ws[col + str(current_row_number)].border = f_border |
||
874 | ws[col + str(current_row_number)] = \ |
||
875 | associated_equipment['energy_category_names'][i] + " (" + associated_equipment['units'][i] + ")" |
||
876 | |||
877 | end_col = chr(ord('B') + ca_len + 1) |
||
878 | ws[end_col + str(current_row_number)].fill = table_fill |
||
879 | ws[end_col + str(current_row_number)].font = name_font |
||
880 | ws[end_col + str(current_row_number)].alignment = c_c_alignment |
||
881 | ws[end_col + str(current_row_number)].border = f_border |
||
882 | ws[end_col + str(current_row_number)] = _("Total") + " (" + reporting_period_data['units'][i] + ")" |
||
883 | |||
884 | associated_equipment_len = len(associated_equipment['associated_equipment_names_array'][0]) |
||
885 | |||
886 | for i in range(0, associated_equipment_len): |
||
887 | current_row_number += 1 |
||
888 | row = str(current_row_number) |
||
889 | value = Decimal(0.0) |
||
890 | |||
891 | ws['B' + row].font = title_font |
||
892 | ws['B' + row].alignment = c_c_alignment |
||
893 | ws['B' + row] = associated_equipment['associated_equipment_names_array'][0][i] |
||
894 | ws['B' + row].border = f_border |
||
895 | |||
896 | for j in range(0, ca_len): |
||
897 | col = chr(ord('C') + j) |
||
898 | ws[col + row].font = title_font |
||
899 | ws[col + row].alignment = c_c_alignment |
||
900 | ws[col + row] = round(associated_equipment['subtotals_array'][j][i], 2) |
||
901 | value += round(associated_equipment['subtotals_array'][j][i], 2) |
||
902 | ws[col + row].border = f_border |
||
903 | |||
904 | end_col = chr(ord(col) + 1) |
||
905 | ws[end_col + row].font = title_font |
||
906 | ws[end_col + row].alignment = c_c_alignment |
||
907 | ws[end_col + row] = round(value, 2) |
||
908 | ws[end_col + row].border = f_border |
||
909 | |||
910 | #################################################################################################################### |
||
911 | current_sheet_parameters_row_number = table_start_draw_flag + len(reporting_period_data['names']) * 6 + 1 |
||
912 | if 'parameters' not in report.keys() or \ |
||
913 | report['parameters'] is None or \ |
||
914 | 'names' not in report['parameters'].keys() or \ |
||
915 | report['parameters']['names'] is None or \ |
||
916 | len(report['parameters']['names']) == 0 or \ |
||
917 | 'timestamps' not in report['parameters'].keys() or \ |
||
918 | report['parameters']['timestamps'] is None or \ |
||
919 | len(report['parameters']['timestamps']) == 0 or \ |
||
920 | 'values' not in report['parameters'].keys() or \ |
||
921 | report['parameters']['values'] is None or \ |
||
922 | len(report['parameters']['values']) == 0 or \ |
||
923 | timestamps_data_all_equal_0(report['parameters']['timestamps']): |
||
924 | pass |
||
925 | else: |
||
926 | ################################################################################################################ |
||
927 | # new worksheet |
||
928 | ################################################################################################################ |
||
929 | |||
930 | parameters_data = report['parameters'] |
||
931 | parameters_names_len = len(parameters_data['names']) |
||
932 | |||
933 | file_name = (re.sub(r'[^A-Z]', '', ws.title)) + '_' |
||
934 | parameters_ws = wb.create_sheet(file_name + _('Parameters')) |
||
935 | |||
936 | parameters_timestamps_data_max_len = \ |
||
937 | get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
||
938 | |||
939 | # Row height |
||
940 | parameters_ws.row_dimensions[1].height = 102 |
||
941 | for i in range(2, 7 + 1): |
||
942 | parameters_ws.row_dimensions[i].height = 42 |
||
943 | |||
944 | for i in range(8, parameters_timestamps_data_max_len + 10): |
||
945 | parameters_ws.row_dimensions[i].height = 60 |
||
946 | |||
947 | # Col width |
||
948 | parameters_ws.column_dimensions['A'].width = 1.5 |
||
949 | |||
950 | parameters_ws.column_dimensions['B'].width = 25.0 |
||
951 | |||
952 | for i in range(3, 12 + parameters_names_len * 3): |
||
953 | parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
||
954 | |||
955 | # Img |
||
956 | img = Image("excelexporters/myems.png") |
||
957 | parameters_ws.add_image(img, 'A1') |
||
958 | |||
959 | # Title |
||
960 | parameters_ws['B3'].alignment = b_r_alignment |
||
961 | parameters_ws['B3'] = _('Name') + ':' |
||
962 | parameters_ws['C3'].border = b_border |
||
963 | parameters_ws['C3'].alignment = b_c_alignment |
||
964 | parameters_ws['C3'] = name |
||
965 | |||
966 | parameters_ws['D3'].alignment = b_r_alignment |
||
967 | parameters_ws['D3'] = _('Period Type') + ':' |
||
968 | parameters_ws['E3'].border = b_border |
||
969 | parameters_ws['E3'].alignment = b_c_alignment |
||
970 | parameters_ws['E3'] = period_type |
||
971 | |||
972 | parameters_ws['B4'].alignment = b_r_alignment |
||
973 | parameters_ws['B4'] = _('Reporting Start Datetime') + ':' |
||
974 | parameters_ws['C4'].border = b_border |
||
975 | parameters_ws['C4'].alignment = b_c_alignment |
||
976 | parameters_ws['C4'] = reporting_start_datetime_local |
||
977 | |||
978 | parameters_ws['D4'].alignment = b_r_alignment |
||
979 | parameters_ws['D4'] = _('Reporting End Datetime') + ':' |
||
980 | parameters_ws['E4'].border = b_border |
||
981 | parameters_ws['E4'].alignment = b_c_alignment |
||
982 | parameters_ws['E4'] = reporting_end_datetime_local |
||
983 | |||
984 | parameters_ws_current_row_number = 6 |
||
985 | |||
986 | parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
||
987 | parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' ' + _('Parameters') |
||
988 | |||
989 | parameters_ws_current_row_number += 1 |
||
990 | |||
991 | parameters_table_start_row_number = parameters_ws_current_row_number |
||
992 | |||
993 | parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
||
994 | |||
995 | parameters_ws_current_row_number += 1 |
||
996 | |||
997 | table_current_col_number = 2 |
||
998 | |||
999 | for i in range(0, parameters_names_len): |
||
1000 | |||
1001 | if len(parameters_data['timestamps'][i]) == 0: |
||
1002 | continue |
||
1003 | |||
1004 | col = format_cell.get_column_letter(table_current_col_number) |
||
1005 | |||
1006 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
1007 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
1008 | |||
1009 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
1010 | |||
1011 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
1012 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
1013 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = name_font |
||
1014 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = c_c_alignment |
||
1015 | parameters_ws[col + str(parameters_ws_current_row_number - 1)] = parameters_data['names'][i] |
||
1016 | |||
1017 | table_current_row_number = parameters_ws_current_row_number |
||
1018 | |||
1019 | for j, value in enumerate(list(parameters_data['timestamps'][i])): |
||
1020 | col = format_cell.get_column_letter(table_current_col_number) |
||
1021 | |||
1022 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
1023 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
1024 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
1025 | parameters_ws[col + str(table_current_row_number)] = value |
||
1026 | |||
1027 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
1028 | |||
1029 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
1030 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
1031 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
1032 | parameters_ws[col + str(table_current_row_number)] = round(parameters_data['values'][i][j], 2) |
||
1033 | |||
1034 | table_current_row_number += 1 |
||
1035 | |||
1036 | table_current_col_number = table_current_col_number + 3 |
||
1037 | |||
1038 | ################################################################################################################ |
||
1039 | # parameters chart and parameters table |
||
1040 | ################################################################################################################ |
||
1041 | |||
1042 | ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
||
1043 | ws['B' + str(current_sheet_parameters_row_number)] = name + ' ' + _('Parameters') |
||
1044 | |||
1045 | current_sheet_parameters_row_number += 1 |
||
1046 | |||
1047 | chart_start_row_number = current_sheet_parameters_row_number |
||
1048 | |||
1049 | col_index = 0 |
||
1050 | |||
1051 | for i in range(0, parameters_names_len): |
||
1052 | |||
1053 | if len(parameters_data['timestamps'][i]) == 0: |
||
1054 | continue |
||
1055 | |||
1056 | line = LineChart() |
||
1057 | data_col = 3 + col_index * 3 |
||
1058 | labels_col = 2 + col_index * 3 |
||
1059 | col_index += 1 |
||
1060 | line.title = _('Parameters') + ' - ' + \ |
||
1061 | parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
||
1062 | labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
||
1063 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
1064 | line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
||
1065 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
1066 | line.add_data(line_data, titles_from_data=True) |
||
1067 | line.set_categories(labels) |
||
1068 | line_data = line.series[0] |
||
1069 | line_data.marker.symbol = "circle" |
||
1070 | line_data.smooth = True |
||
1071 | line.x_axis.crosses = 'min' |
||
1072 | line.height = 8.25 |
||
1073 | line.width = 24 |
||
1074 | line.dLbls = DataLabelList() |
||
1075 | line.dLbls.dLblPos = 't' |
||
1076 | line.dLbls.showVal = False |
||
1077 | line.dLbls.showPercent = False |
||
1078 | chart_col = 'B' |
||
1079 | chart_cell = chart_col + str(chart_start_row_number) |
||
1080 | chart_start_row_number += 6 |
||
1081 | ws.add_chart(line, chart_cell) |
||
1082 | |||
1083 | current_sheet_parameters_row_number = chart_start_row_number |
||
1084 | |||
1085 | current_sheet_parameters_row_number += 1 |
||
1086 | #################################################################################################################### |
||
1087 | filename = str(uuid.uuid4()) + '.xlsx' |
||
1088 | wb.save(filename) |
||
1089 | |||
1090 | return filename |
||
1091 | |||
1129 |