1
|
|
|
import base64 |
2
|
|
|
import os |
3
|
|
|
import uuid |
4
|
|
|
|
5
|
|
|
from openpyxl import Workbook |
6
|
|
|
from openpyxl.chart import ( |
7
|
|
|
LineChart, |
8
|
|
|
Reference, |
9
|
|
|
) |
10
|
|
|
from openpyxl.drawing.image import Image |
11
|
|
|
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
#################################################################################################################### |
15
|
|
|
# PROCEDURES |
16
|
|
|
# Step 1: Validate the report data |
17
|
|
|
# Step 2: Generate excel file |
18
|
|
|
# Step 3: Encode the excel file bytes to Base64 |
19
|
|
|
#################################################################################################################### |
20
|
|
|
|
21
|
|
|
|
22
|
|
View Code Duplication |
def export(report, |
|
|
|
|
23
|
|
|
name, |
24
|
|
|
reporting_start_datetime_local, |
25
|
|
|
reporting_end_datetime_local, |
26
|
|
|
period_type): |
27
|
|
|
#################################################################################################################### |
28
|
|
|
# Step 1: Validate the report data |
29
|
|
|
#################################################################################################################### |
30
|
|
|
if report is None: |
31
|
|
|
return None |
32
|
|
|
print(report) |
33
|
|
|
|
34
|
|
|
#################################################################################################################### |
35
|
|
|
# Step 2: Generate excel file from the report data |
36
|
|
|
#################################################################################################################### |
37
|
|
|
filename = generate_excel(report, |
38
|
|
|
name, |
39
|
|
|
reporting_start_datetime_local, |
40
|
|
|
reporting_end_datetime_local, |
41
|
|
|
period_type) |
42
|
|
|
#################################################################################################################### |
43
|
|
|
# Step 3: Encode the excel file to Base64 |
44
|
|
|
#################################################################################################################### |
45
|
|
|
try: |
46
|
|
|
with open(filename, 'rb') as binary_file: |
47
|
|
|
binary_file_data = binary_file.read() |
48
|
|
|
except IOError as ex: |
49
|
|
|
pass |
50
|
|
|
|
51
|
|
|
# Base64 encode the bytes |
52
|
|
|
base64_encoded_data = base64.b64encode(binary_file_data) |
|
|
|
|
53
|
|
|
# get the Base64 encoded data using human-readable characters. |
54
|
|
|
base64_message = base64_encoded_data.decode('utf-8') |
55
|
|
|
# delete the file from server |
56
|
|
|
try: |
57
|
|
|
os.remove(filename) |
58
|
|
|
except NotImplementedError as ex: |
59
|
|
|
pass |
60
|
|
|
return base64_message |
61
|
|
|
|
62
|
|
|
|
63
|
|
View Code Duplication |
def generate_excel(report, |
|
|
|
|
64
|
|
|
name, |
65
|
|
|
reporting_start_datetime_local, |
66
|
|
|
reporting_end_datetime_local, |
67
|
|
|
period_type): |
68
|
|
|
wb = Workbook() |
69
|
|
|
ws = wb.active |
70
|
|
|
|
71
|
|
|
# Row height |
72
|
|
|
ws.row_dimensions[1].height = 121 |
73
|
|
|
|
74
|
|
|
for i in range(2, 37 + 1): |
75
|
|
|
ws.row_dimensions[i].height = 30 |
76
|
|
|
|
77
|
|
|
for i in range(38, 90 + 1): |
78
|
|
|
ws.row_dimensions[i].height = 30 |
79
|
|
|
|
80
|
|
|
# Col width |
81
|
|
|
ws.column_dimensions['A'].width = 1.5 |
82
|
|
|
ws.column_dimensions['B'].width = 20.0 |
83
|
|
|
|
84
|
|
|
for i in range(ord('C'), ord('I')): |
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=False, |
106
|
|
|
shrink_to_fit=False, |
107
|
|
|
indent=0) |
108
|
|
|
c_c_alignment = Alignment(vertical='center', |
109
|
|
|
horizontal='center', |
110
|
|
|
text_rotation=0, |
111
|
|
|
wrap_text=False, |
112
|
|
|
shrink_to_fit=False, |
113
|
|
|
indent=0) |
114
|
|
|
b_r_alignment = Alignment(vertical='bottom', |
115
|
|
|
horizontal='right', |
116
|
|
|
text_rotation=0, |
117
|
|
|
wrap_text=False, |
118
|
|
|
shrink_to_fit=False, |
119
|
|
|
indent=0) |
120
|
|
|
# c_r_alignment = Alignment(vertical='bottom', |
121
|
|
|
# horizontal='center', |
122
|
|
|
# text_rotation=0, |
123
|
|
|
# wrap_text=False, |
124
|
|
|
# shrink_to_fit=False, |
125
|
|
|
# indent=0) |
126
|
|
|
|
127
|
|
|
# Img |
128
|
|
|
img = Image("excelexporters/myems.png") |
129
|
|
|
# img = Image("myems.png") |
130
|
|
|
ws.add_image(img, 'B1') |
131
|
|
|
|
132
|
|
|
# Title |
133
|
|
|
ws['B3'].font = name_font |
134
|
|
|
ws['B3'].alignment = b_r_alignment |
135
|
|
|
ws['B3'] = 'Name:' |
136
|
|
|
ws['C3'].border = b_border |
137
|
|
|
ws['C3'].alignment = b_c_alignment |
138
|
|
|
ws['C3'].font = name_font |
139
|
|
|
ws['C3'] = name |
140
|
|
|
|
141
|
|
|
ws['D3'].font = name_font |
142
|
|
|
ws['D3'].alignment = b_r_alignment |
143
|
|
|
ws['D3'] = 'Period:' |
144
|
|
|
ws['E3'].border = b_border |
145
|
|
|
ws['E3'].alignment = b_c_alignment |
146
|
|
|
ws['E3'].font = name_font |
147
|
|
|
ws['E3'] = period_type |
148
|
|
|
|
149
|
|
|
ws['F3'].font = name_font |
150
|
|
|
ws['F3'].alignment = b_r_alignment |
151
|
|
|
ws['F3'] = 'Date:' |
152
|
|
|
ws['G3'].border = b_border |
153
|
|
|
ws['G3'].alignment = b_c_alignment |
154
|
|
|
ws['G3'].font = name_font |
155
|
|
|
ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
156
|
|
|
ws.merge_cells("G3:H3") |
157
|
|
|
|
158
|
|
|
if "reporting_period" not in report.keys() or \ |
159
|
|
|
"names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
160
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
|
|
|
|
161
|
|
|
wb.save(filename) |
162
|
|
|
|
163
|
|
|
return filename |
164
|
|
|
################################################# |
165
|
|
|
# First: 统计分析 |
166
|
|
|
# 6: title |
167
|
|
|
# 7: table title |
168
|
|
|
# 8~ca_len table_data |
169
|
|
|
################################################# |
170
|
|
|
reporting_period_data = report['reporting_period'] |
171
|
|
|
|
172
|
|
|
has_energy_data_flag = True |
173
|
|
|
|
174
|
|
|
if "names" not in reporting_period_data.keys() or \ |
175
|
|
|
reporting_period_data['names'] is None or \ |
176
|
|
|
len(reporting_period_data['names']) == 0: |
177
|
|
|
has_energy_data_flag = False |
178
|
|
|
|
179
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
180
|
|
|
wb.save(filename) |
181
|
|
|
|
182
|
|
|
return filename |
183
|
|
|
|
184
|
|
|
if has_energy_data_flag: |
185
|
|
|
ws['B6'].font = title_font |
186
|
|
|
ws['B6'] = name + ' 统计分析' |
187
|
|
|
|
188
|
|
|
category = reporting_period_data['names'] |
189
|
|
|
|
190
|
|
|
# table_title |
191
|
|
|
ws['B7'].fill = table_fill |
192
|
|
|
ws['B7'].font = title_font |
193
|
|
|
ws['B7'].alignment = c_c_alignment |
194
|
|
|
ws['B7'] = '报告期' |
195
|
|
|
ws['B7'].border = f_border |
196
|
|
|
|
197
|
|
|
ws['C7'].font = title_font |
198
|
|
|
ws['C7'].alignment = c_c_alignment |
199
|
|
|
ws['C7'] = '算术平均数' |
200
|
|
|
ws['C7'].border = f_border |
201
|
|
|
|
202
|
|
|
ws['D7'].font = title_font |
203
|
|
|
ws['D7'].alignment = c_c_alignment |
204
|
|
|
ws['D7'] = '中位数' |
205
|
|
|
ws['D7'].border = f_border |
206
|
|
|
|
207
|
|
|
ws['E7'].font = title_font |
208
|
|
|
ws['E7'].alignment = c_c_alignment |
209
|
|
|
ws['E7'] = '最小值' |
210
|
|
|
ws['E7'].border = f_border |
211
|
|
|
|
212
|
|
|
ws['F7'].font = title_font |
213
|
|
|
ws['F7'].alignment = c_c_alignment |
214
|
|
|
ws['F7'] = '最大值' |
215
|
|
|
ws['F7'].border = f_border |
216
|
|
|
|
217
|
|
|
ws['G7'].font = title_font |
218
|
|
|
ws['G7'].alignment = c_c_alignment |
219
|
|
|
ws['G7'] = '样本标准差' |
220
|
|
|
ws['G7'].border = f_border |
221
|
|
|
|
222
|
|
|
ws['H7'].font = title_font |
223
|
|
|
ws['H7'].alignment = c_c_alignment |
224
|
|
|
ws['H7'] = '样本方差' |
225
|
|
|
ws['H7'].border = f_border |
226
|
|
|
|
227
|
|
|
# table_data |
228
|
|
|
|
229
|
|
|
for i, value in enumerate(category): |
230
|
|
|
row = i * 2 + 8 |
231
|
|
|
ws['B' + str(row)].font = name_font |
232
|
|
|
ws['B' + str(row)].alignment = c_c_alignment |
233
|
|
|
ws['B' + str(row)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + " )" |
234
|
|
|
ws['B' + str(row)].border = f_border |
235
|
|
|
|
236
|
|
|
ws['B' + str(row + 1)].font = name_font |
237
|
|
|
ws['B' + str(row + 1)].alignment = c_c_alignment |
238
|
|
|
ws['B' + str(row + 1)] = "环比" |
239
|
|
|
ws['B' + str(row + 1)].border = f_border |
240
|
|
|
|
241
|
|
|
ws['C' + str(row)].font = name_font |
242
|
|
|
ws['C' + str(row)].alignment = c_c_alignment |
243
|
|
|
ws['C' + str(row)] = round(reporting_period_data['means'][i], 2) \ |
244
|
|
|
if reporting_period_data['means'][i] is not None else '' |
245
|
|
|
ws['C' + str(row)].border = f_border |
246
|
|
|
ws['C' + str(row)].number_format = '0.00' |
247
|
|
|
|
248
|
|
|
ws['C' + str(row + 1)].font = name_font |
249
|
|
|
ws['C' + str(row + 1)].alignment = c_c_alignment |
250
|
|
|
ws['C' + str(row + 1)] = str(round(reporting_period_data['means_increment_rate'][i] * 100, 2)) + "%" \ |
251
|
|
|
if reporting_period_data['means_increment_rate'][i] is not None else '0.00%' |
252
|
|
|
ws['C' + str(row + 1)].border = f_border |
253
|
|
|
|
254
|
|
|
ws['D' + str(row)].font = name_font |
255
|
|
|
ws['D' + str(row)].alignment = c_c_alignment |
256
|
|
|
ws['D' + str(row)] = round(reporting_period_data['medians'][i], 2) \ |
257
|
|
|
if reporting_period_data['medians'][i] is not None else '' |
258
|
|
|
ws['D' + str(row)].border = f_border |
259
|
|
|
ws['D' + str(row)].number_format = '0.00' |
260
|
|
|
|
261
|
|
|
ws['D' + str(row + 1)].font = name_font |
262
|
|
|
ws['D' + str(row + 1)].alignment = c_c_alignment |
263
|
|
|
ws['D' + str(row + 1)] = str(round(reporting_period_data['medians_increment_rate'][i] * 100, 2)) + "%" \ |
264
|
|
|
if reporting_period_data['medians_increment_rate'][i] is not None else '0.00%' |
265
|
|
|
ws['D' + str(row + 1)].border = f_border |
266
|
|
|
|
267
|
|
|
ws['E' + str(row)].font = name_font |
268
|
|
|
ws['E' + str(row)].alignment = c_c_alignment |
269
|
|
|
ws['E' + str(row)] = round(reporting_period_data['minimums'][i], 2) \ |
270
|
|
|
if reporting_period_data['minimums'][i] is not None else '' |
271
|
|
|
ws['E' + str(row)].border = f_border |
272
|
|
|
ws['E' + str(row)].number_format = '0.00' |
273
|
|
|
|
274
|
|
|
ws['E' + str(row + 1)].font = name_font |
275
|
|
|
ws['E' + str(row + 1)].alignment = c_c_alignment |
276
|
|
|
ws['E' + str(row + 1)] = str(round(reporting_period_data['minimums_increment_rate'][i] * 100, 2)) + "%" \ |
277
|
|
|
if reporting_period_data['minimums_increment_rate'][i] is not None else '0.00%' |
278
|
|
|
ws['E' + str(row + 1)].border = f_border |
279
|
|
|
|
280
|
|
|
ws['F' + str(row)].font = name_font |
281
|
|
|
ws['F' + str(row)].alignment = c_c_alignment |
282
|
|
|
ws['F' + str(row)] = round(reporting_period_data['maximums'][i], 2) \ |
283
|
|
|
if reporting_period_data['maximums'][i] is not None else '' |
284
|
|
|
ws['F' + str(row)].border = f_border |
285
|
|
|
ws['F' + str(row)].number_format = '0.00' |
286
|
|
|
|
287
|
|
|
ws['F' + str(row + 1)].font = name_font |
288
|
|
|
ws['F' + str(row + 1)].alignment = c_c_alignment |
289
|
|
|
ws['F' + str(row + 1)] = str(round(reporting_period_data['maximums_increment_rate'][i] * 100, 2)) + "%" \ |
290
|
|
|
if reporting_period_data['maximums_increment_rate'][i] is not None else '0.00%' |
291
|
|
|
ws['F' + str(row + 1)].border = f_border |
292
|
|
|
|
293
|
|
|
ws['G' + str(row)].font = name_font |
294
|
|
|
ws['G' + str(row)].alignment = c_c_alignment |
295
|
|
|
ws['G' + str(row)] = round(reporting_period_data['stdevs'][i], 2) \ |
296
|
|
|
if reporting_period_data['stdevs'][i] is not None else '' |
297
|
|
|
ws['G' + str(row)].border = f_border |
298
|
|
|
ws['G' + str(row)].number_format = '0.00' |
299
|
|
|
|
300
|
|
|
ws['G' + str(row + 1)].font = name_font |
301
|
|
|
ws['G' + str(row + 1)].alignment = c_c_alignment |
302
|
|
|
ws['G' + str(row + 1)] = str(round(reporting_period_data['stdevs_increment_rate'][i] * 100, 2)) + "%" \ |
303
|
|
|
if reporting_period_data['stdevs_increment_rate'][i] is not None else '0.00%' |
304
|
|
|
ws['G' + str(row + 1)].border = f_border |
305
|
|
|
|
306
|
|
|
ws['H' + str(row)].font = name_font |
307
|
|
|
ws['H' + str(row)].alignment = c_c_alignment |
308
|
|
|
ws['H' + str(row)] = round(reporting_period_data['variances'][i], 2) \ |
309
|
|
|
if reporting_period_data['variances'][i] is not None else '' |
310
|
|
|
ws['H' + str(row)].border = f_border |
311
|
|
|
ws['H' + str(row)].number_format = '0.00' |
312
|
|
|
|
313
|
|
|
ws['H' + str(row + 1)].font = name_font |
314
|
|
|
ws['H' + str(row + 1)].alignment = c_c_alignment |
315
|
|
|
ws['H' + str(row + 1)] = str(round(reporting_period_data['variances_increment_rate'][i] * 100, 2)) + "%" \ |
316
|
|
|
if reporting_period_data['variances_increment_rate'][i] is not None else '0.00%' |
317
|
|
|
ws['H' + str(row + 1)].border = f_border |
318
|
|
|
################################################# |
319
|
|
|
# Second: 报告期消耗 |
320
|
|
|
# 9 + ca_len * 2: title |
321
|
|
|
# 10 + ca_len * 2: table title |
322
|
|
|
# row_title + 2 ~ row_title + 2 + ca_len : table_data |
323
|
|
|
################################################# |
324
|
|
|
|
325
|
|
|
if has_energy_data_flag: |
326
|
|
|
names = reporting_period_data['names'] |
327
|
|
|
ca_len = len(names) |
328
|
|
|
|
329
|
|
|
row_title = 9 + ca_len * 2 |
330
|
|
|
|
331
|
|
|
ws['B' + str(row_title)].font = title_font |
332
|
|
|
ws['B' + str(row_title)] = name + ' 单位面积值' |
333
|
|
|
ws['D' + str(row_title)].font = title_font |
334
|
|
|
ws['D' + str(row_title)] = str(report['store']['area']) + 'M²' |
335
|
|
|
|
336
|
|
|
category = reporting_period_data['names'] |
337
|
|
|
|
338
|
|
|
# table_title |
339
|
|
|
ws['B' + str(row_title + 1)].fill = table_fill |
340
|
|
|
ws['B' + str(row_title + 1)].font = title_font |
341
|
|
|
ws['B' + str(row_title + 1)].alignment = c_c_alignment |
342
|
|
|
ws['B' + str(row_title + 1)] = '报告期' |
343
|
|
|
ws['B' + str(row_title + 1)].border = f_border |
344
|
|
|
|
345
|
|
|
ws['C' + str(row_title + 1)].font = title_font |
346
|
|
|
ws['C' + str(row_title + 1)].alignment = c_c_alignment |
347
|
|
|
ws['C' + str(row_title + 1)] = '算术平均数' |
348
|
|
|
ws['C' + str(row_title + 1)].border = f_border |
349
|
|
|
|
350
|
|
|
ws['D' + str(row_title + 1)].font = title_font |
351
|
|
|
ws['D' + str(row_title + 1)].alignment = c_c_alignment |
352
|
|
|
ws['D' + str(row_title + 1)] = '中位数' |
353
|
|
|
ws['D' + str(row_title + 1)].border = f_border |
354
|
|
|
|
355
|
|
|
ws['E' + str(row_title + 1)].font = title_font |
356
|
|
|
ws['E' + str(row_title + 1)].alignment = c_c_alignment |
357
|
|
|
ws['E' + str(row_title + 1)] = '最小值' |
358
|
|
|
ws['E' + str(row_title + 1)].border = f_border |
359
|
|
|
|
360
|
|
|
ws['F' + str(row_title + 1)].font = title_font |
361
|
|
|
ws['F' + str(row_title + 1)].alignment = c_c_alignment |
362
|
|
|
ws['F' + str(row_title + 1)] = '最大值' |
363
|
|
|
ws['F' + str(row_title + 1)].border = f_border |
364
|
|
|
|
365
|
|
|
ws['G' + str(row_title + 1)].font = title_font |
366
|
|
|
ws['G' + str(row_title + 1)].alignment = c_c_alignment |
367
|
|
|
ws['G' + str(row_title + 1)] = '样本标准差' |
368
|
|
|
ws['G' + str(row_title + 1)].border = f_border |
369
|
|
|
|
370
|
|
|
ws['H' + str(row_title + 1)].font = title_font |
371
|
|
|
ws['H' + str(row_title + 1)].alignment = c_c_alignment |
372
|
|
|
ws['H' + str(row_title + 1)] = '样本方差' |
373
|
|
|
ws['H' + str(row_title + 1)].border = f_border |
374
|
|
|
|
375
|
|
|
# table_data |
376
|
|
|
|
377
|
|
|
for i, value in enumerate(category): |
378
|
|
|
row_data = row_title + 2 + i |
379
|
|
|
ws['B' + str(row_data)].font = name_font |
380
|
|
|
ws['B' + str(row_data)].alignment = c_c_alignment |
381
|
|
|
ws['B' + str(row_data)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][ |
382
|
|
|
i] + "/M²)" |
383
|
|
|
ws['B' + str(row_data)].border = f_border |
384
|
|
|
|
385
|
|
|
ws['C' + str(row_data)].font = name_font |
386
|
|
|
ws['C' + str(row_data)].alignment = c_c_alignment |
387
|
|
|
if reporting_period_data['means_per_unit_area'][i] \ |
388
|
|
|
or reporting_period_data['means_per_unit_area'][i] == 0: |
389
|
|
|
ws['C' + str(row_data)] = round(reporting_period_data['means_per_unit_area'][i], 2) |
390
|
|
|
ws['C' + str(row_data)].border = f_border |
391
|
|
|
ws['C' + str(row_data)].number_format = '0.00' |
392
|
|
|
|
393
|
|
|
ws['D' + str(row_data)].font = name_font |
394
|
|
|
ws['D' + str(row_data)].alignment = c_c_alignment |
395
|
|
|
if reporting_period_data['medians_per_unit_area'][i] \ |
396
|
|
|
or reporting_period_data['medians_per_unit_area'][i] == 0: |
397
|
|
|
ws['D' + str(row_data)] = round(reporting_period_data['medians_per_unit_area'][i], 2) |
398
|
|
|
ws['D' + str(row_data)].border = f_border |
399
|
|
|
ws['D' + str(row_data)].number_format = '0.00' |
400
|
|
|
|
401
|
|
|
ws['E' + str(row_data)].font = name_font |
402
|
|
|
ws['E' + str(row_data)].alignment = c_c_alignment |
403
|
|
|
if reporting_period_data['minimums_per_unit_area'][i] \ |
404
|
|
|
or reporting_period_data['minimums_per_unit_area'][i] == 0: |
405
|
|
|
ws['E' + str(row_data)] = round(reporting_period_data['minimums_per_unit_area'][i], 2) |
406
|
|
|
ws['E' + str(row_data)].border = f_border |
407
|
|
|
ws['E' + str(row_data)].number_format = '0.00' |
408
|
|
|
|
409
|
|
|
ws['F' + str(row_data)].font = name_font |
410
|
|
|
ws['F' + str(row_data)].alignment = c_c_alignment |
411
|
|
|
if reporting_period_data['maximums_per_unit_area'][i] \ |
412
|
|
|
or reporting_period_data['maximums_per_unit_area'][i] == 0: |
413
|
|
|
ws['F' + str(row_data)] = round(reporting_period_data['maximums_per_unit_area'][i], 2) |
414
|
|
|
ws['F' + str(row_data)].border = f_border |
415
|
|
|
ws['F' + str(row_data)].number_format = '0.00' |
416
|
|
|
|
417
|
|
|
ws['G' + str(row_data)].font = name_font |
418
|
|
|
ws['G' + str(row_data)].alignment = c_c_alignment |
419
|
|
|
if (reporting_period_data['stdevs_per_unit_area'][i]) \ |
420
|
|
|
or reporting_period_data['stdevs_per_unit_area'][i] == 0: |
421
|
|
|
ws['G' + str(row_data)] = round(reporting_period_data['stdevs_per_unit_area'][i], 2) |
422
|
|
|
ws['G' + str(row_data)].border = f_border |
423
|
|
|
ws['G' + str(row_data)].number_format = '0.00' |
424
|
|
|
|
425
|
|
|
ws['H' + str(row_data)].font = name_font |
426
|
|
|
ws['H' + str(row_data)].alignment = c_c_alignment |
427
|
|
|
if reporting_period_data['variances_per_unit_area'][i] \ |
428
|
|
|
or reporting_period_data['variances_per_unit_area'][i] == 0: |
429
|
|
|
ws['H' + str(row_data)] = round(reporting_period_data['variances_per_unit_area'][i], 2) |
430
|
|
|
ws['H' + str(row_data)].border = f_border |
431
|
|
|
ws['H' + str(row_data)].number_format = '0.00' |
432
|
|
|
|
433
|
|
|
######################################################## |
434
|
|
|
# Third: 详细数据 |
435
|
|
|
# row_sat+row_title~ row_sat+row_title+time_len: line |
436
|
|
|
# row_sat+1+row_title: table title |
437
|
|
|
# i + row_sat + 2 + 10 * ca_len~: table_data |
438
|
|
|
######################################################## |
439
|
|
|
has_timestamps_flag = True |
440
|
|
|
if "timestamps" not in reporting_period_data.keys() or \ |
441
|
|
|
reporting_period_data['timestamps'] is None or \ |
442
|
|
|
len(reporting_period_data['timestamps']) == 0: |
443
|
|
|
has_timestamps_flag = False |
444
|
|
|
|
445
|
|
|
if has_timestamps_flag: |
446
|
|
|
timestamps = reporting_period_data['timestamps'][0] |
447
|
|
|
values = reporting_period_data['values'] |
448
|
|
|
names = reporting_period_data['names'] |
449
|
|
|
ca_len = len(names) |
450
|
|
|
time_len = len(timestamps) |
451
|
|
|
# title |
452
|
|
|
row_title = 10 * ca_len |
453
|
|
|
# row_st == row_statistical analysis table |
454
|
|
|
row_sat = 12 + 3 * ca_len |
455
|
|
|
|
456
|
|
|
ws['B' + str(row_sat + row_title)].font = title_font |
457
|
|
|
ws['B' + str(row_sat + row_title)] = name + ' 详细数据' |
458
|
|
|
# table_title |
459
|
|
|
ws['B' + str(row_sat + 1 + row_title)].fill = table_fill |
460
|
|
|
ws['B' + str(row_sat + 1 + row_title)].font = name_font |
461
|
|
|
ws['B' + str(row_sat + 1 + row_title)].alignment = c_c_alignment |
462
|
|
|
ws['B' + str(row_sat + 1 + row_title)] = "时间" |
463
|
|
|
ws['B' + str(row_sat + 1 + row_title)].border = f_border |
464
|
|
|
|
465
|
|
|
for i in range(0, ca_len): |
466
|
|
|
col = chr(ord('C') + i) |
467
|
|
|
|
468
|
|
|
ws[col + str(row_sat + 1 + row_title)].font = name_font |
469
|
|
|
ws[col + str(row_sat + 1 + row_title)].alignment = c_c_alignment |
470
|
|
|
ws[col + str(row_sat + 1 + row_title)] = names[i] + " - (" + reporting_period_data['units'][i] + ")" |
471
|
|
|
ws[col + str(row_sat + 1 + row_title)].border = f_border |
472
|
|
|
# table_date |
473
|
|
|
for i in range(0, time_len): |
474
|
|
|
rows = i + row_sat + 2 + 10 * ca_len |
475
|
|
|
|
476
|
|
|
ws['B' + str(rows)].font = name_font |
477
|
|
|
ws['B' + str(rows)].alignment = c_c_alignment |
478
|
|
|
ws['B' + str(rows)] = timestamps[i] |
479
|
|
|
ws['B' + str(rows)].border = f_border |
480
|
|
|
|
481
|
|
|
for index in range(0, ca_len): |
482
|
|
|
col = chr(ord('C') + index) |
483
|
|
|
|
484
|
|
|
ws[col + str(rows)].font = name_font |
485
|
|
|
ws[col + str(rows)].alignment = c_c_alignment |
486
|
|
|
ws[col + str(rows)] = round(values[index][i], 2) |
487
|
|
|
ws[col + str(rows)].number_format = '0.00' |
488
|
|
|
ws[col + str(rows)].border = f_border |
489
|
|
|
|
490
|
|
|
# 小计 |
491
|
|
|
row_subtotals = row_sat + 2 + time_len + 10 * ca_len |
492
|
|
|
ws['B' + str(row_subtotals)].font = name_font |
493
|
|
|
ws['B' + str(row_subtotals)].alignment = c_c_alignment |
494
|
|
|
ws['B' + str(row_subtotals)] = "小计" |
495
|
|
|
ws['B' + str(row_subtotals)].border = f_border |
496
|
|
|
|
497
|
|
|
for i in range(0, ca_len): |
498
|
|
|
col = chr(ord('C') + i) |
499
|
|
|
|
500
|
|
|
ws[col + str(row_subtotals)].font = name_font |
501
|
|
|
ws[col + str(row_subtotals)].alignment = c_c_alignment |
502
|
|
|
ws[col + str(row_subtotals)] = round(reporting_period_data['subtotals'][i], 2) |
503
|
|
|
ws[col + str(row_subtotals)].border = f_border |
504
|
|
|
ws[col + str(row_subtotals)].number_format = '0.00' |
505
|
|
|
|
506
|
|
|
# LineChart |
507
|
|
|
for i in range(0, ca_len): |
508
|
|
|
lc = LineChart() |
509
|
|
|
lc.title = "报告期消耗" + " - " + names[i] + "(" + reporting_period_data['units'][i] + ")" |
510
|
|
|
lc.style = 10 |
511
|
|
|
lc.height = 8.40 # cm 1.05*8 1.05cm = 30 pt |
512
|
|
|
lc.width = 31 |
513
|
|
|
lc.x_axis.majorTickMark = 'in' |
514
|
|
|
lc.y_axis.majorTickMark = 'in' |
515
|
|
|
times = Reference(ws, min_col=2, min_row=row_sat + 2 + row_title, |
516
|
|
|
max_row=row_sat + 2 + row_title + time_len) |
517
|
|
|
lc_data = Reference(ws, min_col=3 + i, min_row=row_sat + 1 + row_title, |
518
|
|
|
max_row=row_sat + 1 + row_title + time_len) |
519
|
|
|
lc.add_data(lc_data, titles_from_data=True) |
520
|
|
|
lc.set_categories(times) |
521
|
|
|
ser = lc.series[0] |
522
|
|
|
ser.marker.symbol = "diamond" |
523
|
|
|
ser.marker.size = 5 |
524
|
|
|
ws.add_chart(lc, 'B' + str(row_sat + 10 * i)) |
525
|
|
|
|
526
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
527
|
|
|
wb.save(filename) |
528
|
|
|
|
529
|
|
|
return filename |
530
|
|
|
|