1
|
|
|
import base64 |
2
|
|
|
import uuid |
3
|
|
|
import os |
4
|
|
|
from openpyxl.chart import ( |
5
|
|
|
PieChart, |
6
|
|
|
BarChart, |
7
|
|
|
Reference, |
8
|
|
|
) |
9
|
|
|
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
10
|
|
|
from openpyxl.drawing.image import Image |
11
|
|
|
from openpyxl import Workbook |
12
|
|
|
from openpyxl.chart.label import DataLabelList |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
#################################################################################################################### |
16
|
|
|
# PROCEDURES |
17
|
|
|
# Step 1: Validate the report data |
18
|
|
|
# Step 2: Generate excel file |
19
|
|
|
# Step 3: Encode the excel file bytes to Base64 |
20
|
|
|
#################################################################################################################### |
21
|
|
|
|
22
|
|
|
|
23
|
|
View Code Duplication |
def export(report, |
|
|
|
|
24
|
|
|
name, |
25
|
|
|
reporting_start_datetime_local, |
26
|
|
|
reporting_end_datetime_local, |
27
|
|
|
period_type): |
28
|
|
|
#################################################################################################################### |
29
|
|
|
# Step 1: Validate the report data |
30
|
|
|
#################################################################################################################### |
31
|
|
|
if report is None: |
32
|
|
|
return None |
33
|
|
|
print(report) |
34
|
|
|
|
35
|
|
|
#################################################################################################################### |
36
|
|
|
# Step 2: Generate excel file from the report data |
37
|
|
|
#################################################################################################################### |
38
|
|
|
filename = generate_excel(report, |
39
|
|
|
name, |
40
|
|
|
reporting_start_datetime_local, |
41
|
|
|
reporting_end_datetime_local, |
42
|
|
|
period_type) |
43
|
|
|
#################################################################################################################### |
44
|
|
|
# Step 3: Encode the excel file to Base64 |
45
|
|
|
#################################################################################################################### |
46
|
|
|
try: |
47
|
|
|
with open(filename, 'rb') as binary_file: |
48
|
|
|
binary_file_data = binary_file.read() |
49
|
|
|
except IOError as ex: |
50
|
|
|
pass |
51
|
|
|
|
52
|
|
|
# Base64 encode the bytes |
53
|
|
|
base64_encoded_data = base64.b64encode(binary_file_data) |
|
|
|
|
54
|
|
|
# get the Base64 encoded data using human-readable characters. |
55
|
|
|
base64_message = base64_encoded_data.decode('utf-8') |
56
|
|
|
# delete the file from server |
57
|
|
|
try: |
58
|
|
|
os.remove(filename) |
59
|
|
|
except NotImplementedError as ex: |
60
|
|
|
pass |
61
|
|
|
return base64_message |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
def generate_excel(report, |
65
|
|
|
name, |
66
|
|
|
reporting_start_datetime_local, |
67
|
|
|
reporting_end_datetime_local, |
68
|
|
|
period_type): |
69
|
|
|
wb = Workbook() |
70
|
|
|
ws = wb.active |
71
|
|
|
|
72
|
|
|
# Row height |
73
|
|
|
ws.row_dimensions[1].height = 118 |
74
|
|
|
for i in range(2, 2000 + 1): |
75
|
|
|
ws.row_dimensions[i].height = 30 |
76
|
|
|
|
77
|
|
|
# Col width |
78
|
|
|
ws.column_dimensions['A'].width = 1.5 |
79
|
|
|
|
80
|
|
|
ws.column_dimensions['B'].width = 25.0 |
81
|
|
|
|
82
|
|
|
for i in range(ord('C'), ord('I')): |
83
|
|
|
ws.column_dimensions[chr(i)].width = 25.0 |
84
|
|
|
|
85
|
|
|
# Font |
86
|
|
|
name_font = Font(name='Constantia', size=15, bold=True) |
87
|
|
|
name_small_font = Font(name='Constantia', size=10, bold=True) |
88
|
|
|
title_font = Font(name='宋体', size=15, bold=True) |
89
|
|
|
title_small_font = Font(name='宋体', size=10, bold=True) |
90
|
|
|
data_font = Font(name='Franklin Gothic Book', size=11) |
91
|
|
|
|
92
|
|
|
table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
93
|
|
|
f_border = Border(left=Side(border_style='medium', color='00000000'), |
94
|
|
|
right=Side(border_style='medium', color='00000000'), |
95
|
|
|
bottom=Side(border_style='medium', color='00000000'), |
96
|
|
|
top=Side(border_style='medium', color='00000000') |
97
|
|
|
) |
98
|
|
|
b_border = Border( |
99
|
|
|
bottom=Side(border_style='medium', color='00000000'), |
100
|
|
|
) |
101
|
|
|
|
102
|
|
|
b_c_alignment = Alignment(vertical='bottom', |
103
|
|
|
horizontal='center', |
104
|
|
|
text_rotation=0, |
105
|
|
|
wrap_text=False, |
106
|
|
|
shrink_to_fit=False, |
107
|
|
|
indent=0) |
108
|
|
|
c_c_alignment = Alignment(vertical='center', |
109
|
|
|
horizontal='center', |
110
|
|
|
text_rotation=0, |
111
|
|
|
wrap_text=False, |
112
|
|
|
shrink_to_fit=False, |
113
|
|
|
indent=0) |
114
|
|
|
b_r_alignment = Alignment(vertical='bottom', |
115
|
|
|
horizontal='right', |
116
|
|
|
text_rotation=0, |
117
|
|
|
wrap_text=False, |
118
|
|
|
shrink_to_fit=False, |
119
|
|
|
indent=0) |
120
|
|
|
c_r_alignment = Alignment(vertical='bottom', |
121
|
|
|
horizontal='center', |
122
|
|
|
text_rotation=0, |
123
|
|
|
wrap_text=False, |
124
|
|
|
shrink_to_fit=False, |
125
|
|
|
indent=0) |
126
|
|
|
# Img |
127
|
|
|
img = Image("excelexporters/myems.png") |
128
|
|
|
# img = Image("myems.png") |
129
|
|
|
ws.add_image(img, 'B1') |
130
|
|
|
|
131
|
|
|
# Title |
132
|
|
|
ws['B3'].font = name_font |
133
|
|
|
ws['B3'].alignment = b_r_alignment |
134
|
|
|
ws['B3'] = 'Name:' |
135
|
|
|
ws['C3'].border = b_border |
136
|
|
|
ws['C3'].alignment = b_c_alignment |
137
|
|
|
ws['C3'].font = name_font |
138
|
|
|
ws['C3'] = name |
139
|
|
|
|
140
|
|
|
ws['D3'].font = name_font |
141
|
|
|
ws['D3'].alignment = b_r_alignment |
142
|
|
|
ws['D3'] = 'Period:' |
143
|
|
|
ws['E3'].border = b_border |
144
|
|
|
ws['E3'].alignment = b_c_alignment |
145
|
|
|
ws['E3'].font = name_font |
146
|
|
|
ws['E3'] = period_type |
147
|
|
|
|
148
|
|
|
ws['F3'].font = name_font |
149
|
|
|
ws['F3'].alignment = b_r_alignment |
150
|
|
|
ws['F3'] = 'Date:' |
151
|
|
|
ws.merge_cells("G3:J3") |
152
|
|
|
for i in range(ord('G'), ord('K')): |
153
|
|
|
ws[chr(i) + '3'].border = b_border |
154
|
|
|
ws['G3'].alignment = b_c_alignment |
155
|
|
|
ws['G3'].font = name_font |
156
|
|
|
ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
157
|
|
|
|
158
|
|
|
if "reporting_period" not in report.keys() or \ |
159
|
|
|
"names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
160
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
|
|
|
|
161
|
|
|
wb.save(filename) |
162
|
|
|
|
163
|
|
|
return filename |
164
|
|
|
|
165
|
|
|
################################## |
166
|
|
|
|
167
|
|
|
current_row_number = 6 |
168
|
|
|
|
169
|
|
|
reporting_period_data = report['reporting_period'] |
170
|
|
|
|
171
|
|
|
has_names_data_flag = True |
172
|
|
|
|
173
|
|
|
if "names" not in reporting_period_data.keys() or \ |
174
|
|
|
reporting_period_data['names'] is None or \ |
175
|
|
|
len(reporting_period_data['names']) == 0: |
176
|
|
|
has_names_data_flag = False |
177
|
|
|
|
178
|
|
|
if has_names_data_flag: |
179
|
|
|
ws['B' + str(current_row_number)].font = title_font |
180
|
|
|
ws['B' + str(current_row_number)] = name + ' 报告期节约' |
181
|
|
|
|
182
|
|
|
current_row_number += 1 |
183
|
|
|
|
184
|
|
|
category = reporting_period_data['names'] |
185
|
|
|
ca_len = len(category) |
186
|
|
|
|
187
|
|
|
ws['B' + str(current_row_number)].fill = table_fill |
188
|
|
|
|
189
|
|
|
col = 'C' |
190
|
|
|
|
191
|
|
|
for i in range(0, ca_len): |
192
|
|
|
ws[col + str(current_row_number)].fill = table_fill |
193
|
|
|
ws[col + str(current_row_number)].font = name_small_font |
194
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
195
|
|
|
ws[col + str(current_row_number)].border = f_border |
196
|
|
|
ws[col + str(current_row_number)] = \ |
197
|
|
|
reporting_period_data['names'][i] + " (基线-实际) (" + reporting_period_data['units'][i] + ")" |
198
|
|
|
|
199
|
|
|
col = chr(ord(col) + 1) |
200
|
|
|
|
201
|
|
|
ws[col + str(current_row_number)].fill = table_fill |
202
|
|
|
ws[col + str(current_row_number)].font = name_small_font |
203
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
204
|
|
|
ws[col + str(current_row_number)].border = f_border |
205
|
|
|
ws[col + str(current_row_number)] = '吨标准煤 (基线-实际) (TCE)' |
206
|
|
|
|
207
|
|
|
col = chr(ord(col) + 1) |
208
|
|
|
|
209
|
|
|
ws[col + str(current_row_number)].fill = table_fill |
210
|
|
|
ws[col + str(current_row_number)].font = name_small_font |
211
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
212
|
|
|
ws[col + str(current_row_number)].border = f_border |
213
|
|
|
ws[col + str(current_row_number)] = '吨二氧化碳排放 (基线-实际) (TCO2E)' |
214
|
|
|
|
215
|
|
|
col = chr(ord(col) + 1) |
216
|
|
|
|
217
|
|
|
current_row_number += 1 |
218
|
|
|
|
219
|
|
|
ws['B' + str(current_row_number)].font = title_font |
220
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
221
|
|
|
ws['B' + str(current_row_number)].border = f_border |
222
|
|
|
ws['B' + str(current_row_number)] = '节约' |
223
|
|
|
|
224
|
|
|
col = 'C' |
225
|
|
|
|
226
|
|
|
for i in range(0, ca_len): |
227
|
|
|
ws[col + str(current_row_number)].font = name_font |
228
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
229
|
|
|
ws[col + str(current_row_number)].border = f_border |
230
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['subtotals_saving'][i], 2) |
231
|
|
|
|
232
|
|
|
col = chr(ord(col) + 1) |
233
|
|
|
|
234
|
|
|
ws[col + str(current_row_number)].font = name_font |
235
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
236
|
|
|
ws[col + str(current_row_number)].border = f_border |
237
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgce_saving'], 2) |
238
|
|
|
|
239
|
|
|
col = chr(ord(col) + 1) |
240
|
|
|
|
241
|
|
|
ws[col + str(current_row_number)].font = name_font |
242
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
243
|
|
|
ws[col + str(current_row_number)].border = f_border |
244
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgco2e_saving'], 2) |
245
|
|
|
|
246
|
|
|
col = chr(ord(col) + 1) |
247
|
|
|
|
248
|
|
|
current_row_number += 1 |
249
|
|
|
|
250
|
|
|
ws['B' + str(current_row_number)].font = title_font |
251
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
252
|
|
|
ws['B' + str(current_row_number)].border = f_border |
253
|
|
|
ws['B' + str(current_row_number)] = '单位面积值' |
254
|
|
|
|
255
|
|
|
col = 'C' |
256
|
|
|
|
257
|
|
|
for i in range(0, ca_len): |
258
|
|
|
ws[col + str(current_row_number)].font = name_font |
259
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
260
|
|
|
ws[col + str(current_row_number)].border = f_border |
261
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['subtotals_per_unit_area_saving'][i], 2) |
262
|
|
|
|
263
|
|
|
col = chr(ord(col) + 1) |
264
|
|
|
|
265
|
|
|
ws[col + str(current_row_number)].font = name_font |
266
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
267
|
|
|
ws[col + str(current_row_number)].border = f_border |
268
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgco2e_per_unit_area_saving'], 2) |
269
|
|
|
|
270
|
|
|
col = chr(ord(col) + 1) |
271
|
|
|
|
272
|
|
|
ws[col + str(current_row_number)].font = name_font |
273
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
274
|
|
|
ws[col + str(current_row_number)].border = f_border |
275
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgce_per_unit_area_saving'], 2) |
276
|
|
|
|
277
|
|
|
col = chr(ord(col) + 1) |
278
|
|
|
|
279
|
|
|
ws['B' + str(current_row_number)].font = title_font |
280
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
281
|
|
|
ws['B' + str(current_row_number)].border = f_border |
282
|
|
|
ws['B' + str(current_row_number)] = '环比' |
283
|
|
|
|
284
|
|
|
col = 'C' |
285
|
|
|
|
286
|
|
|
for i in range(0, ca_len): |
287
|
|
|
ws[col + str(current_row_number)].font = name_font |
288
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
289
|
|
|
ws[col + str(current_row_number)].border = f_border |
290
|
|
|
ws[col + str(current_row_number)] = str( |
291
|
|
|
round(reporting_period_data['increment_rates_saving'][i] * 100, 2)) + '%' \ |
292
|
|
|
if reporting_period_data['increment_rates_saving'][i] is not None else '-' |
293
|
|
|
|
294
|
|
|
col = chr(ord(col) + 1) |
295
|
|
|
|
296
|
|
|
ws[col + str(current_row_number)].font = name_font |
297
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
298
|
|
|
ws[col + str(current_row_number)].border = f_border |
299
|
|
|
ws[col + str(current_row_number)] = str( |
300
|
|
|
round(reporting_period_data['increment_rate_in_kgce_saving'] * 100, 2)) + '%' \ |
301
|
|
|
if reporting_period_data['increment_rate_in_kgce_saving'] is not None else '-' |
302
|
|
|
|
303
|
|
|
col = chr(ord(col) + 1) |
304
|
|
|
|
305
|
|
|
ws[col + str(current_row_number)].font = name_font |
306
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
307
|
|
|
ws[col + str(current_row_number)].border = f_border |
308
|
|
|
ws[col + str(current_row_number)] = str( |
309
|
|
|
round(reporting_period_data['increment_rate_in_kgco2e_saving'] * 100, 2)) + '%' \ |
310
|
|
|
if reporting_period_data['increment_rate_in_kgco2e_saving'] is not None else '-' |
311
|
|
|
|
312
|
|
|
col = chr(ord(col) + 1) |
313
|
|
|
|
314
|
|
|
current_row_number += 2 |
315
|
|
|
|
316
|
|
|
ws['B' + str(current_row_number)].font = title_font |
317
|
|
|
ws['B' + str(current_row_number)] = name + ' 吨标准煤(TCE)占比' |
318
|
|
|
|
319
|
|
|
current_row_number += 1 |
320
|
|
|
table_start_row_number = current_row_number |
321
|
|
|
chart_start_row_number = current_row_number |
322
|
|
|
|
323
|
|
|
ws['B' + str(current_row_number)].fill = table_fill |
324
|
|
|
|
325
|
|
|
ws['C' + str(current_row_number)].fill = table_fill |
326
|
|
|
ws['C' + str(current_row_number)].font = name_small_font |
327
|
|
|
ws['C' + str(current_row_number)].alignment = c_c_alignment |
328
|
|
|
ws['C' + str(current_row_number)].border = f_border |
329
|
|
|
ws['C' + str(current_row_number)] = '吨标准煤(TCE)占比' |
330
|
|
|
|
331
|
|
|
current_row_number += 1 |
332
|
|
|
|
333
|
|
|
for i in range(0, ca_len): |
334
|
|
|
ws['B' + str(current_row_number)].font = title_font |
335
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
336
|
|
|
ws['B' + str(current_row_number)].border = f_border |
337
|
|
|
ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
338
|
|
|
|
339
|
|
|
ws['C' + str(current_row_number)].font = name_font |
340
|
|
|
ws['C' + str(current_row_number)].alignment = c_c_alignment |
341
|
|
|
ws['C' + str(current_row_number)].border = f_border |
342
|
|
|
ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals_in_kgce_saving'][i], 2) |
343
|
|
|
|
344
|
|
|
current_row_number += 1 |
345
|
|
|
|
346
|
|
|
table_end_row_number = current_row_number - 1 |
347
|
|
|
|
348
|
|
|
if ca_len < 4: |
349
|
|
|
current_row_number = current_row_number - ca_len + 4 |
350
|
|
|
|
351
|
|
|
current_row_number += 1 |
352
|
|
|
|
353
|
|
|
pie = PieChart() |
354
|
|
|
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
355
|
|
|
pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
356
|
|
|
pie.add_data(pie_data, titles_from_data=True) |
357
|
|
|
pie.set_categories(labels) |
358
|
|
|
pie.height = 5.25 |
359
|
|
|
pie.width = 9 |
360
|
|
|
s1 = pie.series[0] |
361
|
|
|
s1.dLbls = DataLabelList() |
362
|
|
|
s1.dLbls.showCatName = False |
363
|
|
|
s1.dLbls.showVal = True |
364
|
|
|
s1.dLbls.showPercent = True |
365
|
|
|
ws.add_chart(pie, 'D' + str(chart_start_row_number)) |
366
|
|
|
|
367
|
|
|
ws['B' + str(current_row_number)].font = title_font |
368
|
|
|
ws['B' + str(current_row_number)] = name + ' 吨二氧化碳排放(TCO2E)占比' |
369
|
|
|
|
370
|
|
|
current_row_number += 1 |
371
|
|
|
table_start_row_number = current_row_number |
372
|
|
|
chart_start_row_number = current_row_number |
373
|
|
|
|
374
|
|
|
ws['B' + str(current_row_number)].fill = table_fill |
375
|
|
|
|
376
|
|
|
ws['C' + str(current_row_number)].fill = table_fill |
377
|
|
|
ws['C' + str(current_row_number)].font = name_small_font |
378
|
|
|
ws['C' + str(current_row_number)].alignment = c_c_alignment |
379
|
|
|
ws['C' + str(current_row_number)].border = f_border |
380
|
|
|
ws['C' + str(current_row_number)] = '吨二氧化碳排放(TCO2E)占比' |
381
|
|
|
|
382
|
|
|
current_row_number += 1 |
383
|
|
|
|
384
|
|
|
for i in range(0, ca_len): |
385
|
|
|
ws['B' + str(current_row_number)].font = title_font |
386
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
387
|
|
|
ws['B' + str(current_row_number)].border = f_border |
388
|
|
|
ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
389
|
|
|
|
390
|
|
|
ws['C' + str(current_row_number)].font = name_font |
391
|
|
|
ws['C' + str(current_row_number)].alignment = c_c_alignment |
392
|
|
|
ws['C' + str(current_row_number)].border = f_border |
393
|
|
|
ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals_in_kgco2e_saving'][i], 2) |
394
|
|
|
|
395
|
|
|
current_row_number += 1 |
396
|
|
|
|
397
|
|
|
table_end_row_number = current_row_number - 1 |
398
|
|
|
|
399
|
|
|
if ca_len < 4: |
400
|
|
|
current_row_number = current_row_number - ca_len + 4 |
401
|
|
|
|
402
|
|
|
current_row_number += 1 |
403
|
|
|
|
404
|
|
|
pie = PieChart() |
405
|
|
|
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
406
|
|
|
pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
407
|
|
|
pie.add_data(pie_data, titles_from_data=True) |
408
|
|
|
pie.set_categories(labels) |
409
|
|
|
pie.height = 5.25 |
410
|
|
|
pie.width = 9 |
411
|
|
|
s1 = pie.series[0] |
412
|
|
|
s1.dLbls = DataLabelList() |
413
|
|
|
s1.dLbls.showCatName = False |
414
|
|
|
s1.dLbls.showVal = True |
415
|
|
|
s1.dLbls.showPercent = True |
416
|
|
|
ws.add_chart(pie, 'D' + str(chart_start_row_number)) |
417
|
|
|
|
418
|
|
|
############################################# |
419
|
|
|
|
420
|
|
|
has_child_space_data_flag = True |
421
|
|
|
|
422
|
|
|
if 'child_space' not in report.keys() or \ |
423
|
|
|
report['child_space'] is None or \ |
424
|
|
|
'energy_category_names' not in report['child_space'].keys() or \ |
425
|
|
|
report['child_space']['energy_category_names'] is None or \ |
426
|
|
|
len(report['child_space']['energy_category_names']) == 0: |
427
|
|
|
has_child_space_data_flag = False |
428
|
|
|
|
429
|
|
|
if has_child_space_data_flag: |
430
|
|
|
child_space_data = report['child_space'] |
431
|
|
|
ca_len = len(child_space_data['energy_category_names']) |
432
|
|
|
|
433
|
|
|
ws['B' + str(current_row_number)].font = title_font |
434
|
|
|
ws['B' + str(current_row_number)] = name + ' 子空间数据' |
435
|
|
|
|
436
|
|
|
current_row_number += 1 |
437
|
|
|
table_start_row_number = current_row_number |
438
|
|
|
|
439
|
|
|
ws['B' + str(current_row_number)].fill = table_fill |
440
|
|
|
ws['B' + str(current_row_number)].font = name_font |
441
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
442
|
|
|
ws['B' + str(current_row_number)].border = f_border |
443
|
|
|
ws['B' + str(current_row_number)] = '子空间' |
444
|
|
|
|
445
|
|
|
col = 'C' |
446
|
|
|
|
447
|
|
|
for i in range(0, ca_len): |
448
|
|
|
ws[col + str(current_row_number)].fill = table_fill |
449
|
|
|
ws[col + str(current_row_number)].font = name_font |
450
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
451
|
|
|
ws[col + str(current_row_number)].border = f_border |
452
|
|
|
ws[col + str(current_row_number)] = \ |
453
|
|
|
child_space_data['energy_category_names'][i] + " (" + child_space_data['units'][i] + ")" |
454
|
|
|
col = chr(ord(col) + 1) |
455
|
|
|
|
456
|
|
|
current_row_number += 1 |
457
|
|
|
ca_child_len = len(child_space_data['child_space_names_array'][0]) |
458
|
|
|
|
459
|
|
|
for i in range(0, ca_child_len): |
460
|
|
|
ws['B' + str(current_row_number)].font = title_font |
461
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
462
|
|
|
ws['B' + str(current_row_number)].border = f_border |
463
|
|
|
ws['B' + str(current_row_number)] = child_space_data['child_space_names_array'][0][i] |
464
|
|
|
current_row_number += 1 |
465
|
|
|
|
466
|
|
|
current_row_number -= ca_child_len |
467
|
|
|
|
468
|
|
|
for i in range(0, ca_child_len): |
469
|
|
|
col = 'C' |
470
|
|
|
for j in range(0, ca_len): |
471
|
|
|
ws[col + str(current_row_number)].font = name_font |
472
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
473
|
|
|
ws[col + str(current_row_number)].border = f_border |
474
|
|
|
ws[col + str(current_row_number)] = child_space_data['subtotals_saving_array'][j][i] |
475
|
|
|
col = chr(ord(col) + 1) |
476
|
|
|
|
477
|
|
|
current_row_number += 1 |
478
|
|
|
|
479
|
|
|
table_end_row_number = current_row_number - 1 |
480
|
|
|
|
481
|
|
|
col = 'B' |
482
|
|
|
|
483
|
|
|
for i in range(0, ca_len): |
484
|
|
|
pie = PieChart() |
485
|
|
|
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
486
|
|
|
pie_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number) |
487
|
|
|
pie.add_data(pie_data, titles_from_data=True) |
488
|
|
|
pie.set_categories(labels) |
489
|
|
|
pie.title = reporting_period_data['names'][i] + " (" + \ |
490
|
|
|
reporting_period_data['units'][i] + ")" |
491
|
|
|
pie.height = 5.25 |
492
|
|
|
pie.width = 9 |
493
|
|
|
s1 = pie.series[0] |
494
|
|
|
s1.dLbls = DataLabelList() |
495
|
|
|
s1.dLbls.showCatName = False |
496
|
|
|
s1.dLbls.showVal = True |
497
|
|
|
s1.dLbls.showPercent = True |
498
|
|
|
ws.add_chart(pie, col + str(current_row_number)) |
499
|
|
|
col = chr(ord(col) + 2) |
500
|
|
|
|
501
|
|
|
current_row_number += 6 |
502
|
|
|
|
503
|
|
|
################################ |
504
|
|
|
|
505
|
|
|
has_values_saving_data = True |
506
|
|
|
has_timestamps_data = True |
507
|
|
|
|
508
|
|
|
if 'values_saving' not in reporting_period_data.keys() or \ |
509
|
|
|
reporting_period_data['values_saving'] is None or \ |
510
|
|
|
len(reporting_period_data['values_saving']) == 0: |
511
|
|
|
has_values_saving_data = False |
512
|
|
|
|
513
|
|
|
if 'timestamps' not in reporting_period_data.keys() or \ |
514
|
|
|
reporting_period_data['timestamps'] is None or \ |
515
|
|
|
len(reporting_period_data['timestamps']) == 0 or \ |
516
|
|
|
len(reporting_period_data['timestamps'][0]) == 0: |
517
|
|
|
has_timestamps_data = False |
518
|
|
|
|
519
|
|
|
if has_values_saving_data and has_timestamps_data: |
520
|
|
|
ca_len = len(reporting_period_data['names']) |
521
|
|
|
time = reporting_period_data['timestamps'][0] |
522
|
|
|
|
523
|
|
|
ws['B' + str(current_row_number)].font = title_font |
524
|
|
|
ws['B' + str(current_row_number)] = name + ' 详细数据' |
525
|
|
|
|
526
|
|
|
current_row_number += 1 |
527
|
|
|
|
528
|
|
|
chart_start_row_number = current_row_number |
529
|
|
|
|
530
|
|
|
current_row_number += ca_len * 5 |
531
|
|
|
table_start_row_number = current_row_number |
532
|
|
|
|
533
|
|
|
ws['B' + str(current_row_number)].fill = table_fill |
534
|
|
|
ws['B' + str(current_row_number)].font = title_font |
535
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
536
|
|
|
ws['B' + str(current_row_number)].border = f_border |
537
|
|
|
ws['B' + str(current_row_number)] = '日期时间' |
538
|
|
|
|
539
|
|
|
col = 'C' |
540
|
|
|
|
541
|
|
|
for i in range(0, ca_len): |
542
|
|
|
ws[col + str(current_row_number)].fill = table_fill |
543
|
|
|
ws[col + str(current_row_number)].font = title_font |
544
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
545
|
|
|
ws[col + str(current_row_number)].border = f_border |
546
|
|
|
ws[col + str(current_row_number)] = \ |
547
|
|
|
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
548
|
|
|
col = chr(ord(col) + 1) |
549
|
|
|
|
550
|
|
|
current_row_number += 1 |
551
|
|
|
|
552
|
|
|
for i in range(0, len(time)): |
553
|
|
|
ws['B' + str(current_row_number)].font = title_font |
554
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
555
|
|
|
ws['B' + str(current_row_number)].border = f_border |
556
|
|
|
ws['B' + str(current_row_number)] = time[i] |
557
|
|
|
|
558
|
|
|
col = 'C' |
559
|
|
|
for j in range(0, ca_len): |
560
|
|
|
ws[col + str(current_row_number)].font = title_font |
561
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
562
|
|
|
ws[col + str(current_row_number)].border = f_border |
563
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['values_saving'][j][i], 2) \ |
564
|
|
|
if reporting_period_data['values_saving'][j][i] is not None else 0.00 |
565
|
|
|
col = chr(ord(col) + 1) |
566
|
|
|
|
567
|
|
|
current_row_number += 1 |
568
|
|
|
|
569
|
|
|
table_end_row_number = current_row_number - 1 |
570
|
|
|
|
571
|
|
|
for i in range(0, ca_len): |
572
|
|
|
bar = BarChart() |
573
|
|
|
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
574
|
|
|
bar_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number) |
575
|
|
|
bar.add_data(bar_data, titles_from_data=True) |
576
|
|
|
bar.set_categories(labels) |
577
|
|
|
bar.height = 5.25 |
578
|
|
|
bar.width = len(time) |
579
|
|
|
bar.dLbls = DataLabelList() |
580
|
|
|
bar.dLbls.showVal = True |
581
|
|
|
bar.dLbls.showPercent = True |
582
|
|
|
chart_col = 'B' |
583
|
|
|
chart_cell = chart_col + str(chart_start_row_number) |
584
|
|
|
chart_start_row_number += 5 |
585
|
|
|
ws.add_chart(bar, chart_cell) |
586
|
|
|
|
587
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
588
|
|
|
wb.save(filename) |
589
|
|
|
|
590
|
|
|
return filename |
591
|
|
|
|