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