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