1
|
|
|
import base64 |
2
|
|
|
import gettext |
3
|
|
|
import os |
4
|
|
|
import re |
5
|
|
|
import uuid |
6
|
|
|
|
7
|
|
|
import openpyxl.utils.cell as format_cell |
8
|
|
|
from openpyxl import Workbook |
9
|
|
|
from openpyxl.chart import LineChart, Reference |
10
|
|
|
from openpyxl.chart.label import DataLabelList |
11
|
|
|
from openpyxl.drawing.image import Image |
12
|
|
|
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
######################################################################################################################## |
16
|
|
|
# PROCEDURES |
17
|
|
|
# Step 1: Validate the report data |
18
|
|
|
# Step 2: Generate excel file from the report data |
19
|
|
|
# Step 3: Encode the excel file to Base64 |
20
|
|
|
######################################################################################################################## |
21
|
|
|
|
22
|
|
View Code Duplication |
def export(report, name, reporting_start_datetime_local, reporting_end_datetime_local, base_period_start_datetime, |
|
|
|
|
23
|
|
|
base_period_end_datetime, period_type, language): |
24
|
|
|
#################################################################################################################### |
25
|
|
|
# Step 1: Validate the report data |
26
|
|
|
#################################################################################################################### |
27
|
|
|
if report is None: |
28
|
|
|
return None |
29
|
|
|
|
30
|
|
|
#################################################################################################################### |
31
|
|
|
# Step 2: Generate excel file from the report data |
32
|
|
|
#################################################################################################################### |
33
|
|
|
filename = generate_excel(report, |
34
|
|
|
name, |
35
|
|
|
reporting_start_datetime_local, |
36
|
|
|
reporting_end_datetime_local, |
37
|
|
|
base_period_start_datetime, |
38
|
|
|
base_period_end_datetime, |
39
|
|
|
period_type, |
40
|
|
|
language) |
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
|
|
|
print(str(ex)) |
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
|
|
|
print(str(ex)) |
60
|
|
|
return base64_message |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
def generate_excel(report, name, reporting_start_datetime_local, reporting_end_datetime_local, |
64
|
|
|
base_period_start_datetime, base_period_end_datetime, period_type, language): |
65
|
|
|
locale_path = './i18n/' |
66
|
|
|
if language == 'zh_CN': |
67
|
|
|
trans = gettext.translation('myems', locale_path, languages=['zh_CN']) |
68
|
|
|
elif language == 'de': |
69
|
|
|
trans = gettext.translation('myems', locale_path, languages=['de']) |
70
|
|
|
elif language == 'en': |
71
|
|
|
trans = gettext.translation('myems', locale_path, languages=['en']) |
72
|
|
|
else: |
73
|
|
|
trans = gettext.translation('myems', locale_path, languages=['en']) |
74
|
|
|
trans.install() |
75
|
|
|
_ = trans.gettext |
76
|
|
|
|
77
|
|
|
wb = Workbook() |
78
|
|
|
ws = wb.active |
79
|
|
|
ws.title = "MeterEnergy" |
80
|
|
|
|
81
|
|
|
# Row height |
82
|
|
|
ws.row_dimensions[1].height = 102 |
83
|
|
|
for i in range(2, 2000 + 1): |
84
|
|
|
ws.row_dimensions[i].height = 42 |
85
|
|
|
|
86
|
|
|
# Col width |
87
|
|
|
ws.column_dimensions['A'].width = 1.5 |
88
|
|
|
|
89
|
|
|
ws.column_dimensions['B'].width = 25.0 |
90
|
|
|
|
91
|
|
|
for i in range(ord('C'), ord('L')): |
92
|
|
|
ws.column_dimensions[chr(i)].width = 15.0 |
93
|
|
|
|
94
|
|
|
# Font |
95
|
|
|
name_font = Font(name='Arial', size=15, bold=True) |
96
|
|
|
title_font = Font(name='Arial', size=15, bold=True) |
97
|
|
|
|
98
|
|
|
table_fill = PatternFill(fill_type='solid', fgColor='90ee90') |
99
|
|
|
f_border = Border(left=Side(border_style='medium'), |
100
|
|
|
right=Side(border_style='medium'), |
101
|
|
|
bottom=Side(border_style='medium'), |
102
|
|
|
top=Side(border_style='medium') |
103
|
|
|
) |
104
|
|
|
b_border = Border( |
105
|
|
|
bottom=Side(border_style='medium'), |
106
|
|
|
) |
107
|
|
|
|
108
|
|
|
b_c_alignment = Alignment(vertical='bottom', |
109
|
|
|
horizontal='center', |
110
|
|
|
text_rotation=0, |
111
|
|
|
wrap_text=True, |
112
|
|
|
shrink_to_fit=False, |
113
|
|
|
indent=0) |
114
|
|
|
c_c_alignment = Alignment(vertical='center', |
115
|
|
|
horizontal='center', |
116
|
|
|
text_rotation=0, |
117
|
|
|
wrap_text=True, |
118
|
|
|
shrink_to_fit=False, |
119
|
|
|
indent=0) |
120
|
|
|
b_r_alignment = Alignment(vertical='bottom', |
121
|
|
|
horizontal='right', |
122
|
|
|
text_rotation=0, |
123
|
|
|
wrap_text=True, |
124
|
|
|
shrink_to_fit=False, |
125
|
|
|
indent=0) |
126
|
|
|
|
127
|
|
|
# Img |
128
|
|
|
img = Image("excelexporters/myems.png") |
129
|
|
|
ws.add_image(img, 'A1') |
130
|
|
|
|
131
|
|
|
# Title |
132
|
|
|
ws['B3'].alignment = b_r_alignment |
133
|
|
|
ws['B3'] = _('Name') + ':' |
134
|
|
|
ws['C3'].border = b_border |
135
|
|
|
ws['C3'].alignment = b_c_alignment |
136
|
|
|
ws['C3'] = name |
137
|
|
|
|
138
|
|
|
ws['D3'].alignment = b_r_alignment |
139
|
|
|
ws['D3'] = _('Period Type') + ':' |
140
|
|
|
ws['E3'].border = b_border |
141
|
|
|
ws['E3'].alignment = b_c_alignment |
142
|
|
|
ws['E3'] = period_type |
143
|
|
|
|
144
|
|
|
ws['B4'].alignment = b_r_alignment |
145
|
|
|
ws['B4'] = _('Reporting Start Datetime') + ':' |
146
|
|
|
ws['C4'].border = b_border |
147
|
|
|
ws['C4'].alignment = b_c_alignment |
148
|
|
|
ws['C4'] = reporting_start_datetime_local |
149
|
|
|
|
150
|
|
|
ws['D4'].alignment = b_r_alignment |
151
|
|
|
ws['D4'] = _('Reporting End Datetime') + ':' |
152
|
|
|
ws['E4'].border = b_border |
153
|
|
|
ws['E4'].alignment = b_c_alignment |
154
|
|
|
ws['E4'] = reporting_end_datetime_local |
155
|
|
|
|
156
|
|
|
is_base_period_timestamp_exists_flag = is_base_period_timestamp_exists(report['base_period']) |
157
|
|
|
|
158
|
|
|
if is_base_period_timestamp_exists_flag: |
159
|
|
|
ws['B5'].alignment = b_r_alignment |
160
|
|
|
ws['B5'] = _('Base Period Start Datetime') + ':' |
161
|
|
|
ws['C5'].border = b_border |
162
|
|
|
ws['C5'].alignment = b_c_alignment |
163
|
|
|
ws['C5'] = base_period_start_datetime |
164
|
|
|
|
165
|
|
|
ws['D5'].alignment = b_r_alignment |
166
|
|
|
ws['D5'] = _('Base Period End Datetime') + ':' |
167
|
|
|
ws['E5'].border = b_border |
168
|
|
|
ws['E5'].alignment = b_c_alignment |
169
|
|
|
ws['E5'] = base_period_end_datetime |
170
|
|
|
|
171
|
|
|
if "reporting_period" not in report.keys() or \ |
172
|
|
|
"values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
173
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
|
|
|
|
174
|
|
|
wb.save(filename) |
175
|
|
|
|
176
|
|
|
return filename |
177
|
|
|
#################################################################################################################### |
178
|
|
|
# First: Consumption |
179
|
|
|
# 6: title |
180
|
|
|
# 7: table title |
181
|
|
|
# 8~9 table_data |
182
|
|
|
#################################################################################################################### |
183
|
|
|
if "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
184
|
|
|
for i in range(6, 9 + 1): |
185
|
|
|
ws.row_dimensions[i].height = 0.1 |
186
|
|
|
else: |
187
|
|
|
reporting_period_data = report['reporting_period'] |
188
|
|
|
|
189
|
|
|
ws.row_dimensions[7].height = 60 |
190
|
|
|
|
191
|
|
|
ws['B7'].font = title_font |
192
|
|
|
ws['B7'].alignment = c_c_alignment |
193
|
|
|
ws['B7'].fill = table_fill |
194
|
|
|
ws['B7'].border = f_border |
195
|
|
|
ws['B7'] = name |
196
|
|
|
|
197
|
|
|
ws['B8'].font = title_font |
198
|
|
|
ws['B8'].alignment = c_c_alignment |
199
|
|
|
ws['B8'] = _('Consumption') |
200
|
|
|
ws['B8'].border = f_border |
201
|
|
|
|
202
|
|
|
ws['B9'].font = title_font |
203
|
|
|
ws['B9'].alignment = c_c_alignment |
204
|
|
|
ws['B9'] = _('Increment Rate') |
205
|
|
|
ws['B9'].border = f_border |
206
|
|
|
|
207
|
|
|
ws['C7'].fill = table_fill |
208
|
|
|
ws['C7'].font = name_font |
209
|
|
|
ws['C7'].alignment = c_c_alignment |
210
|
|
|
ws['C7'] = report['meter']['energy_category_name'] + " (" + report['meter']['unit_of_measure'] + ")" |
211
|
|
|
ws['C7'].border = f_border |
212
|
|
|
|
213
|
|
|
ws['C8'].font = name_font |
214
|
|
|
ws['C8'].alignment = c_c_alignment |
215
|
|
|
ws['C8'] = round(reporting_period_data['total_in_category'], 2) |
216
|
|
|
ws['C8'].border = f_border |
217
|
|
|
|
218
|
|
|
ws['C9'].font = name_font |
219
|
|
|
ws['C9'].alignment = c_c_alignment |
220
|
|
|
ws['C9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
221
|
|
|
if reporting_period_data['increment_rate'] is not None else "-" |
222
|
|
|
ws['C9'].border = f_border |
223
|
|
|
|
224
|
|
|
# TCE |
225
|
|
|
ws['D7'].fill = table_fill |
226
|
|
|
ws['D7'].font = name_font |
227
|
|
|
ws['D7'].alignment = c_c_alignment |
228
|
|
|
ws['D7'] = _('Ton of Standard Coal') + '(TCE)' |
229
|
|
|
ws['D7'].border = f_border |
230
|
|
|
|
231
|
|
|
ws['D8'].font = name_font |
232
|
|
|
ws['D8'].alignment = c_c_alignment |
233
|
|
|
ws['D8'] = round(reporting_period_data['total_in_kgce'] / 1000, 2) |
234
|
|
|
ws['D8'].border = f_border |
235
|
|
|
|
236
|
|
|
ws['D9'].font = name_font |
237
|
|
|
ws['D9'].alignment = c_c_alignment |
238
|
|
|
ws['D9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
239
|
|
|
if reporting_period_data['increment_rate'] is not None else "-" |
240
|
|
|
ws['D9'].border = f_border |
241
|
|
|
|
242
|
|
|
# TCO2E |
243
|
|
|
ws['E7'].fill = table_fill |
244
|
|
|
ws['E7'].font = name_font |
245
|
|
|
ws['E7'].alignment = c_c_alignment |
246
|
|
|
ws['E7'] = _('Ton of Carbon Dioxide Emissions') + '(TCO2E)' |
247
|
|
|
ws['E7'].border = f_border |
248
|
|
|
|
249
|
|
|
ws['E8'].font = name_font |
250
|
|
|
ws['E8'].alignment = c_c_alignment |
251
|
|
|
ws['E8'] = round(reporting_period_data['total_in_kgco2e'] / 1000, 2) |
252
|
|
|
ws['E8'].border = f_border |
253
|
|
|
|
254
|
|
|
ws['E9'].font = name_font |
255
|
|
|
ws['E9'].alignment = c_c_alignment |
256
|
|
|
ws['E9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
257
|
|
|
if reporting_period_data['increment_rate'] is not None else "-" |
258
|
|
|
ws['E9'].border = f_border |
259
|
|
|
|
260
|
|
|
#################################################################################################################### |
261
|
|
|
# Second: Detailed Data |
262
|
|
|
# 11: title |
263
|
|
|
# 12 ~ 16: chart |
264
|
|
|
# 18: table title |
265
|
|
|
# 19~43: table_data |
266
|
|
|
#################################################################################################################### |
267
|
|
|
if "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
268
|
|
|
for i in range(11, 43 + 1): |
269
|
|
|
ws.row_dimensions[i].height = 0.0 |
270
|
|
|
else: |
271
|
|
|
reporting_period_data = report['reporting_period'] |
272
|
|
|
parameters_names_len = len(report['parameters']['names']) |
273
|
|
|
parameters_parameters_datas_len = parameters_names_len |
274
|
|
|
start_detail_data_row_num = 13 + (parameters_parameters_datas_len + 1) * 6 |
275
|
|
|
ws['B11'].font = title_font |
276
|
|
|
ws['B11'] = name + _('Detailed Data') |
277
|
|
|
|
278
|
|
|
ws.row_dimensions[start_detail_data_row_num].height = 60 |
279
|
|
|
|
280
|
|
|
if not is_base_period_timestamp_exists_flag: |
281
|
|
|
ws['B' + str(start_detail_data_row_num)].fill = table_fill |
282
|
|
|
ws['B' + str(start_detail_data_row_num)].font = title_font |
283
|
|
|
ws['B' + str(start_detail_data_row_num)].border = f_border |
284
|
|
|
ws['B' + str(start_detail_data_row_num)].alignment = c_c_alignment |
285
|
|
|
ws['B' + str(start_detail_data_row_num)] = _('Datetime') |
286
|
|
|
else: |
287
|
|
|
ws['B' + str(start_detail_data_row_num)].fill = table_fill |
288
|
|
|
ws['B' + str(start_detail_data_row_num)].font = title_font |
289
|
|
|
ws['B' + str(start_detail_data_row_num)].border = f_border |
290
|
|
|
ws['B' + str(start_detail_data_row_num)].alignment = c_c_alignment |
291
|
|
|
ws['B' + str(start_detail_data_row_num)] = _('Base Period') + ' - ' + _('Datetime') |
292
|
|
|
|
293
|
|
|
ws['D' + str(start_detail_data_row_num)].fill = table_fill |
294
|
|
|
ws['D' + str(start_detail_data_row_num)].font = title_font |
295
|
|
|
ws['D' + str(start_detail_data_row_num)].border = f_border |
296
|
|
|
ws['D' + str(start_detail_data_row_num)].alignment = c_c_alignment |
297
|
|
|
ws['D' + str(start_detail_data_row_num)] = _('Reporting Period') + ' - ' + _('Datetime') |
298
|
|
|
|
299
|
|
|
has_data = False |
300
|
|
|
max_row = 0 |
301
|
|
|
if len(reporting_period_data['timestamps']) > 0: |
302
|
|
|
has_data = True |
303
|
|
|
max_row = start_detail_data_row_num + len(reporting_period_data['timestamps']) |
304
|
|
|
|
305
|
|
|
if has_data: |
306
|
|
|
if not is_base_period_timestamp_exists_flag: |
307
|
|
|
for i in range(0, len(reporting_period_data['timestamps'])): |
308
|
|
|
col = 'B' |
309
|
|
|
row = str(start_detail_data_row_num + 1 + i) |
310
|
|
|
# col = chr(ord('B') + i) |
311
|
|
|
ws[col + row].font = title_font |
312
|
|
|
ws[col + row].alignment = c_c_alignment |
313
|
|
|
ws[col + row] = reporting_period_data['timestamps'][i] |
314
|
|
|
ws[col + row].border = f_border |
315
|
|
|
else: |
316
|
|
|
for i in range(0, len(reporting_period_data['timestamps'])): |
317
|
|
|
col = 'B' |
318
|
|
|
row = str(start_detail_data_row_num + 1 + i) |
319
|
|
|
# col = chr(ord('B') + i) |
320
|
|
|
ws[col + row].font = title_font |
321
|
|
|
ws[col + row].alignment = c_c_alignment |
322
|
|
|
ws[col + row] = report['base_period']['timestamps'][i] \ |
323
|
|
|
if i < len(report['base_period']['timestamps']) else None |
324
|
|
|
ws[col + row].border = f_border |
325
|
|
|
|
326
|
|
|
col = 'D' |
327
|
|
|
row = str(start_detail_data_row_num + 1 + i) |
328
|
|
|
# col = chr(ord('B') + i) |
329
|
|
|
ws[col + row].font = title_font |
330
|
|
|
ws[col + row].alignment = c_c_alignment |
331
|
|
|
ws[col + row] = reporting_period_data['timestamps'][i] |
332
|
|
|
ws[col + row].border = f_border |
333
|
|
|
|
334
|
|
|
if not is_base_period_timestamp_exists_flag: |
335
|
|
|
ws['C' + str(start_detail_data_row_num)].fill = table_fill |
336
|
|
|
ws['C' + str(start_detail_data_row_num)].font = title_font |
337
|
|
|
ws['C' + str(start_detail_data_row_num)].alignment = c_c_alignment |
338
|
|
|
ws['C' + str(start_detail_data_row_num)] = report['meter']['energy_category_name'] + \ |
339
|
|
|
" (" + report['meter']['unit_of_measure'] + ")" |
340
|
|
|
ws['C' + str(start_detail_data_row_num)].border = f_border |
341
|
|
|
else: |
342
|
|
|
ws['C' + str(start_detail_data_row_num)].fill = table_fill |
343
|
|
|
ws['C' + str(start_detail_data_row_num)].font = title_font |
344
|
|
|
ws['C' + str(start_detail_data_row_num)].alignment = c_c_alignment |
345
|
|
|
ws['C' + str(start_detail_data_row_num)] = _('Base Period') + ' - ' + \ |
346
|
|
|
report['meter']['energy_category_name'] + " (" + report['meter']['unit_of_measure'] + ")" |
347
|
|
|
ws['C' + str(start_detail_data_row_num)].border = f_border |
348
|
|
|
|
349
|
|
|
ws['E' + str(start_detail_data_row_num)].fill = table_fill |
350
|
|
|
ws['E' + str(start_detail_data_row_num)].font = title_font |
351
|
|
|
ws['E' + str(start_detail_data_row_num)].alignment = c_c_alignment |
352
|
|
|
ws['E' + str(start_detail_data_row_num)] = _('Reporting Period') + ' - ' + \ |
353
|
|
|
report['meter']['energy_category_name'] + " (" + report['meter']['unit_of_measure'] + ")" |
354
|
|
|
ws['E' + str(start_detail_data_row_num)].border = f_border |
355
|
|
|
|
356
|
|
|
# 13 data |
357
|
|
|
if not is_base_period_timestamp_exists_flag: |
358
|
|
|
for j in range(0, len(reporting_period_data['timestamps'])): |
359
|
|
|
row = str(start_detail_data_row_num + 1 + j) |
360
|
|
|
ws['C' + row].font = title_font |
361
|
|
|
ws['C' + row].alignment = c_c_alignment |
362
|
|
|
ws['C' + row] = round(reporting_period_data['values'][j], 2) |
363
|
|
|
ws['C' + row].border = f_border |
364
|
|
|
# line |
365
|
|
|
# 13~: line |
366
|
|
|
line = LineChart() |
367
|
|
|
line.title = _('Reporting Period Consumption') + ' - ' + report['meter']['energy_category_name'] + \ |
368
|
|
|
" (" + report['meter']['unit_of_measure'] + ")" |
369
|
|
|
labels = Reference(ws, min_col=2, min_row=start_detail_data_row_num + 1, max_row=max_row) |
370
|
|
|
line_data = Reference(ws, min_col=3, min_row=start_detail_data_row_num, max_row=max_row) |
371
|
|
|
line.add_data(line_data, titles_from_data=True) |
372
|
|
|
line.set_categories(labels) |
373
|
|
|
line_data = line.series[0] |
374
|
|
|
line_data.marker.symbol = "circle" |
375
|
|
|
line_data.smooth = True |
376
|
|
|
line.x_axis.crosses = 'min' |
377
|
|
|
line.height = 8.25 |
378
|
|
|
line.width = 24 |
379
|
|
|
line.dLbls = DataLabelList() |
380
|
|
|
line.dLbls.dLblPos = 't' |
381
|
|
|
line.dLbls.showVal = True |
382
|
|
|
line.dLbls.showPercent = False |
383
|
|
|
ws.add_chart(line, "B12") |
384
|
|
|
else: |
385
|
|
|
for j in range(0, len(reporting_period_data['timestamps'])): |
386
|
|
|
row = str(start_detail_data_row_num + 1 + j) |
387
|
|
|
ws['C' + row].font = title_font |
388
|
|
|
ws['C' + row].alignment = c_c_alignment |
389
|
|
|
ws['C' + row] = round(report['base_period']['values'][j], 2) \ |
390
|
|
|
if j < len(report['base_period']['values']) else None |
391
|
|
|
ws['C' + row].border = f_border |
392
|
|
|
|
393
|
|
|
ws['E' + row].font = title_font |
394
|
|
|
ws['E' + row].alignment = c_c_alignment |
395
|
|
|
ws['E' + row] = round(reporting_period_data['values'][j], 2) |
396
|
|
|
ws['E' + row].border = f_border |
397
|
|
|
# line |
398
|
|
|
# 13~: line |
399
|
|
|
line = LineChart() |
400
|
|
|
line.title = _('Reporting Period Consumption') + ' - ' + report['meter']['energy_category_name'] + \ |
401
|
|
|
" (" + report['meter']['unit_of_measure'] + ")" |
402
|
|
|
labels = Reference(ws, min_col=4, min_row=start_detail_data_row_num + 1, max_row=max_row) |
403
|
|
|
base_line_data = Reference(ws, min_col=3, min_row=start_detail_data_row_num, max_row=max_row) |
404
|
|
|
reporting_data = Reference(ws, min_col=5, min_row=start_detail_data_row_num, max_row=max_row) |
405
|
|
|
line.add_data(base_line_data, titles_from_data=True) |
406
|
|
|
line.add_data(reporting_data, titles_from_data=True) |
407
|
|
|
line.set_categories(labels) |
408
|
|
|
line_data = line.series[0] |
409
|
|
|
line_data.marker.symbol = "circle" |
410
|
|
|
line_data.smooth = True |
411
|
|
|
line.x_axis.crosses = 'min' |
412
|
|
|
line.height = 8.25 |
413
|
|
|
line.width = 24 |
414
|
|
|
line.dLbls = DataLabelList() |
415
|
|
|
line.dLbls.dLblPos = 't' |
416
|
|
|
line.dLbls.showVal = True |
417
|
|
|
line.dLbls.showPercent = False |
418
|
|
|
ws.add_chart(line, "B12") |
419
|
|
|
|
420
|
|
|
row = str(start_detail_data_row_num + 1 + len(reporting_period_data['timestamps'])) |
421
|
|
|
|
422
|
|
|
if not is_base_period_timestamp_exists_flag: |
423
|
|
|
ws['B' + row].font = title_font |
424
|
|
|
ws['B' + row].alignment = c_c_alignment |
425
|
|
|
ws['B' + row] = _('Total') |
426
|
|
|
ws['B' + row].border = f_border |
427
|
|
|
|
428
|
|
|
ws['C' + row].font = title_font |
429
|
|
|
ws['C' + row].alignment = c_c_alignment |
430
|
|
|
ws['C' + row] = round(reporting_period_data['total_in_category'], 2) |
431
|
|
|
ws['C' + row].border = f_border |
432
|
|
|
else: |
433
|
|
|
ws['B' + row].font = title_font |
434
|
|
|
ws['B' + row].alignment = c_c_alignment |
435
|
|
|
ws['B' + row] = _('Total') |
436
|
|
|
ws['B' + row].border = f_border |
437
|
|
|
|
438
|
|
|
ws['C' + row].font = title_font |
439
|
|
|
ws['C' + row].alignment = c_c_alignment |
440
|
|
|
ws['C' + row] = round(report['base_period']['total_in_category'], 2) |
441
|
|
|
ws['C' + row].border = f_border |
442
|
|
|
|
443
|
|
|
ws['D' + row].font = title_font |
444
|
|
|
ws['D' + row].alignment = c_c_alignment |
445
|
|
|
ws['D' + row] = _('Total') |
446
|
|
|
ws['D' + row].border = f_border |
447
|
|
|
|
448
|
|
|
ws['E' + row].font = title_font |
449
|
|
|
ws['E' + row].alignment = c_c_alignment |
450
|
|
|
ws['E' + row] = round(reporting_period_data['total_in_category'], 2) |
451
|
|
|
ws['E' + row].border = f_border |
452
|
|
|
|
453
|
|
|
########################################## |
454
|
|
|
# 12 is the starting line number of the last line chart in the report period |
455
|
|
|
current_sheet_parameters_row_number = 12 + 1 * 6 |
456
|
|
View Code Duplication |
if 'parameters' not in report.keys() or \ |
|
|
|
|
457
|
|
|
report['parameters'] is None or \ |
458
|
|
|
'names' not in report['parameters'].keys() or \ |
459
|
|
|
report['parameters']['names'] is None or \ |
460
|
|
|
len(report['parameters']['names']) == 0 or \ |
461
|
|
|
'timestamps' not in report['parameters'].keys() or \ |
462
|
|
|
report['parameters']['timestamps'] is None or \ |
463
|
|
|
len(report['parameters']['timestamps']) == 0 or \ |
464
|
|
|
'values' not in report['parameters'].keys() or \ |
465
|
|
|
report['parameters']['values'] is None or \ |
466
|
|
|
len(report['parameters']['values']) == 0 or \ |
467
|
|
|
timestamps_data_all_equal_0(report['parameters']['timestamps']): |
468
|
|
|
pass |
469
|
|
|
else: |
470
|
|
|
################################################################################################################ |
471
|
|
|
# new worksheet |
472
|
|
|
################################################################################################################ |
473
|
|
|
|
474
|
|
|
parameters_data = report['parameters'] |
475
|
|
|
|
476
|
|
|
parameters_names_len = len(parameters_data['names']) |
477
|
|
|
|
478
|
|
|
file_name = (re.sub(r'[^A-Z]', '', ws.title)) + '_' |
479
|
|
|
parameters_ws = wb.create_sheet(file_name + _('Parameters')) |
480
|
|
|
|
481
|
|
|
parameters_timestamps_data_max_len = \ |
482
|
|
|
get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
483
|
|
|
|
484
|
|
|
# Row height |
485
|
|
|
parameters_ws.row_dimensions[1].height = 102 |
486
|
|
|
for i in range(2, 7 + 1): |
487
|
|
|
parameters_ws.row_dimensions[i].height = 42 |
488
|
|
|
|
489
|
|
|
for i in range(8, parameters_timestamps_data_max_len + 10): |
490
|
|
|
parameters_ws.row_dimensions[i].height = 60 |
491
|
|
|
|
492
|
|
|
# Col width |
493
|
|
|
parameters_ws.column_dimensions['A'].width = 1.5 |
494
|
|
|
|
495
|
|
|
parameters_ws.column_dimensions['B'].width = 25.0 |
496
|
|
|
|
497
|
|
|
for i in range(3, 12 + parameters_names_len * 3): |
498
|
|
|
parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
499
|
|
|
|
500
|
|
|
# Img |
501
|
|
|
img = Image("excelexporters/myems.png") |
502
|
|
|
parameters_ws.add_image(img, 'A1') |
503
|
|
|
|
504
|
|
|
# Title |
505
|
|
|
parameters_ws['B3'].alignment = b_r_alignment |
506
|
|
|
parameters_ws['B3'] = _('Name') + ':' |
507
|
|
|
parameters_ws['C3'].border = b_border |
508
|
|
|
parameters_ws['C3'].alignment = b_c_alignment |
509
|
|
|
parameters_ws['C3'] = name |
510
|
|
|
|
511
|
|
|
parameters_ws['D3'].alignment = b_r_alignment |
512
|
|
|
parameters_ws['D3'] = _('Period Type') + ':' |
513
|
|
|
parameters_ws['E3'].border = b_border |
514
|
|
|
parameters_ws['E3'].alignment = b_c_alignment |
515
|
|
|
parameters_ws['E3'] = period_type |
516
|
|
|
|
517
|
|
|
parameters_ws['B4'].alignment = b_r_alignment |
518
|
|
|
parameters_ws['B4'] = _('Reporting Start Datetime') + ':' |
519
|
|
|
parameters_ws['C4'].border = b_border |
520
|
|
|
parameters_ws['C4'].alignment = b_c_alignment |
521
|
|
|
parameters_ws['C4'] = reporting_start_datetime_local |
522
|
|
|
|
523
|
|
|
parameters_ws['D4'].alignment = b_r_alignment |
524
|
|
|
parameters_ws['D4'] = _('Reporting End Datetime') + ':' |
525
|
|
|
parameters_ws['E4'].border = b_border |
526
|
|
|
parameters_ws['E4'].alignment = b_c_alignment |
527
|
|
|
parameters_ws['E4'] = reporting_end_datetime_local |
528
|
|
|
|
529
|
|
|
parameters_ws_current_row_number = 6 |
530
|
|
|
|
531
|
|
|
parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
532
|
|
|
parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' ' + _('Parameters') |
533
|
|
|
|
534
|
|
|
parameters_ws_current_row_number += 1 |
535
|
|
|
|
536
|
|
|
parameters_table_start_row_number = parameters_ws_current_row_number |
537
|
|
|
|
538
|
|
|
parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
539
|
|
|
|
540
|
|
|
parameters_ws_current_row_number += 1 |
541
|
|
|
|
542
|
|
|
table_current_col_number = 2 |
543
|
|
|
|
544
|
|
|
for i in range(0, parameters_names_len): |
545
|
|
|
col = format_cell.get_column_letter(table_current_col_number) |
546
|
|
|
|
547
|
|
|
parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
548
|
|
|
col = format_cell.get_column_letter(table_current_col_number + 1) |
549
|
|
|
|
550
|
|
|
parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
551
|
|
|
parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = name_font |
552
|
|
|
parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = c_c_alignment |
553
|
|
|
parameters_ws[col + str(parameters_ws_current_row_number - 1)] = parameters_data['names'][i] |
554
|
|
|
|
555
|
|
|
table_current_col_number = table_current_col_number + 3 |
556
|
|
|
|
557
|
|
|
################################################################################################################ |
558
|
|
|
# Optimized insert parameter data |
559
|
|
|
################################################################################################################ |
560
|
|
|
|
561
|
|
|
timestamps_list = parameters_data['timestamps'] |
562
|
|
|
values_list = parameters_data['values'] |
563
|
|
|
|
564
|
|
|
timestamps_data_temp_save_start_row = parameters_timestamps_data_max_len + 10 |
565
|
|
|
|
566
|
|
|
values_data_temp_save_start_row = parameters_timestamps_data_max_len * 2 + 10 + 1 |
567
|
|
|
|
568
|
|
|
parameters_ws["A" + str(timestamps_data_temp_save_start_row)] = "" |
569
|
|
|
for i in range(parameters_timestamps_data_max_len): |
570
|
|
|
temp_list = [] |
571
|
|
|
for j in range(len(timestamps_list)): |
572
|
|
|
try: |
573
|
|
|
temp_list.append(timestamps_list[j][i]) |
574
|
|
|
except IndexError: |
575
|
|
|
temp_list.append("") |
576
|
|
|
parameters_ws.append(temp_list) |
577
|
|
|
|
578
|
|
|
parameters_ws["A" + str(values_data_temp_save_start_row)] = "" |
579
|
|
|
for i in range(parameters_timestamps_data_max_len): |
580
|
|
|
temp_list = [] |
581
|
|
|
for j in range(len(values_list)): |
582
|
|
|
try: |
583
|
|
|
temp_list.append(values_list[j][i]) |
584
|
|
|
except IndexError: |
585
|
|
|
temp_list.append("") |
586
|
|
|
parameters_ws.append(temp_list) |
587
|
|
|
|
588
|
|
|
parameter_current_col_number = 1 |
589
|
|
|
|
590
|
|
|
for i in range(len(timestamps_list)): |
591
|
|
|
col = format_cell.get_column_letter(parameter_current_col_number) |
592
|
|
|
parameters_ws.move_range( |
593
|
|
|
"{}{}:{}{}".format(col, timestamps_data_temp_save_start_row + 1, col, |
594
|
|
|
timestamps_data_temp_save_start_row + parameters_timestamps_data_max_len), |
595
|
|
|
(- timestamps_data_temp_save_start_row + (parameters_ws_current_row_number - 1)), (i * 2) + 1) |
596
|
|
|
parameters_ws.move_range( |
597
|
|
|
"{}{}:{}{}".format(col, values_data_temp_save_start_row + 1, col, |
598
|
|
|
values_data_temp_save_start_row + parameters_timestamps_data_max_len), |
599
|
|
|
(- values_data_temp_save_start_row + (parameters_ws_current_row_number - 1)), (i * 2) + 2) |
600
|
|
|
|
601
|
|
|
parameter_current_col_number += 1 |
602
|
|
|
|
603
|
|
|
################################################################################################################ |
604
|
|
|
# parameters chart and parameters table |
605
|
|
|
################################################################################################################ |
606
|
|
|
|
607
|
|
|
ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
608
|
|
|
ws['B' + str(current_sheet_parameters_row_number)] = name + ' ' + _('Parameters') |
609
|
|
|
|
610
|
|
|
current_sheet_parameters_row_number += 1 |
611
|
|
|
|
612
|
|
|
chart_start_row_number = current_sheet_parameters_row_number |
613
|
|
|
|
614
|
|
|
col_index = 0 |
615
|
|
|
|
616
|
|
|
for i in range(0, parameters_names_len): |
617
|
|
|
line = LineChart() |
618
|
|
|
data_col = 3 + col_index * 3 |
619
|
|
|
labels_col = 2 + col_index * 3 |
620
|
|
|
col_index += 1 |
621
|
|
|
line.title = _('Parameters') + ' - ' + \ |
622
|
|
|
parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
623
|
|
|
labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
624
|
|
|
max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
625
|
|
|
line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
626
|
|
|
max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
627
|
|
|
line.add_data(line_data, titles_from_data=True) |
628
|
|
|
line.set_categories(labels) |
629
|
|
|
line_data = line.series[0] |
630
|
|
|
line_data.marker.symbol = "circle" |
631
|
|
|
line_data.smooth = True |
632
|
|
|
line.x_axis.crosses = 'min' |
633
|
|
|
line.height = 8.25 |
634
|
|
|
line.width = 24 |
635
|
|
|
line.dLbls = DataLabelList() |
636
|
|
|
line.dLbls.dLblPos = 't' |
637
|
|
|
line.dLbls.showVal = False |
638
|
|
|
line.dLbls.showPercent = False |
639
|
|
|
chart_col = 'B' |
640
|
|
|
chart_cell = chart_col + str(chart_start_row_number) |
641
|
|
|
chart_start_row_number += 6 |
642
|
|
|
ws.add_chart(line, chart_cell) |
643
|
|
|
|
644
|
|
|
current_sheet_parameters_row_number = chart_start_row_number |
645
|
|
|
|
646
|
|
|
current_sheet_parameters_row_number += 1 |
647
|
|
|
|
648
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
649
|
|
|
wb.save(filename) |
650
|
|
|
|
651
|
|
|
return filename |
652
|
|
|
|
653
|
|
|
|
654
|
|
|
def timestamps_data_all_equal_0(lists): |
655
|
|
|
for i, value in enumerate(list(lists)): |
656
|
|
|
if len(value) > 0: |
657
|
|
|
return False |
658
|
|
|
|
659
|
|
|
return True |
660
|
|
|
|
661
|
|
|
|
662
|
|
|
def get_parameters_timestamps_lists_max_len(parameters_timestamps_lists): |
663
|
|
|
max_len = 0 |
664
|
|
|
for i, value in enumerate(list(parameters_timestamps_lists)): |
665
|
|
|
if len(value) > max_len: |
666
|
|
|
max_len = len(value) |
667
|
|
|
|
668
|
|
|
return max_len |
669
|
|
|
|
670
|
|
|
|
671
|
|
|
def timestamps_data_not_equal_0(lists): |
672
|
|
|
number = 0 |
673
|
|
|
for i, value in enumerate(list(lists)): |
674
|
|
|
if len(value) > 0: |
675
|
|
|
number += 1 |
676
|
|
|
return number |
677
|
|
|
|
678
|
|
|
|
679
|
|
View Code Duplication |
def is_base_period_timestamp_exists(base_period_data): |
|
|
|
|
680
|
|
|
timestamps = base_period_data['timestamps'] |
681
|
|
|
|
682
|
|
|
if len(timestamps) == 0: |
683
|
|
|
return False |
684
|
|
|
|
685
|
|
|
for timestamp in timestamps: |
686
|
|
|
if len(timestamp) > 0: |
687
|
|
|
return True |
688
|
|
|
|
689
|
|
|
return False |
690
|
|
|
|