1
|
|
|
import base64 |
2
|
|
|
import uuid |
3
|
|
|
import os |
4
|
|
|
from openpyxl.chart import ( |
5
|
|
|
PieChart, |
6
|
|
|
LineChart, |
7
|
|
|
BarChart, |
8
|
|
|
Reference, |
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
|
|
|
import openpyxl.utils.cell as format_cell |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
#################################################################################################################### |
18
|
|
|
# PROCEDURES |
19
|
|
|
# Step 1: Validate the report data |
20
|
|
|
# Step 2: Generate excel file |
21
|
|
|
# Step 3: Encode the excel file bytes to Base64 |
22
|
|
|
#################################################################################################################### |
23
|
|
|
|
24
|
|
|
|
25
|
|
View Code Duplication |
def export(report, |
|
|
|
|
26
|
|
|
name, |
27
|
|
|
reporting_start_datetime_local, |
28
|
|
|
reporting_end_datetime_local, |
29
|
|
|
period_type): |
30
|
|
|
#################################################################################################################### |
31
|
|
|
# Step 1: Validate the report data |
32
|
|
|
#################################################################################################################### |
33
|
|
|
if report is None: |
34
|
|
|
return None |
35
|
|
|
print(report) |
36
|
|
|
|
37
|
|
|
#################################################################################################################### |
38
|
|
|
# Step 2: Generate excel file from the report data |
39
|
|
|
#################################################################################################################### |
40
|
|
|
filename = generate_excel(report, |
41
|
|
|
name, |
42
|
|
|
reporting_start_datetime_local, |
43
|
|
|
reporting_end_datetime_local, |
44
|
|
|
period_type) |
45
|
|
|
#################################################################################################################### |
46
|
|
|
# Step 3: Encode the excel file to Base64 |
47
|
|
|
#################################################################################################################### |
48
|
|
|
try: |
49
|
|
|
with open(filename, 'rb') as binary_file: |
50
|
|
|
binary_file_data = binary_file.read() |
51
|
|
|
except IOError as ex: |
52
|
|
|
pass |
53
|
|
|
|
54
|
|
|
# Base64 encode the bytes |
55
|
|
|
base64_encoded_data = base64.b64encode(binary_file_data) |
|
|
|
|
56
|
|
|
# get the Base64 encoded data using human-readable characters. |
57
|
|
|
base64_message = base64_encoded_data.decode('utf-8') |
58
|
|
|
# delete the file from server |
59
|
|
|
try: |
60
|
|
|
os.remove(filename) |
61
|
|
|
except NotImplementedError as ex: |
62
|
|
|
pass |
63
|
|
|
return base64_message |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
def generate_excel(report, |
67
|
|
|
name, |
68
|
|
|
reporting_start_datetime_local, |
69
|
|
|
reporting_end_datetime_local, |
70
|
|
|
period_type): |
71
|
|
|
wb = Workbook() |
72
|
|
|
ws = wb.active |
73
|
|
|
|
74
|
|
|
# Row height |
75
|
|
|
ws.row_dimensions[1].height = 102 |
76
|
|
|
for i in range(2, 2000 + 1): |
77
|
|
|
ws.row_dimensions[i].height = 42 |
78
|
|
|
|
79
|
|
|
# Col width |
80
|
|
|
ws.column_dimensions['A'].width = 1.5 |
81
|
|
|
|
82
|
|
|
ws.column_dimensions['B'].width = 25.0 |
83
|
|
|
|
84
|
|
|
for i in range(ord('C'), ord('L')): |
85
|
|
|
ws.column_dimensions[chr(i)].width = 15.0 |
86
|
|
|
|
87
|
|
|
# Font |
88
|
|
|
name_font = Font(name='Constantia', size=15, bold=True) |
89
|
|
|
title_font = Font(name='宋体', size=15, 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=True, |
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=True, |
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=True, |
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=True, |
124
|
|
|
shrink_to_fit=False, |
125
|
|
|
indent=0) |
126
|
|
|
# Img |
127
|
|
|
img = Image("excelexporters/myems.png") |
128
|
|
|
img.width = img.width * 0.85 |
129
|
|
|
img.height = img.height * 0.85 |
130
|
|
|
# img = Image("myems.png") |
131
|
|
|
ws.add_image(img, 'B1') |
132
|
|
|
|
133
|
|
|
# Title |
134
|
|
|
ws.row_dimensions[3].height = 60 |
135
|
|
|
|
136
|
|
|
ws['B3'].font = name_font |
137
|
|
|
ws['B3'].alignment = b_r_alignment |
138
|
|
|
ws['B3'] = 'Name:' |
139
|
|
|
ws['C3'].border = b_border |
140
|
|
|
ws['C3'].alignment = b_c_alignment |
141
|
|
|
ws['C3'].font = name_font |
142
|
|
|
ws['C3'] = name |
143
|
|
|
|
144
|
|
|
ws['D3'].font = name_font |
145
|
|
|
ws['D3'].alignment = b_r_alignment |
146
|
|
|
ws['D3'] = 'Period:' |
147
|
|
|
ws['E3'].border = b_border |
148
|
|
|
ws['E3'].alignment = b_c_alignment |
149
|
|
|
ws['E3'].font = name_font |
150
|
|
|
ws['E3'] = period_type |
151
|
|
|
|
152
|
|
|
ws['F3'].font = name_font |
153
|
|
|
ws['F3'].alignment = b_r_alignment |
154
|
|
|
ws['F3'] = 'Date:' |
155
|
|
|
ws['G3'].border = b_border |
156
|
|
|
ws['G3'].alignment = b_c_alignment |
157
|
|
|
ws['G3'].font = name_font |
158
|
|
|
ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
159
|
|
|
ws.merge_cells("G3:H3") |
160
|
|
|
|
161
|
|
|
if "reporting_period" not in report.keys() or \ |
162
|
|
|
"names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
163
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
|
|
|
|
164
|
|
|
wb.save(filename) |
165
|
|
|
|
166
|
|
|
return filename |
167
|
|
|
|
168
|
|
|
################################## |
169
|
|
|
|
170
|
|
|
current_row_number = 6 |
171
|
|
|
|
172
|
|
|
reporting_period_data = report['reporting_period'] |
173
|
|
|
|
174
|
|
|
has_names_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_names_data_flag = False |
180
|
|
|
|
181
|
|
|
if has_names_data_flag: |
182
|
|
|
ws['B' + str(current_row_number)].font = title_font |
183
|
|
|
ws['B' + str(current_row_number)] = name + ' 报告期产出' |
184
|
|
|
|
185
|
|
|
current_row_number += 1 |
186
|
|
|
|
187
|
|
|
category = reporting_period_data['names'] |
188
|
|
|
ca_len = len(category) |
189
|
|
|
|
190
|
|
|
ws.row_dimensions[current_row_number].height = 60 |
191
|
|
|
ws['B' + str(current_row_number)].fill = table_fill |
192
|
|
|
ws['B' + str(current_row_number)].border = f_border |
193
|
|
|
|
194
|
|
|
col = 'C' |
195
|
|
|
|
196
|
|
|
for i in range(0, ca_len): |
197
|
|
|
ws[col + str(current_row_number)].fill = table_fill |
198
|
|
|
ws[col + str(current_row_number)].font = name_font |
199
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
200
|
|
|
ws[col + str(current_row_number)].border = f_border |
201
|
|
|
ws[col + str(current_row_number)] = \ |
202
|
|
|
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
203
|
|
|
|
204
|
|
|
col = chr(ord(col) + 1) |
205
|
|
|
|
206
|
|
|
current_row_number += 1 |
207
|
|
|
|
208
|
|
|
ws['B' + str(current_row_number)].font = title_font |
209
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
210
|
|
|
ws['B' + str(current_row_number)].border = f_border |
211
|
|
|
ws['B' + str(current_row_number)] = '产出' |
212
|
|
|
|
213
|
|
|
col = 'C' |
214
|
|
|
|
215
|
|
|
for i in range(0, ca_len): |
216
|
|
|
ws[col + str(current_row_number)].font = name_font |
217
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
218
|
|
|
ws[col + str(current_row_number)].border = f_border |
219
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) |
220
|
|
|
|
221
|
|
|
col = chr(ord(col) + 1) |
222
|
|
|
|
223
|
|
|
current_row_number += 1 |
224
|
|
|
|
225
|
|
|
ws['B' + str(current_row_number)].font = title_font |
226
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
227
|
|
|
ws['B' + str(current_row_number)].border = f_border |
228
|
|
|
ws['B' + str(current_row_number)] = '单位面积值' |
229
|
|
|
|
230
|
|
|
col = 'C' |
231
|
|
|
|
232
|
|
|
for i in range(0, ca_len): |
233
|
|
|
ws[col + str(current_row_number)].font = name_font |
234
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
235
|
|
|
ws[col + str(current_row_number)].border = f_border |
236
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['subtotals_per_unit_area'][i], 2) |
237
|
|
|
|
238
|
|
|
col = chr(ord(col) + 1) |
239
|
|
|
|
240
|
|
|
current_row_number += 1 |
241
|
|
|
|
242
|
|
|
ws['B' + str(current_row_number)].font = title_font |
243
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
244
|
|
|
ws['B' + str(current_row_number)].border = f_border |
245
|
|
|
ws['B' + str(current_row_number)] = '环比' |
246
|
|
|
|
247
|
|
|
col = 'C' |
248
|
|
|
|
249
|
|
|
for i in range(0, ca_len): |
250
|
|
|
ws[col + str(current_row_number)].font = name_font |
251
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
252
|
|
|
ws[col + str(current_row_number)].border = f_border |
253
|
|
|
ws[col + str(current_row_number)] = str( |
254
|
|
|
round(reporting_period_data['increment_rates'][i] * 100, 2)) + '%' \ |
255
|
|
|
if reporting_period_data['increment_rates'][i] is not None else '-' |
256
|
|
|
|
257
|
|
|
col = chr(ord(col) + 1) |
258
|
|
|
|
259
|
|
|
current_row_number += 2 |
260
|
|
|
|
261
|
|
|
####################### |
262
|
|
|
|
263
|
|
|
has_values_data = True |
264
|
|
|
has_timestamps_data = True |
265
|
|
|
|
266
|
|
|
if 'values' not in reporting_period_data.keys() or \ |
267
|
|
|
reporting_period_data['values'] is None or \ |
268
|
|
|
len(reporting_period_data['values']) == 0: |
269
|
|
|
has_values_data = False |
270
|
|
|
|
271
|
|
|
if 'timestamps' not in reporting_period_data.keys() or \ |
272
|
|
|
reporting_period_data['timestamps'] is None or \ |
273
|
|
|
len(reporting_period_data['timestamps']) == 0 or \ |
274
|
|
|
len(reporting_period_data['timestamps'][0]) == 0: |
275
|
|
|
has_timestamps_data = False |
276
|
|
|
|
277
|
|
View Code Duplication |
if has_values_data and has_timestamps_data: |
|
|
|
|
278
|
|
|
ca_len = len(reporting_period_data['names']) |
279
|
|
|
time = reporting_period_data['timestamps'][0] |
280
|
|
|
real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
281
|
|
|
ws['B' + str(current_row_number)].font = title_font |
282
|
|
|
ws['B' + str(current_row_number)] = name + ' 详细数据' |
283
|
|
|
|
284
|
|
|
current_row_number += 1 |
285
|
|
|
|
286
|
|
|
chart_start_row_number = current_row_number |
287
|
|
|
|
288
|
|
|
current_row_number += ca_len * 6 + real_timestamps_len * 7 + 1 |
289
|
|
|
table_start_row_number = current_row_number |
290
|
|
|
|
291
|
|
|
ws.row_dimensions[current_row_number].height = 60 |
292
|
|
|
ws['B' + str(current_row_number)].fill = table_fill |
293
|
|
|
ws['B' + str(current_row_number)].font = title_font |
294
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
295
|
|
|
ws['B' + str(current_row_number)].border = f_border |
296
|
|
|
ws['B' + str(current_row_number)] = '日期时间' |
297
|
|
|
|
298
|
|
|
col = 'C' |
299
|
|
|
|
300
|
|
|
for i in range(0, ca_len): |
301
|
|
|
ws[col + str(current_row_number)].fill = table_fill |
302
|
|
|
ws[col + str(current_row_number)].font = title_font |
303
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
304
|
|
|
ws[col + str(current_row_number)].border = f_border |
305
|
|
|
ws[col + str(current_row_number)] = \ |
306
|
|
|
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
307
|
|
|
col = chr(ord(col) + 1) |
308
|
|
|
|
309
|
|
|
current_row_number += 1 |
310
|
|
|
|
311
|
|
|
for i in range(0, len(time)): |
312
|
|
|
ws['B' + str(current_row_number)].font = title_font |
313
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
314
|
|
|
ws['B' + str(current_row_number)].border = f_border |
315
|
|
|
ws['B' + str(current_row_number)] = time[i] |
316
|
|
|
|
317
|
|
|
col = 'C' |
318
|
|
|
for j in range(0, ca_len): |
319
|
|
|
ws[col + str(current_row_number)].font = title_font |
320
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
321
|
|
|
ws[col + str(current_row_number)].border = f_border |
322
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \ |
323
|
|
|
if reporting_period_data['values'][j][i] is not None else 0.00 |
324
|
|
|
col = chr(ord(col) + 1) |
325
|
|
|
|
326
|
|
|
current_row_number += 1 |
327
|
|
|
|
328
|
|
|
table_end_row_number = current_row_number - 1 |
329
|
|
|
|
330
|
|
|
ws['B' + str(current_row_number)].font = title_font |
331
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
332
|
|
|
ws['B' + str(current_row_number)].border = f_border |
333
|
|
|
ws['B' + str(current_row_number)] = '小计' |
334
|
|
|
|
335
|
|
|
col = 'C' |
336
|
|
|
|
337
|
|
|
for i in range(0, ca_len): |
338
|
|
|
ws[col + str(current_row_number)].font = title_font |
339
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
340
|
|
|
ws[col + str(current_row_number)].border = f_border |
341
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) |
342
|
|
|
col = chr(ord(col) + 1) |
343
|
|
|
|
344
|
|
|
current_row_number += 2 |
345
|
|
|
|
346
|
|
|
format_time_width_number = 1.0 |
347
|
|
|
min_len_number = 1.0 |
348
|
|
|
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0 |
349
|
|
|
|
350
|
|
|
if period_type == 'hourly': |
351
|
|
|
format_time_width_number = 4.0 |
352
|
|
|
min_len_number = 2 |
353
|
|
|
min_width_number = 12.0 |
354
|
|
|
elif period_type == 'daily': |
355
|
|
|
format_time_width_number = 2.5 |
356
|
|
|
min_len_number = 4 |
357
|
|
|
min_width_number = 14.0 |
358
|
|
|
elif period_type == 'monthly': |
359
|
|
|
format_time_width_number = 2.1 |
360
|
|
|
min_len_number = 4 |
361
|
|
|
min_width_number = 12.4 |
362
|
|
|
elif period_type == 'yearly': |
363
|
|
|
format_time_width_number = 1.5 |
364
|
|
|
min_len_number = 5 |
365
|
|
|
min_width_number = 11.5 |
366
|
|
|
|
367
|
|
|
for i in range(0, ca_len): |
368
|
|
|
line = LineChart() |
369
|
|
|
line.title = '报告期产出 - ' + \ |
370
|
|
|
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
371
|
|
|
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
372
|
|
|
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number) |
373
|
|
|
line.add_data(line_data, titles_from_data=True) |
374
|
|
|
line.set_categories(labels) |
375
|
|
|
line_data = line.series[0] |
376
|
|
|
line_data.marker.symbol = "circle" |
377
|
|
|
line_data.smooth = True |
378
|
|
|
line.x_axis.crosses = 'min' |
379
|
|
|
line.height = 8.25 |
380
|
|
|
line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number |
381
|
|
|
if line.width > 24: |
382
|
|
|
line.width = 24 |
383
|
|
|
line.dLbls = DataLabelList() |
384
|
|
|
line.dLbls.dLblPos = 't' |
385
|
|
|
line.dLbls.showVal = True |
386
|
|
|
line.dLbls.showPercent = False |
387
|
|
|
chart_col = 'B' |
388
|
|
|
chart_cell = chart_col + str(chart_start_row_number) |
389
|
|
|
chart_start_row_number += 6 |
390
|
|
|
ws.add_chart(line, chart_cell) |
391
|
|
|
|
392
|
|
|
##################################### |
393
|
|
|
|
394
|
|
|
has_child_flag = True |
395
|
|
|
|
396
|
|
|
if "child_space" not in report.keys() or "energy_category_names" not in report['child_space'].keys() or \ |
397
|
|
|
len(report['child_space']["energy_category_names"]) == 0 \ |
398
|
|
|
or 'child_space_names_array' not in report['child_space'].keys() \ |
399
|
|
|
or report['child_space']['child_space_names_array'] is None \ |
400
|
|
|
or len(report['child_space']['child_space_names_array']) == 0 \ |
401
|
|
|
or len(report['child_space']['child_space_names_array'][0]) == 0: |
402
|
|
|
has_child_flag = False |
403
|
|
|
|
404
|
|
|
if has_child_flag: |
405
|
|
|
child = report['child_space'] |
406
|
|
|
|
407
|
|
|
ws['B' + str(current_row_number)].font = title_font |
408
|
|
|
ws['B' + str(current_row_number)] = name + ' 子空间数据' |
409
|
|
|
|
410
|
|
|
current_row_number += 1 |
411
|
|
|
table_start_row_number = current_row_number |
412
|
|
|
|
413
|
|
|
ws.row_dimensions[current_row_number].height = 60 |
414
|
|
|
ws['B' + str(current_row_number)].fill = table_fill |
415
|
|
|
ws['B' + str(current_row_number)].border = f_border |
416
|
|
|
ca_len = len(child['energy_category_names']) |
417
|
|
|
|
418
|
|
|
for i in range(0, ca_len): |
419
|
|
|
row = chr(ord('C') + i) |
420
|
|
|
ws[row + str(current_row_number)].fill = table_fill |
421
|
|
|
ws[row + str(current_row_number)].font = name_font |
422
|
|
|
ws[row + str(current_row_number)].alignment = c_c_alignment |
423
|
|
|
ws[row + str(current_row_number)].border = f_border |
424
|
|
|
ws[row + str(current_row_number)] = \ |
425
|
|
|
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
426
|
|
|
|
427
|
|
|
space_len = len(child['child_space_names_array'][0]) |
428
|
|
|
|
429
|
|
|
for i in range(0, space_len): |
430
|
|
|
current_row_number += 1 |
431
|
|
|
row = str(current_row_number) |
432
|
|
|
|
433
|
|
|
ws['B' + row].font = name_font |
434
|
|
|
ws['B' + row].alignment = c_c_alignment |
435
|
|
|
ws['B' + row] = child['child_space_names_array'][0][i] |
436
|
|
|
ws['B' + row].border = f_border |
437
|
|
|
|
438
|
|
|
for j in range(0, ca_len): |
439
|
|
|
col = chr(ord('C') + j) |
440
|
|
|
ws[col + row].font = name_font |
441
|
|
|
ws[col + row].alignment = c_c_alignment |
442
|
|
|
ws[col + row] = round(child['subtotals_array'][j][i], 2) |
443
|
|
|
ws[col + row].border = f_border |
444
|
|
|
|
445
|
|
|
table_end_row_number = current_row_number |
446
|
|
|
current_row_number += 1 |
447
|
|
|
pie_start_row_number = current_row_number |
448
|
|
|
|
449
|
|
|
# Pie |
450
|
|
|
for i in range(0, ca_len): |
451
|
|
|
pie = PieChart() |
452
|
|
|
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, |
453
|
|
|
max_row=table_end_row_number) |
454
|
|
|
pie_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, |
455
|
|
|
max_row=table_end_row_number) |
456
|
|
|
pie.add_data(pie_data, titles_from_data=True) |
457
|
|
|
pie.set_categories(labels) |
458
|
|
|
pie.height = 6.6 |
459
|
|
|
pie.width = 8 |
460
|
|
|
pie.title = ws.cell(column=3 + i, row=table_start_row_number).value |
461
|
|
|
s1 = pie.series[0] |
462
|
|
|
s1.dLbls = DataLabelList() |
463
|
|
|
s1.dLbls.showCatName = False |
464
|
|
|
s1.dLbls.showVal = True |
465
|
|
|
s1.dLbls.showPercent = True |
466
|
|
|
chart_cell = '' |
467
|
|
|
if i % 2 == 0: |
468
|
|
|
chart_cell = 'B' + str(pie_start_row_number) |
469
|
|
|
else: |
470
|
|
|
chart_cell = 'E' + str(pie_start_row_number) |
471
|
|
|
pie_start_row_number += 5 |
472
|
|
|
ws.add_chart(pie, chart_cell) |
473
|
|
|
|
474
|
|
|
current_row_number = pie_start_row_number |
475
|
|
|
if ca_len % 2 == 1: |
476
|
|
|
current_row_number += 5 |
477
|
|
|
|
478
|
|
|
current_row_number += 1 |
479
|
|
|
########################################## |
480
|
|
|
current_sheet_parameters_row_number = chart_start_row_number + 1 |
|
|
|
|
481
|
|
|
has_parameters_names_and_timestamps_and_values_data = True |
482
|
|
|
if 'parameters' not in report.keys() or \ |
483
|
|
|
report['parameters'] is None or \ |
484
|
|
|
'names' not in report['parameters'].keys() or \ |
485
|
|
|
report['parameters']['names'] is None or \ |
486
|
|
|
len(report['parameters']['names']) == 0 or \ |
487
|
|
|
'timestamps' not in report['parameters'].keys() or \ |
488
|
|
|
report['parameters']['timestamps'] is None or \ |
489
|
|
|
len(report['parameters']['timestamps']) == 0 or \ |
490
|
|
|
'values' not in report['parameters'].keys() or \ |
491
|
|
|
report['parameters']['values'] is None or \ |
492
|
|
|
len(report['parameters']['values']) == 0 or \ |
493
|
|
|
timestamps_data_all_equal_0(report['parameters']['timestamps']): |
494
|
|
|
has_parameters_names_and_timestamps_and_values_data = False |
495
|
|
|
if has_parameters_names_and_timestamps_and_values_data: |
496
|
|
|
|
497
|
|
|
############################### |
498
|
|
|
# new worksheet |
499
|
|
|
############################### |
500
|
|
|
|
501
|
|
|
parameters_data = report['parameters'] |
502
|
|
|
parameters_names_len = len(parameters_data['names']) |
503
|
|
|
|
504
|
|
|
parameters_ws = wb.create_sheet('相关参数') |
505
|
|
|
|
506
|
|
|
parameters_timestamps_data_max_len = \ |
507
|
|
|
get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
508
|
|
|
|
509
|
|
|
# Row height |
510
|
|
|
parameters_ws.row_dimensions[1].height = 102 |
511
|
|
|
for i in range(2, 7 + 1): |
512
|
|
|
parameters_ws.row_dimensions[i].height = 42 |
513
|
|
|
|
514
|
|
|
for i in range(8, parameters_timestamps_data_max_len + 10): |
515
|
|
|
parameters_ws.row_dimensions[i].height = 60 |
516
|
|
|
|
517
|
|
|
# Col width |
518
|
|
|
parameters_ws.column_dimensions['A'].width = 1.5 |
519
|
|
|
|
520
|
|
|
parameters_ws.column_dimensions['B'].width = 25.0 |
521
|
|
|
|
522
|
|
|
for i in range(3, 12 + parameters_names_len * 3): |
523
|
|
|
parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
524
|
|
|
|
525
|
|
|
# Img |
526
|
|
|
img = Image("excelexporters/myems.png") |
527
|
|
|
img.width = img.width * 0.85 |
528
|
|
|
img.height = img.height * 0.85 |
529
|
|
|
# img = Image("myems.png") |
530
|
|
|
parameters_ws.add_image(img, 'B1') |
531
|
|
|
|
532
|
|
|
# Title |
533
|
|
|
parameters_ws.row_dimensions[3].height = 60 |
534
|
|
|
|
535
|
|
|
parameters_ws['B3'].font = name_font |
536
|
|
|
parameters_ws['B3'].alignment = b_r_alignment |
537
|
|
|
parameters_ws['B3'] = 'Name:' |
538
|
|
|
parameters_ws['C3'].border = b_border |
539
|
|
|
parameters_ws['C3'].alignment = b_c_alignment |
540
|
|
|
parameters_ws['C3'].font = name_font |
541
|
|
|
parameters_ws['C3'] = name |
542
|
|
|
|
543
|
|
|
parameters_ws['D3'].font = name_font |
544
|
|
|
parameters_ws['D3'].alignment = b_r_alignment |
545
|
|
|
parameters_ws['D3'] = 'Period:' |
546
|
|
|
parameters_ws['E3'].border = b_border |
547
|
|
|
parameters_ws['E3'].alignment = b_c_alignment |
548
|
|
|
parameters_ws['E3'].font = name_font |
549
|
|
|
parameters_ws['E3'] = period_type |
550
|
|
|
|
551
|
|
|
parameters_ws['F3'].font = name_font |
552
|
|
|
parameters_ws['F3'].alignment = b_r_alignment |
553
|
|
|
parameters_ws['F3'] = 'Date:' |
554
|
|
|
parameters_ws['G3'].border = b_border |
555
|
|
|
parameters_ws['G3'].alignment = b_c_alignment |
556
|
|
|
parameters_ws['G3'].font = name_font |
557
|
|
|
parameters_ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
558
|
|
|
parameters_ws.merge_cells("G3:H3") |
559
|
|
|
|
560
|
|
|
parameters_ws_current_row_number = 6 |
561
|
|
|
|
562
|
|
|
parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
563
|
|
|
parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' 相关参数' |
564
|
|
|
|
565
|
|
|
parameters_ws_current_row_number += 1 |
566
|
|
|
|
567
|
|
|
parameters_table_start_row_number = parameters_ws_current_row_number |
568
|
|
|
|
569
|
|
|
parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
570
|
|
|
|
571
|
|
|
parameters_ws_current_row_number += 1 |
572
|
|
|
|
573
|
|
|
table_current_col_number = 'B' |
574
|
|
|
|
575
|
|
|
for i in range(0, parameters_names_len): |
576
|
|
|
|
577
|
|
|
if len(parameters_data['timestamps'][i]) == 0: |
578
|
|
|
continue |
579
|
|
|
|
580
|
|
|
parameters_ws[table_current_col_number + str(parameters_ws_current_row_number - 1)].fill = table_fill |
581
|
|
|
parameters_ws[table_current_col_number + str(parameters_ws_current_row_number - 1)].border = f_border |
582
|
|
|
|
583
|
|
|
col = chr(ord(table_current_col_number) + 1) |
584
|
|
|
|
585
|
|
|
parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
586
|
|
|
parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
587
|
|
|
parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = name_font |
588
|
|
|
parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = c_c_alignment |
589
|
|
|
parameters_ws[col + str(parameters_ws_current_row_number - 1)] = parameters_data['names'][i] |
590
|
|
|
|
591
|
|
|
table_current_row_number = parameters_ws_current_row_number |
592
|
|
|
|
593
|
|
|
for j, value in enumerate(list(parameters_data['timestamps'][i])): |
594
|
|
|
col = table_current_col_number |
595
|
|
|
|
596
|
|
|
parameters_ws[col + str(table_current_row_number)].border = f_border |
597
|
|
|
parameters_ws[col + str(table_current_row_number)].font = title_font |
598
|
|
|
parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
599
|
|
|
parameters_ws[col + str(table_current_row_number)] = value |
600
|
|
|
|
601
|
|
|
col = chr(ord(col) + 1) |
602
|
|
|
|
603
|
|
|
parameters_ws[col + str(table_current_row_number)].border = f_border |
604
|
|
|
parameters_ws[col + str(table_current_row_number)].font = title_font |
605
|
|
|
parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
606
|
|
|
parameters_ws[col + str(table_current_row_number)] = round(parameters_data['values'][i][j], 2) |
607
|
|
|
|
608
|
|
|
table_current_row_number += 1 |
609
|
|
|
|
610
|
|
|
table_current_col_number = chr(ord(table_current_col_number) + 3) |
611
|
|
|
|
612
|
|
|
######################################################## |
613
|
|
|
# parameters chart and parameters table |
614
|
|
|
######################################################## |
615
|
|
|
|
616
|
|
|
ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
617
|
|
|
ws['B' + str(current_sheet_parameters_row_number)] = name + ' 相关参数' |
618
|
|
|
|
619
|
|
|
current_sheet_parameters_row_number += 1 |
620
|
|
|
|
621
|
|
|
chart_start_row_number = current_sheet_parameters_row_number |
622
|
|
|
|
623
|
|
|
col_index = 0 |
624
|
|
|
|
625
|
|
|
for i in range(0, parameters_names_len): |
626
|
|
|
|
627
|
|
|
if len(parameters_data['timestamps'][i]) == 0: |
628
|
|
|
continue |
629
|
|
|
|
630
|
|
|
line = LineChart() |
631
|
|
|
data_col = 3 + col_index * 3 |
632
|
|
|
labels_col = 2 + col_index * 3 |
633
|
|
|
col_index += 1 |
634
|
|
|
line.title = '相关参数 - ' + \ |
635
|
|
|
parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
636
|
|
|
labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
637
|
|
|
max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
638
|
|
|
line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
639
|
|
|
max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
640
|
|
|
line.add_data(line_data, titles_from_data=True) |
641
|
|
|
line.set_categories(labels) |
642
|
|
|
line_data = line.series[0] |
643
|
|
|
line_data.marker.symbol = "circle" |
644
|
|
|
line_data.smooth = True |
645
|
|
|
line.x_axis.crosses = 'min' |
646
|
|
|
line.height = 8.25 |
647
|
|
|
line.width = 24 |
648
|
|
|
line.dLbls = DataLabelList() |
649
|
|
|
line.dLbls.dLblPos = 't' |
650
|
|
|
line.dLbls.showVal = False |
651
|
|
|
line.dLbls.showPercent = False |
652
|
|
|
chart_col = 'B' |
653
|
|
|
chart_cell = chart_col + str(chart_start_row_number) |
654
|
|
|
chart_start_row_number += 6 |
655
|
|
|
ws.add_chart(line, chart_cell) |
656
|
|
|
|
657
|
|
|
current_sheet_parameters_row_number = chart_start_row_number |
658
|
|
|
|
659
|
|
|
current_sheet_parameters_row_number += 1 |
660
|
|
|
########################################## |
661
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
662
|
|
|
wb.save(filename) |
663
|
|
|
|
664
|
|
|
return filename |
665
|
|
|
|
666
|
|
|
|
667
|
|
|
def timestamps_data_all_equal_0(lists): |
668
|
|
|
for i, value in enumerate(list(lists)): |
669
|
|
|
if len(value) > 0: |
670
|
|
|
return False |
671
|
|
|
|
672
|
|
|
return True |
673
|
|
|
|
674
|
|
|
|
675
|
|
|
def get_parameters_timestamps_lists_max_len(parameters_timestamps_lists): |
676
|
|
|
max_len = 0 |
677
|
|
|
for i, value in enumerate(list(parameters_timestamps_lists)): |
678
|
|
|
if len(value) > max_len: |
679
|
|
|
max_len = len(value) |
680
|
|
|
|
681
|
|
|
return max_len |
682
|
|
|
|
683
|
|
|
|
684
|
|
|
def timestamps_data_not_equal_0(lists): |
685
|
|
|
number = 0 |
686
|
|
|
for i, value in enumerate(list(lists)): |
687
|
|
|
if len(value) > 0: |
688
|
|
|
number += 1 |
689
|
|
|
return number |
690
|
|
|
|