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
|
|
View Code Duplication |
def generate_excel(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type): |
|
|
|
|
67
|
|
|
wb = Workbook() |
68
|
|
|
|
69
|
|
|
# todo |
70
|
|
|
ws = wb.active |
71
|
|
|
|
72
|
|
|
# Row height |
73
|
|
|
ws.row_dimensions[1].height = 102 |
74
|
|
|
for i in range(2, 2000 + 1): |
75
|
|
|
ws.row_dimensions[i].height = 42 |
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('L')): |
83
|
|
|
ws.column_dimensions[chr(i)].width = 15.0 |
84
|
|
|
|
85
|
|
|
# Font |
86
|
|
|
name_font = Font(name='Constantia', size=15, bold=True) |
87
|
|
|
title_font = Font(name='宋体', size=15, bold=True) |
88
|
|
|
data_font = Font(name='Franklin Gothic Book', size=11) |
89
|
|
|
|
90
|
|
|
table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
91
|
|
|
f_border = Border(left=Side(border_style='medium', color='00000000'), |
92
|
|
|
right=Side(border_style='medium', color='00000000'), |
93
|
|
|
bottom=Side(border_style='medium', color='00000000'), |
94
|
|
|
top=Side(border_style='medium', color='00000000') |
95
|
|
|
) |
96
|
|
|
b_border = Border( |
97
|
|
|
bottom=Side(border_style='medium', color='00000000'), |
98
|
|
|
) |
99
|
|
|
|
100
|
|
|
b_c_alignment = Alignment(vertical='bottom', |
101
|
|
|
horizontal='center', |
102
|
|
|
text_rotation=0, |
103
|
|
|
wrap_text=True, |
104
|
|
|
shrink_to_fit=False, |
105
|
|
|
indent=0) |
106
|
|
|
c_c_alignment = Alignment(vertical='center', |
107
|
|
|
horizontal='center', |
108
|
|
|
text_rotation=0, |
109
|
|
|
wrap_text=True, |
110
|
|
|
shrink_to_fit=False, |
111
|
|
|
indent=0) |
112
|
|
|
b_r_alignment = Alignment(vertical='bottom', |
113
|
|
|
horizontal='right', |
114
|
|
|
text_rotation=0, |
115
|
|
|
wrap_text=True, |
116
|
|
|
shrink_to_fit=False, |
117
|
|
|
indent=0) |
118
|
|
|
c_r_alignment = Alignment(vertical='bottom', |
119
|
|
|
horizontal='center', |
120
|
|
|
text_rotation=0, |
121
|
|
|
wrap_text=True, |
122
|
|
|
shrink_to_fit=False, |
123
|
|
|
indent=0) |
124
|
|
|
|
125
|
|
|
# Img |
126
|
|
|
img = Image("excelexporters/myems.png") |
127
|
|
|
img.width = img.width * 0.85 |
128
|
|
|
img.height = img.height * 0.85 |
129
|
|
|
# img = Image("myems.png") |
130
|
|
|
ws.add_image(img, 'B1') |
131
|
|
|
|
132
|
|
|
# Title |
133
|
|
|
ws.row_dimensions[3].height = 60 |
134
|
|
|
|
135
|
|
|
ws['B3'].font = name_font |
136
|
|
|
ws['B3'].alignment = b_r_alignment |
137
|
|
|
ws['B3'] = 'Name:' |
138
|
|
|
ws['C3'].border = b_border |
139
|
|
|
ws['C3'].alignment = b_c_alignment |
140
|
|
|
ws['C3'].font = name_font |
141
|
|
|
ws['C3'] = name |
142
|
|
|
|
143
|
|
|
ws['D3'].font = name_font |
144
|
|
|
ws['D3'].alignment = b_r_alignment |
145
|
|
|
ws['D3'] = 'Period:' |
146
|
|
|
ws['E3'].border = b_border |
147
|
|
|
ws['E3'].alignment = b_c_alignment |
148
|
|
|
ws['E3'].font = name_font |
149
|
|
|
ws['E3'] = period_type |
150
|
|
|
|
151
|
|
|
ws['F3'].font = name_font |
152
|
|
|
ws['F3'].alignment = b_r_alignment |
153
|
|
|
ws['F3'] = 'Date:' |
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
|
|
|
ws.merge_cells("G3:H3") |
158
|
|
|
|
159
|
|
|
if "reporting_period" not in report.keys() or \ |
160
|
|
|
"timestamps" not in report['reporting_period'].keys() or len(report['reporting_period']['timestamps']) == 0: |
161
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
|
|
|
|
162
|
|
|
wb.save(filename) |
163
|
|
|
|
164
|
|
|
return filename |
165
|
|
|
|
166
|
|
|
############################### |
167
|
|
|
|
168
|
|
|
has_names_data_flag = True |
169
|
|
|
|
170
|
|
|
if "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
171
|
|
|
has_names_data_flag = False |
172
|
|
|
|
173
|
|
|
current_row_number = 6 |
174
|
|
|
if has_names_data_flag: |
175
|
|
|
reporting_period_data = report['reporting_period'] |
176
|
|
|
category = reporting_period_data['names'] |
177
|
|
|
ca_len = len(category) |
178
|
|
|
|
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
|
|
|
ws.row_dimensions[current_row_number].height = 60 |
185
|
|
|
ws['B' + str(current_row_number)].fill = table_fill |
186
|
|
|
ws['B' + str(current_row_number)].border = f_border |
187
|
|
|
for i in range(0, ca_len): |
188
|
|
|
col = chr(ord('C') + i) |
189
|
|
|
ws[col + str(current_row_number)].fill = table_fill |
190
|
|
|
ws[col + str(current_row_number)].font = name_font |
191
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
192
|
|
|
ws[col + str(current_row_number)].border = f_border |
193
|
|
|
ws[col + str(current_row_number)] = reporting_period_data['names'][i] + \ |
194
|
|
|
" (" + reporting_period_data['units'][i] + "/H)" |
195
|
|
|
|
196
|
|
|
current_row_number += 1 |
197
|
|
|
|
198
|
|
|
ws['B' + str(current_row_number)].font = title_font |
199
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
200
|
|
|
ws['B' + str(current_row_number)].border = f_border |
201
|
|
|
ws['B' + str(current_row_number)] = '平均负荷' |
202
|
|
|
|
203
|
|
|
for i in range(0, ca_len): |
204
|
|
|
col = chr(ord('C') + i) |
205
|
|
|
ws[col + str(current_row_number)].font = name_font |
206
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
207
|
|
|
ws[col + str(current_row_number)].border = f_border |
208
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['averages'][i], 2) \ |
209
|
|
|
if reporting_period_data['averages'][i] is not None else 'N/A' |
210
|
|
|
|
211
|
|
|
current_row_number += 1 |
212
|
|
|
|
213
|
|
|
ws['B' + str(current_row_number)].font = title_font |
214
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
215
|
|
|
ws['B' + str(current_row_number)].border = f_border |
216
|
|
|
ws['B' + str(current_row_number)] = '单位面积值' |
217
|
|
|
|
218
|
|
|
for i in range(0, ca_len): |
219
|
|
|
col = chr(ord('C') + i) |
220
|
|
|
ws[col + str(current_row_number)].font = name_font |
221
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
222
|
|
|
ws[col + str(current_row_number)].border = f_border |
223
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['averages_per_unit_area'][i], 2) \ |
224
|
|
|
if reporting_period_data['averages_per_unit_area'][i] is not None else 'N/A' |
225
|
|
|
|
226
|
|
|
current_row_number += 1 |
227
|
|
|
|
228
|
|
|
ws['B' + str(current_row_number)].font = title_font |
229
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
230
|
|
|
ws['B' + str(current_row_number)].border = f_border |
231
|
|
|
ws['B' + str(current_row_number)] = '环比' |
232
|
|
|
|
233
|
|
|
for i in range(0, ca_len): |
234
|
|
|
col = chr(ord('C') + i) |
235
|
|
|
ws[col + str(current_row_number)].font = name_font |
236
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
237
|
|
|
ws[col + str(current_row_number)].border = f_border |
238
|
|
|
ws[col + str(current_row_number)] = str( |
239
|
|
|
round(reporting_period_data['averages_increment_rate'][i] * 100, 2)) + "%" \ |
240
|
|
|
if reporting_period_data['averages_increment_rate'][i] is not None else "-" |
241
|
|
|
|
242
|
|
|
current_row_number += 2 |
243
|
|
|
|
244
|
|
|
ws['B' + str(current_row_number)].font = title_font |
245
|
|
|
ws['B' + str(current_row_number)] = name + '报告期最大负荷' |
246
|
|
|
|
247
|
|
|
current_row_number += 1 |
248
|
|
|
|
249
|
|
|
ws.row_dimensions[current_row_number].height = 60 |
250
|
|
|
ws['B' + str(current_row_number)].fill = table_fill |
251
|
|
|
ws['B' + str(current_row_number)].border = f_border |
252
|
|
|
for i in range(0, ca_len): |
253
|
|
|
col = chr(ord('C') + i) |
254
|
|
|
ws[col + str(current_row_number)].fill = table_fill |
255
|
|
|
ws[col + str(current_row_number)].font = name_font |
256
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
257
|
|
|
ws[col + str(current_row_number)].border = f_border |
258
|
|
|
ws[col + str(current_row_number)] = reporting_period_data['names'][i] + \ |
259
|
|
|
" (" + reporting_period_data['units'][i] + "/H)" |
260
|
|
|
|
261
|
|
|
current_row_number += 1 |
262
|
|
|
|
263
|
|
|
ws['B' + str(current_row_number)].font = title_font |
264
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
265
|
|
|
ws['B' + str(current_row_number)].border = f_border |
266
|
|
|
ws['B' + str(current_row_number)] = '最大负荷' |
267
|
|
|
|
268
|
|
|
for i in range(0, ca_len): |
269
|
|
|
col = chr(ord('C') + i) |
270
|
|
|
ws[col + str(current_row_number)].font = name_font |
271
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
272
|
|
|
ws[col + str(current_row_number)].border = f_border |
273
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['maximums'][i], 2) \ |
274
|
|
|
if reporting_period_data['maximums'][i] is not None else 'N/A' |
275
|
|
|
|
276
|
|
|
current_row_number += 1 |
277
|
|
|
|
278
|
|
|
ws['B' + str(current_row_number)].font = title_font |
279
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
280
|
|
|
ws['B' + str(current_row_number)].border = f_border |
281
|
|
|
ws['B' + str(current_row_number)] = '单位面积值' |
282
|
|
|
|
283
|
|
|
for i in range(0, ca_len): |
284
|
|
|
col = chr(ord('C') + i) |
285
|
|
|
ws[col + str(current_row_number)].font = name_font |
286
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
287
|
|
|
ws[col + str(current_row_number)].border = f_border |
288
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['maximums_per_unit_area'][i], 2) \ |
289
|
|
|
if reporting_period_data['maximums_per_unit_area'][i] is not None else 'N/A' |
290
|
|
|
|
291
|
|
|
current_row_number += 1 |
292
|
|
|
|
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
|
|
|
for i in range(0, ca_len): |
299
|
|
|
col = chr(ord('C') + i) |
300
|
|
|
ws[col + str(current_row_number)].font = name_font |
301
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
302
|
|
|
ws[col + str(current_row_number)].border = f_border |
303
|
|
|
ws[col + str(current_row_number)] = str( |
304
|
|
|
round(reporting_period_data['maximums_increment_rate'][i] * 100, 2)) + "%" \ |
305
|
|
|
if reporting_period_data['maximums_increment_rate'][i] is not None else "-" |
306
|
|
|
|
307
|
|
|
current_row_number += 2 |
308
|
|
|
|
309
|
|
|
ws['B' + str(current_row_number)].font = title_font |
310
|
|
|
ws['B' + str(current_row_number)] = name + '报告期负荷系数' |
311
|
|
|
|
312
|
|
|
current_row_number += 1 |
313
|
|
|
|
314
|
|
|
ws.row_dimensions[current_row_number].height = 60 |
315
|
|
|
ws['B' + str(current_row_number)].fill = table_fill |
316
|
|
|
ws['B' + str(current_row_number)].border = f_border |
317
|
|
|
for i in range(0, ca_len): |
318
|
|
|
col = chr(ord('C') + i) |
319
|
|
|
ws[col + str(current_row_number)].fill = table_fill |
320
|
|
|
ws[col + str(current_row_number)].font = name_font |
321
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
322
|
|
|
ws[col + str(current_row_number)].border = f_border |
323
|
|
|
ws[col + str(current_row_number)] = reporting_period_data['names'][i] |
324
|
|
|
|
325
|
|
|
current_row_number += 1 |
326
|
|
|
|
327
|
|
|
ws['B' + str(current_row_number)].font = title_font |
328
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
329
|
|
|
ws['B' + str(current_row_number)].border = f_border |
330
|
|
|
ws['B' + str(current_row_number)] = '负荷系数' |
331
|
|
|
|
332
|
|
|
for i in range(0, ca_len): |
333
|
|
|
col = chr(ord('C') + i) |
334
|
|
|
ws[col + str(current_row_number)].font = name_font |
335
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
336
|
|
|
ws[col + str(current_row_number)].border = f_border |
337
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['factors'][i], 2) \ |
338
|
|
|
if reporting_period_data['factors'][i] is not None else '-' |
339
|
|
|
|
340
|
|
|
current_row_number += 1 |
341
|
|
|
|
342
|
|
|
ws['B' + str(current_row_number)].font = title_font |
343
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
344
|
|
|
ws['B' + str(current_row_number)].border = f_border |
345
|
|
|
ws['B' + str(current_row_number)] = '环比' |
346
|
|
|
|
347
|
|
|
for i in range(0, ca_len): |
348
|
|
|
col = chr(ord('C') + i) |
349
|
|
|
ws[col + str(current_row_number)].font = name_font |
350
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
351
|
|
|
ws[col + str(current_row_number)].border = f_border |
352
|
|
|
ws[col + str(current_row_number)] = str( |
353
|
|
|
round(reporting_period_data['factors_increment_rate'][i] * 100, 2)) + "%" \ |
354
|
|
|
if reporting_period_data['factors_increment_rate'][i] is not None else "-" |
355
|
|
|
|
356
|
|
|
current_row_number += 2 |
357
|
|
|
|
358
|
|
|
has_sub_averages_data_flag = True |
359
|
|
|
has_sub_maximums_data_flag = True |
360
|
|
|
|
361
|
|
|
if "sub_averages" not in report['reporting_period'].keys() or len(report['reporting_period']['sub_averages']) == 0: |
362
|
|
|
has_sub_averages_data_flag = False |
363
|
|
|
|
364
|
|
|
if "sub_averages" not in report['reporting_period'].keys() or len(report['reporting_period']['sub_averages']) == 0: |
365
|
|
|
has_sub_maximums_data_flag = False |
366
|
|
|
|
367
|
|
|
if has_sub_averages_data_flag or has_sub_maximums_data_flag: |
368
|
|
|
reporting_period_data = report['reporting_period'] |
369
|
|
|
category = reporting_period_data['names'] |
370
|
|
|
ca_len = len(category) |
371
|
|
|
times = reporting_period_data['timestamps'] |
372
|
|
|
time = times[0] |
373
|
|
|
real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
374
|
|
|
ws['B' + str(current_row_number)].font = title_font |
375
|
|
|
ws['B' + str(current_row_number)] = name + '详细数据' |
376
|
|
|
|
377
|
|
|
current_row_number += 1 |
378
|
|
|
chart_start_number = current_row_number |
379
|
|
|
current_row_number += real_timestamps_len * 7 + 1 |
380
|
|
|
if has_sub_averages_data_flag: |
381
|
|
|
current_row_number = (current_row_number + ca_len * 6) |
382
|
|
|
|
383
|
|
|
if has_sub_maximums_data_flag: |
384
|
|
|
current_row_number = (current_row_number + ca_len * 6) |
385
|
|
|
|
386
|
|
|
table_start_number = current_row_number |
387
|
|
|
|
388
|
|
|
ws.row_dimensions[current_row_number].height = 60 |
389
|
|
|
ws['B' + str(current_row_number)].fill = table_fill |
390
|
|
|
ws['B' + str(current_row_number)].font = title_font |
391
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
392
|
|
|
ws['B' + str(current_row_number)].border = f_border |
393
|
|
|
ws['B' + str(current_row_number)] = '日期时间' |
394
|
|
|
|
395
|
|
|
col = 'C' |
396
|
|
|
|
397
|
|
|
for i in range(0, ca_len): |
398
|
|
|
if has_sub_averages_data_flag: |
399
|
|
|
ws[col + str(current_row_number)].fill = table_fill |
400
|
|
|
ws[col + str(current_row_number)].font = title_font |
401
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
402
|
|
|
ws[col + str(current_row_number)].border = f_border |
403
|
|
|
ws[col + str(current_row_number)] = reporting_period_data['names'][i] + \ |
404
|
|
|
" 平均负荷(" + reporting_period_data['units'][i] + "/H)" |
405
|
|
|
col = chr(ord(col) + 1) |
406
|
|
|
|
407
|
|
|
if has_sub_maximums_data_flag: |
408
|
|
|
ws[col + str(current_row_number)].fill = table_fill |
409
|
|
|
ws[col + str(current_row_number)].font = title_font |
410
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
411
|
|
|
ws[col + str(current_row_number)].border = f_border |
412
|
|
|
ws[col + str(current_row_number)] = reporting_period_data['names'][i] + \ |
413
|
|
|
" 最大负荷(" + reporting_period_data['units'][i] + "/H)" |
414
|
|
|
col = chr(ord(col) + 1) |
415
|
|
|
|
416
|
|
|
current_row_number += 1 |
417
|
|
|
|
418
|
|
|
for i in range(0, len(time)): |
419
|
|
|
ws['B' + str(current_row_number)].font = title_font |
420
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
421
|
|
|
ws['B' + str(current_row_number)].border = f_border |
422
|
|
|
ws['B' + str(current_row_number)] = time[i] |
423
|
|
|
|
424
|
|
|
col = 'C' |
425
|
|
|
for j in range(0, ca_len): |
426
|
|
|
|
427
|
|
|
if has_sub_averages_data_flag: |
428
|
|
|
ws[col + str(current_row_number)].font = title_font |
429
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
430
|
|
|
ws[col + str(current_row_number)].border = f_border |
431
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['sub_averages'][j][i], 2) \ |
432
|
|
|
if reporting_period_data['sub_averages'][j][i] is not None else 0.00 |
433
|
|
|
col = chr(ord(col) + 1) |
434
|
|
|
|
435
|
|
|
if has_sub_maximums_data_flag: |
436
|
|
|
ws[col + str(current_row_number)].font = title_font |
437
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
438
|
|
|
ws[col + str(current_row_number)].border = f_border |
439
|
|
|
ws[col + str(current_row_number)] = round(reporting_period_data['sub_maximums'][j][i], 2) \ |
440
|
|
|
if reporting_period_data['sub_maximums'][j][i] is not None else 0.00 |
441
|
|
|
col = chr(ord(col) + 1) |
442
|
|
|
|
443
|
|
|
current_row_number += 1 |
444
|
|
|
|
445
|
|
|
table_end_number = current_row_number - 1 |
446
|
|
|
|
447
|
|
|
current_chart_col_number = 3 |
448
|
|
|
current_chart_row_number = chart_start_number |
449
|
|
|
|
450
|
|
|
for i in range(0, ca_len): |
451
|
|
|
labels = Reference(ws, min_col=2, min_row=table_start_number + 1, max_row=table_end_number) |
452
|
|
|
|
453
|
|
|
if has_sub_averages_data_flag: |
454
|
|
|
line = LineChart() |
455
|
|
|
line.title = '报告期 平均负荷 - ' + ws.cell(column=current_chart_col_number, row=table_start_number).value |
456
|
|
|
datas = Reference(ws, min_col=current_chart_col_number, min_row=table_start_number, |
457
|
|
|
max_row=table_end_number) |
458
|
|
|
line.add_data(datas, titles_from_data=True) |
459
|
|
|
line.set_categories(labels) |
460
|
|
|
line_data = line.series[0] |
461
|
|
|
line_data.marker.symbol = "circle" |
462
|
|
|
line_data.smooth = True |
463
|
|
|
line.x_axis.crosses = 'min' |
464
|
|
|
line.height = 8.25 |
465
|
|
|
line.width = 24 |
466
|
|
|
line.dLbls = DataLabelList() |
467
|
|
|
line.dLbls.dLblPos = 't' |
468
|
|
|
line.dLbls.showVal = True |
469
|
|
|
ws.add_chart(line, "B" + str(current_chart_row_number)) |
470
|
|
|
current_chart_row_number += 6 |
471
|
|
|
current_chart_col_number += 1 |
472
|
|
|
|
473
|
|
|
if has_sub_maximums_data_flag: |
474
|
|
|
line = LineChart() |
475
|
|
|
line.title = '报告期 最大负荷 - ' + ws.cell(column=current_chart_col_number, row=table_start_number).value |
476
|
|
|
datas = Reference(ws, min_col=current_chart_col_number, min_row=table_start_number, |
477
|
|
|
max_row=table_end_number) |
478
|
|
|
line.add_data(datas, titles_from_data=True) |
479
|
|
|
line.set_categories(labels) |
480
|
|
|
line_data = line.series[0] |
481
|
|
|
line_data.marker.symbol = "circle" |
482
|
|
|
line_data.smooth = True |
483
|
|
|
line.x_axis.crosses = 'min' |
484
|
|
|
line.height = 8.25 |
485
|
|
|
line.width = 24 |
486
|
|
|
line.dLbls = DataLabelList() |
487
|
|
|
line.dLbls.dLblPos = 't' |
488
|
|
|
line.dLbls.showVal = True |
489
|
|
|
ws.add_chart(line, "B" + str(current_chart_row_number)) |
490
|
|
|
current_chart_row_number += 6 |
491
|
|
|
########################################## |
492
|
|
|
current_sheet_parameters_row_number = current_chart_row_number + 1 |
|
|
|
|
493
|
|
|
has_parameters_names_and_timestamps_and_values_data = True |
494
|
|
|
if 'parameters' not in report.keys() or \ |
495
|
|
|
report['parameters'] is None or \ |
496
|
|
|
'names' not in report['parameters'].keys() or \ |
497
|
|
|
report['parameters']['names'] is None or \ |
498
|
|
|
len(report['parameters']['names']) == 0 or \ |
499
|
|
|
'timestamps' not in report['parameters'].keys() or \ |
500
|
|
|
report['parameters']['timestamps'] is None or \ |
501
|
|
|
len(report['parameters']['timestamps']) == 0 or \ |
502
|
|
|
'values' not in report['parameters'].keys() or \ |
503
|
|
|
report['parameters']['values'] is None or \ |
504
|
|
|
len(report['parameters']['values']) == 0 or \ |
505
|
|
|
timestamps_data_all_equal_0(report['parameters']['timestamps']): |
506
|
|
|
has_parameters_names_and_timestamps_and_values_data = False |
507
|
|
|
if has_parameters_names_and_timestamps_and_values_data: |
508
|
|
|
|
509
|
|
|
############################### |
510
|
|
|
# new worksheet |
511
|
|
|
############################### |
512
|
|
|
|
513
|
|
|
parameters_data = report['parameters'] |
514
|
|
|
parameters_names_len = len(parameters_data['names']) |
515
|
|
|
|
516
|
|
|
parameters_ws = wb.create_sheet('相关参数') |
517
|
|
|
|
518
|
|
|
parameters_timestamps_data_max_len = \ |
519
|
|
|
get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
520
|
|
|
|
521
|
|
|
# Row height |
522
|
|
|
parameters_ws.row_dimensions[1].height = 102 |
523
|
|
|
for i in range(2, 7 + 1): |
524
|
|
|
parameters_ws.row_dimensions[i].height = 42 |
525
|
|
|
|
526
|
|
|
for i in range(8, parameters_timestamps_data_max_len + 10): |
527
|
|
|
parameters_ws.row_dimensions[i].height = 60 |
528
|
|
|
|
529
|
|
|
# Col width |
530
|
|
|
parameters_ws.column_dimensions['A'].width = 1.5 |
531
|
|
|
|
532
|
|
|
parameters_ws.column_dimensions['B'].width = 25.0 |
533
|
|
|
|
534
|
|
|
for i in range(3, 12 + parameters_names_len * 3): |
535
|
|
|
parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
536
|
|
|
|
537
|
|
|
# Img |
538
|
|
|
img = Image("excelexporters/myems.png") |
539
|
|
|
img.width = img.width * 0.85 |
540
|
|
|
img.height = img.height * 0.85 |
541
|
|
|
# img = Image("myems.png") |
542
|
|
|
parameters_ws.add_image(img, 'B1') |
543
|
|
|
|
544
|
|
|
# Title |
545
|
|
|
parameters_ws.row_dimensions[3].height = 60 |
546
|
|
|
|
547
|
|
|
parameters_ws['B3'].font = name_font |
548
|
|
|
parameters_ws['B3'].alignment = b_r_alignment |
549
|
|
|
parameters_ws['B3'] = 'Name:' |
550
|
|
|
parameters_ws['C3'].border = b_border |
551
|
|
|
parameters_ws['C3'].alignment = b_c_alignment |
552
|
|
|
parameters_ws['C3'].font = name_font |
553
|
|
|
parameters_ws['C3'] = name |
554
|
|
|
|
555
|
|
|
parameters_ws['D3'].font = name_font |
556
|
|
|
parameters_ws['D3'].alignment = b_r_alignment |
557
|
|
|
parameters_ws['D3'] = 'Period:' |
558
|
|
|
parameters_ws['E3'].border = b_border |
559
|
|
|
parameters_ws['E3'].alignment = b_c_alignment |
560
|
|
|
parameters_ws['E3'].font = name_font |
561
|
|
|
parameters_ws['E3'] = period_type |
562
|
|
|
|
563
|
|
|
parameters_ws['F3'].font = name_font |
564
|
|
|
parameters_ws['F3'].alignment = b_r_alignment |
565
|
|
|
parameters_ws['F3'] = 'Date:' |
566
|
|
|
parameters_ws['G3'].border = b_border |
567
|
|
|
parameters_ws['G3'].alignment = b_c_alignment |
568
|
|
|
parameters_ws['G3'].font = name_font |
569
|
|
|
parameters_ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
570
|
|
|
parameters_ws.merge_cells("G3:H3") |
571
|
|
|
|
572
|
|
|
parameters_ws_current_row_number = 6 |
573
|
|
|
|
574
|
|
|
parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
575
|
|
|
parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' 相关参数' |
576
|
|
|
|
577
|
|
|
parameters_ws_current_row_number += 1 |
578
|
|
|
|
579
|
|
|
parameters_table_start_row_number = parameters_ws_current_row_number |
580
|
|
|
|
581
|
|
|
parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
582
|
|
|
|
583
|
|
|
parameters_ws_current_row_number += 1 |
584
|
|
|
|
585
|
|
|
table_current_col_number = 'B' |
586
|
|
|
|
587
|
|
|
for i in range(0, parameters_names_len): |
588
|
|
|
|
589
|
|
|
if len(parameters_data['timestamps'][i]) == 0: |
590
|
|
|
continue |
591
|
|
|
|
592
|
|
|
parameters_ws[table_current_col_number + str(parameters_ws_current_row_number - 1)].fill = table_fill |
593
|
|
|
parameters_ws[table_current_col_number + str(parameters_ws_current_row_number - 1)].border = f_border |
594
|
|
|
|
595
|
|
|
col = chr(ord(table_current_col_number) + 1) |
596
|
|
|
|
597
|
|
|
parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
598
|
|
|
parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
599
|
|
|
parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = name_font |
600
|
|
|
parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = c_c_alignment |
601
|
|
|
parameters_ws[col + str(parameters_ws_current_row_number - 1)] = parameters_data['names'][i] |
602
|
|
|
|
603
|
|
|
table_current_row_number = parameters_ws_current_row_number |
604
|
|
|
|
605
|
|
|
for j, value in enumerate(list(parameters_data['timestamps'][i])): |
606
|
|
|
col = table_current_col_number |
607
|
|
|
|
608
|
|
|
parameters_ws[col + str(table_current_row_number)].border = f_border |
609
|
|
|
parameters_ws[col + str(table_current_row_number)].font = title_font |
610
|
|
|
parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
611
|
|
|
parameters_ws[col + str(table_current_row_number)] = value |
612
|
|
|
|
613
|
|
|
col = chr(ord(col) + 1) |
614
|
|
|
|
615
|
|
|
parameters_ws[col + str(table_current_row_number)].border = f_border |
616
|
|
|
parameters_ws[col + str(table_current_row_number)].font = title_font |
617
|
|
|
parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
618
|
|
|
parameters_ws[col + str(table_current_row_number)] = round(parameters_data['values'][i][j], 2) |
619
|
|
|
|
620
|
|
|
table_current_row_number += 1 |
621
|
|
|
|
622
|
|
|
table_current_col_number = chr(ord(table_current_col_number) + 3) |
623
|
|
|
|
624
|
|
|
######################################################## |
625
|
|
|
# parameters chart and parameters table |
626
|
|
|
######################################################## |
627
|
|
|
|
628
|
|
|
ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
629
|
|
|
ws['B' + str(current_sheet_parameters_row_number)] = name + ' 相关参数' |
630
|
|
|
|
631
|
|
|
current_sheet_parameters_row_number += 1 |
632
|
|
|
|
633
|
|
|
chart_start_row_number = current_sheet_parameters_row_number |
634
|
|
|
|
635
|
|
|
col_index = 0 |
636
|
|
|
|
637
|
|
|
for i in range(0, parameters_names_len): |
638
|
|
|
|
639
|
|
|
if len(parameters_data['timestamps'][i]) == 0: |
640
|
|
|
continue |
641
|
|
|
|
642
|
|
|
line = LineChart() |
643
|
|
|
data_col = 3 + col_index * 3 |
644
|
|
|
labels_col = 2 + col_index * 3 |
645
|
|
|
col_index += 1 |
646
|
|
|
line.title = '相关参数 - ' + \ |
647
|
|
|
parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
648
|
|
|
labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
649
|
|
|
max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
650
|
|
|
line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
651
|
|
|
max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
652
|
|
|
line.add_data(line_data, titles_from_data=True) |
653
|
|
|
line.set_categories(labels) |
654
|
|
|
line_data = line.series[0] |
655
|
|
|
line_data.marker.symbol = "circle" |
656
|
|
|
line_data.smooth = True |
657
|
|
|
line.x_axis.crosses = 'min' |
658
|
|
|
line.height = 8.25 |
659
|
|
|
line.width = 24 |
660
|
|
|
line.dLbls = DataLabelList() |
661
|
|
|
line.dLbls.dLblPos = 't' |
662
|
|
|
line.dLbls.showVal = False |
663
|
|
|
line.dLbls.showPercent = False |
664
|
|
|
chart_col = 'B' |
665
|
|
|
chart_cell = chart_col + str(chart_start_row_number) |
666
|
|
|
chart_start_row_number += 6 |
667
|
|
|
ws.add_chart(line, chart_cell) |
668
|
|
|
|
669
|
|
|
current_sheet_parameters_row_number = chart_start_row_number |
670
|
|
|
|
671
|
|
|
current_sheet_parameters_row_number += 1 |
672
|
|
|
########################################## |
673
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
674
|
|
|
wb.save(filename) |
675
|
|
|
|
676
|
|
|
return filename |
677
|
|
|
|
678
|
|
|
|
679
|
|
|
def timestamps_data_all_equal_0(lists): |
680
|
|
|
for i, value in enumerate(list(lists)): |
681
|
|
|
if len(value) > 0: |
682
|
|
|
return False |
683
|
|
|
|
684
|
|
|
return True |
685
|
|
|
|
686
|
|
|
|
687
|
|
|
def get_parameters_timestamps_lists_max_len(parameters_timestamps_lists): |
688
|
|
|
max_len = 0 |
689
|
|
|
for i, value in enumerate(list(parameters_timestamps_lists)): |
690
|
|
|
if len(value) > max_len: |
691
|
|
|
max_len = len(value) |
692
|
|
|
|
693
|
|
|
return max_len |
694
|
|
|
|
695
|
|
|
|
696
|
|
|
def timestamps_data_not_equal_0(lists): |
697
|
|
|
number = 0 |
698
|
|
|
for i, value in enumerate(list(lists)): |
699
|
|
|
if len(value) > 0: |
700
|
|
|
number += 1 |
701
|
|
|
return number |
702
|
|
|
|