1
|
|
|
import base64 |
2
|
|
|
import uuid |
3
|
|
|
import os |
4
|
|
|
from openpyxl.chart import ( |
5
|
|
|
BarChart, |
6
|
|
|
LineChart, |
7
|
|
|
Reference, |
8
|
|
|
Series |
9
|
|
|
) |
10
|
|
|
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
11
|
|
|
from openpyxl.drawing.image import Image |
12
|
|
|
from openpyxl import Workbook |
13
|
|
|
from openpyxl.chart.label import DataLabelList |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
#################################################################################################################### |
17
|
|
|
# PROCEDURES |
18
|
|
|
# Step 1: Validate the report data |
19
|
|
|
# Step 2: Generate excelexporters file |
20
|
|
|
# Step 3: Encode the excelexporters file to Base64 |
21
|
|
|
#################################################################################################################### |
22
|
|
|
|
23
|
|
View Code Duplication |
def export(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type): |
|
|
|
|
24
|
|
|
#################################################################################################################### |
25
|
|
|
# Step 1: Validate the report data |
26
|
|
|
#################################################################################################################### |
27
|
|
|
if report is None: |
28
|
|
|
return None |
29
|
|
|
|
30
|
|
|
if "reporting_period" not in report.keys() or \ |
31
|
|
|
"values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
32
|
|
|
return None |
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
|
|
|
try: |
45
|
|
|
with open(filename, 'rb') as binary_file: |
46
|
|
|
binary_file_data = binary_file.read() |
47
|
|
|
except IOError as ex: |
48
|
|
|
pass |
49
|
|
|
|
50
|
|
|
# Base64 encode the bytes |
51
|
|
|
base64_encoded_data = base64.b64encode(binary_file_data) |
|
|
|
|
52
|
|
|
# get the Base64 encoded data using human-readable characters. |
53
|
|
|
base64_message = base64_encoded_data.decode('utf-8') |
54
|
|
|
# delete the file from server |
55
|
|
|
try: |
56
|
|
|
os.remove(filename) |
57
|
|
|
except NotImplementedError as ex: |
58
|
|
|
pass |
59
|
|
|
return base64_message |
60
|
|
|
|
61
|
|
|
|
62
|
|
View Code Duplication |
def generate_excel(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type): |
|
|
|
|
63
|
|
|
wb = Workbook() |
64
|
|
|
|
65
|
|
|
# todo |
66
|
|
|
ws = wb.active |
67
|
|
|
|
68
|
|
|
# Row height |
69
|
|
|
ws.row_dimensions[1].height = 102 |
70
|
|
|
for i in range(2, 2000 + 1): |
71
|
|
|
ws.row_dimensions[i].height = 42 |
72
|
|
|
|
73
|
|
|
# Col width |
74
|
|
|
ws.column_dimensions['A'].width = 1.5 |
75
|
|
|
|
76
|
|
|
ws.column_dimensions['B'].width = 25.0 |
77
|
|
|
|
78
|
|
|
for i in range(ord('C'), ord('L')): |
79
|
|
|
ws.column_dimensions[chr(i)].width = 15.0 |
80
|
|
|
|
81
|
|
|
# Font |
82
|
|
|
name_font = Font(name='Constantia', size=15, bold=True) |
83
|
|
|
title_font = Font(name='宋体', size=15, bold=True) |
84
|
|
|
data_font = Font(name='Franklin Gothic Book', size=11) |
85
|
|
|
|
86
|
|
|
table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
87
|
|
|
f_border = Border(left=Side(border_style='medium', color='00000000'), |
88
|
|
|
right=Side(border_style='medium', color='00000000'), |
89
|
|
|
bottom=Side(border_style='medium', color='00000000'), |
90
|
|
|
top=Side(border_style='medium', color='00000000') |
91
|
|
|
) |
92
|
|
|
b_border = Border( |
93
|
|
|
bottom=Side(border_style='medium', color='00000000'), |
94
|
|
|
) |
95
|
|
|
|
96
|
|
|
b_c_alignment = Alignment(vertical='bottom', |
97
|
|
|
horizontal='center', |
98
|
|
|
text_rotation=0, |
99
|
|
|
wrap_text=True, |
100
|
|
|
shrink_to_fit=False, |
101
|
|
|
indent=0) |
102
|
|
|
c_c_alignment = Alignment(vertical='center', |
103
|
|
|
horizontal='center', |
104
|
|
|
text_rotation=0, |
105
|
|
|
wrap_text=True, |
106
|
|
|
shrink_to_fit=False, |
107
|
|
|
indent=0) |
108
|
|
|
b_r_alignment = Alignment(vertical='bottom', |
109
|
|
|
horizontal='right', |
110
|
|
|
text_rotation=0, |
111
|
|
|
wrap_text=True, |
112
|
|
|
shrink_to_fit=False, |
113
|
|
|
indent=0) |
114
|
|
|
c_r_alignment = Alignment(vertical='bottom', |
115
|
|
|
horizontal='center', |
116
|
|
|
text_rotation=0, |
117
|
|
|
wrap_text=True, |
118
|
|
|
shrink_to_fit=False, |
119
|
|
|
indent=0) |
120
|
|
|
|
121
|
|
|
# Img |
122
|
|
|
img = Image("excelexporters/myems.png") |
123
|
|
|
img.width = img.width * 0.85 |
124
|
|
|
img.height = img.height * 0.85 |
125
|
|
|
# img = Image("myems.png") |
126
|
|
|
ws.add_image(img, 'B1') |
127
|
|
|
|
128
|
|
|
# Title |
129
|
|
|
ws.row_dimensions[3].height = 60 |
130
|
|
|
|
131
|
|
|
ws['B3'].font = name_font |
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'].font = name_font |
137
|
|
|
ws['C3'] = name |
138
|
|
|
|
139
|
|
|
ws['D3'].font = name_font |
140
|
|
|
ws['D3'].alignment = b_r_alignment |
141
|
|
|
ws['D3'] = 'Period:' |
142
|
|
|
ws['E3'].border = b_border |
143
|
|
|
ws['E3'].alignment = b_c_alignment |
144
|
|
|
ws['E3'].font = name_font |
145
|
|
|
ws['E3'] = period_type |
146
|
|
|
|
147
|
|
|
ws['F3'].font = name_font |
148
|
|
|
ws['F3'].alignment = b_r_alignment |
149
|
|
|
ws['F3'] = 'Date:' |
150
|
|
|
ws['G3'].border = b_border |
151
|
|
|
ws['G3'].alignment = b_c_alignment |
152
|
|
|
ws['G3'].font = name_font |
153
|
|
|
ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
154
|
|
|
ws.merge_cells("G3:H3") |
155
|
|
|
|
156
|
|
|
if "reporting_period" not in report.keys() or \ |
157
|
|
|
"values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
158
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
|
|
|
|
159
|
|
|
wb.save(filename) |
160
|
|
|
|
161
|
|
|
return filename |
162
|
|
|
|
163
|
|
|
############################### |
164
|
|
|
|
165
|
|
|
has_cost_data_flag = True |
166
|
|
|
|
167
|
|
|
if "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
168
|
|
|
has_cost_data_flag = False |
169
|
|
|
|
170
|
|
|
if has_cost_data_flag: |
171
|
|
|
ws['B6'].font = title_font |
172
|
|
|
ws['B6'] = name + '报告期成本' |
173
|
|
|
|
174
|
|
|
reporting_period_data = report['reporting_period'] |
175
|
|
|
category = report['offline_meter']['energy_category_name'] |
176
|
|
|
ca_len = len(category) |
177
|
|
|
|
178
|
|
|
ws.row_dimensions[7].height = 60 |
179
|
|
|
ws['B7'].fill = table_fill |
180
|
|
|
ws['B7'].border = f_border |
181
|
|
|
|
182
|
|
|
ws['B8'].font = title_font |
183
|
|
|
ws['B8'].alignment = c_c_alignment |
184
|
|
|
ws['B8'] = '成本' |
185
|
|
|
ws['B8'].border = f_border |
186
|
|
|
|
187
|
|
|
ws['B9'].font = title_font |
188
|
|
|
ws['B9'].alignment = c_c_alignment |
189
|
|
|
ws['B9'] = '环比' |
190
|
|
|
ws['B9'].border = f_border |
191
|
|
|
|
192
|
|
|
col = 'B' |
193
|
|
|
|
194
|
|
|
for i in range(0, ca_len): |
195
|
|
|
col = chr(ord('C') + i) |
196
|
|
|
|
197
|
|
|
ws[col + '7'].fill = table_fill |
198
|
|
|
ws[col + '7'].font = name_font |
199
|
|
|
ws[col + '7'].alignment = c_c_alignment |
200
|
|
|
ws[col + '7'] = report['offline_meter']['energy_category_name'] + \ |
201
|
|
|
" (" + report['offline_meter']['unit_of_measure'] + ")" |
202
|
|
|
ws[col + '7'].border = f_border |
203
|
|
|
|
204
|
|
|
ws[col + '8'].font = name_font |
205
|
|
|
ws[col + '8'].alignment = c_c_alignment |
206
|
|
|
ws[col + '8'] = round(reporting_period_data['total_in_category'], 2) |
207
|
|
|
ws[col + '8'].border = f_border |
208
|
|
|
|
209
|
|
|
ws[col + '9'].font = name_font |
210
|
|
|
ws[col + '9'].alignment = c_c_alignment |
211
|
|
|
ws[col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
212
|
|
|
if reporting_period_data['increment_rate'] is not None else "-" |
213
|
|
|
ws[col + '9'].border = f_border |
214
|
|
|
|
215
|
|
|
# TCE TCO2E |
216
|
|
|
end_col = col |
217
|
|
|
# TCE |
218
|
|
|
tce_col = chr(ord(end_col) + 1) |
219
|
|
|
ws[tce_col + '7'].fill = table_fill |
220
|
|
|
ws[tce_col + '7'].font = name_font |
221
|
|
|
ws[tce_col + '7'].alignment = c_c_alignment |
222
|
|
|
ws[tce_col + '7'] = "吨标准煤 (TCE)" |
223
|
|
|
ws[tce_col + '7'].border = f_border |
224
|
|
|
|
225
|
|
|
ws[tce_col + '8'].font = name_font |
226
|
|
|
ws[tce_col + '8'].alignment = c_c_alignment |
227
|
|
|
ws[tce_col + '8'] = round(reporting_period_data['total_in_kgce'] / 1000, 2) |
228
|
|
|
ws[tce_col + '8'].border = f_border |
229
|
|
|
|
230
|
|
|
ws[tce_col + '9'].font = name_font |
231
|
|
|
ws[tce_col + '9'].alignment = c_c_alignment |
232
|
|
|
ws[tce_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
233
|
|
|
if reporting_period_data['increment_rate'] is not None else "-" |
234
|
|
|
ws[tce_col + '9'].border = f_border |
235
|
|
|
|
236
|
|
|
# TCO2E |
237
|
|
|
tco2e_col = chr(ord(end_col) + 2) |
238
|
|
|
ws[tco2e_col + '7'].fill = table_fill |
239
|
|
|
ws[tco2e_col + '7'].font = name_font |
240
|
|
|
ws[tco2e_col + '7'].alignment = c_c_alignment |
241
|
|
|
ws[tco2e_col + '7'] = "吨二氧化碳排放 (TCO2E)" |
242
|
|
|
ws[tco2e_col + '7'].border = f_border |
243
|
|
|
|
244
|
|
|
ws[tco2e_col + '8'].font = name_font |
245
|
|
|
ws[tco2e_col + '8'].alignment = c_c_alignment |
246
|
|
|
ws[tco2e_col + '8'] = round(reporting_period_data['total_in_kgco2e'] / 1000, 2) |
247
|
|
|
ws[tco2e_col + '8'].border = f_border |
248
|
|
|
|
249
|
|
|
ws[tco2e_col + '9'].font = name_font |
250
|
|
|
ws[tco2e_col + '9'].alignment = c_c_alignment |
251
|
|
|
ws[tco2e_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
252
|
|
|
if reporting_period_data['increment_rate'] is not None else "-" |
253
|
|
|
ws[tco2e_col + '9'].border = f_border |
254
|
|
|
|
255
|
|
|
else: |
256
|
|
|
for i in range(6, 9 + 1): |
257
|
|
|
ws.rows_dimensions[i].height = 0.1 |
258
|
|
|
|
259
|
|
|
###################################### |
260
|
|
|
|
261
|
|
|
has_cost_datail_flag = True |
262
|
|
|
reporting_period_data = report['reporting_period'] |
263
|
|
|
category = report['offline_meter']['energy_category_name'] |
264
|
|
|
ca_len = len(category) |
265
|
|
|
times = reporting_period_data['timestamps'] |
266
|
|
|
|
267
|
|
|
if "values" not in reporting_period_data.keys() or len(reporting_period_data['values']) == 0: |
268
|
|
|
has_cost_datail_flag = False |
269
|
|
|
|
270
|
|
|
if has_cost_datail_flag: |
271
|
|
|
ws['B11'].font = title_font |
272
|
|
|
ws['B11'] = name + '详细数据' |
273
|
|
|
|
274
|
|
|
ws.row_dimensions[18].height = 60 |
275
|
|
|
ws['B18'].fill = table_fill |
276
|
|
|
ws['B18'].font = title_font |
277
|
|
|
ws['B18'].border = f_border |
278
|
|
|
ws['B18'].alignment = c_c_alignment |
279
|
|
|
ws['B18'] = '日期时间' |
280
|
|
|
time = times |
281
|
|
|
has_data = False |
282
|
|
|
max_row = 0 |
283
|
|
|
if len(time) > 0: |
284
|
|
|
has_data = True |
285
|
|
|
max_row = 18 + len(time) |
286
|
|
|
|
287
|
|
|
if has_data: |
288
|
|
|
|
289
|
|
|
end_data_row_number = 19 |
290
|
|
|
|
291
|
|
|
for i in range(0, len(time)): |
292
|
|
|
col = 'B' |
293
|
|
|
end_data_row_number = 19 + i |
294
|
|
|
row = str(end_data_row_number) |
295
|
|
|
|
296
|
|
|
ws[col + row].font = title_font |
297
|
|
|
ws[col + row].alignment = c_c_alignment |
298
|
|
|
ws[col + row] = time[i] |
299
|
|
|
ws[col + row].border = f_border |
300
|
|
|
|
301
|
|
|
ws['B' + str(end_data_row_number + 1)].font = title_font |
302
|
|
|
ws['B' + str(end_data_row_number + 1)].alignment = c_c_alignment |
303
|
|
|
ws['B' + str(end_data_row_number + 1)] = '总计' |
304
|
|
|
ws['B' + str(end_data_row_number + 1)].border = f_border |
305
|
|
|
|
306
|
|
|
for i in range(0, ca_len): |
307
|
|
|
|
308
|
|
|
col = chr(ord('C') + i) |
309
|
|
|
|
310
|
|
|
ws[col + '18'].fill = table_fill |
311
|
|
|
ws[col + '18'].font = title_font |
312
|
|
|
ws[col + '18'].alignment = c_c_alignment |
313
|
|
|
ws[col + '18'] = report['offline_meter']['energy_category_name'] + \ |
314
|
|
|
" (" + report['offline_meter']['unit_of_measure'] + ")" |
315
|
|
|
ws[col + '18'].border = f_border |
316
|
|
|
|
317
|
|
|
time = times |
318
|
|
|
time_len = len(time) |
319
|
|
|
|
320
|
|
|
for j in range(0, time_len): |
321
|
|
|
row = str(19 + j) |
322
|
|
|
|
323
|
|
|
ws[col + row].font = title_font |
324
|
|
|
ws[col + row].alignment = c_c_alignment |
325
|
|
|
ws[col + row] = round(reporting_period_data['values'][j], 2) |
326
|
|
|
ws[col + row].border = f_border |
327
|
|
|
|
328
|
|
|
ws[col + str(end_data_row_number + 1)].font = title_font |
329
|
|
|
ws[col + str(end_data_row_number + 1)].alignment = c_c_alignment |
330
|
|
|
ws[col + str(end_data_row_number + 1)] = round(reporting_period_data['total_in_category'], 2) |
331
|
|
|
ws[col + str(end_data_row_number + 1)].border = f_border |
332
|
|
|
|
333
|
|
|
line = LineChart() |
334
|
|
|
line.title = '报告期成本 - ' + report['offline_meter']['energy_category_name'] + \ |
335
|
|
|
" (" + report['offline_meter']['unit_of_measure'] + ")" |
336
|
|
|
line_data = Reference(ws, min_col=3, min_row=18, max_row=max_row) |
337
|
|
|
line.series.append(Series(line_data, title_from_data=True)) |
338
|
|
|
labels = Reference(ws, min_col=2, min_row=19, max_row=max_row) |
339
|
|
|
line.set_categories(labels) |
340
|
|
|
line_data = line.series[0] |
341
|
|
|
line_data.marker.symbol = "circle" |
342
|
|
|
line_data.smooth = True |
343
|
|
|
line.x_axis.crosses = 'min' |
344
|
|
|
line.dLbls = DataLabelList() |
345
|
|
|
line.dLbls.dLblPos = 't' |
346
|
|
|
line.dLbls.showVal = True |
347
|
|
|
line.height = 8.25 |
348
|
|
|
line.width = 24 |
349
|
|
|
ws.add_chart(line, "B12") |
350
|
|
|
else: |
351
|
|
|
for i in range(11, 43 + 1): |
352
|
|
|
ws.row_dimensions[i].height = 0.0 |
353
|
|
|
|
354
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
355
|
|
|
wb.save(filename) |
356
|
|
|
|
357
|
|
|
return filename |
358
|
|
|
|