|
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
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
#################################################################################################################### |
|
17
|
|
|
# PROCEDURES |
|
18
|
|
|
# Step 1: Validate the report data |
|
19
|
|
|
# Step 2: Generate excel file |
|
20
|
|
|
# Step 3: Encode the excel file bytes to Base64 |
|
21
|
|
|
#################################################################################################################### |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
View Code Duplication |
def export(result, |
|
|
|
|
|
|
25
|
|
|
name, |
|
26
|
|
|
reporting_start_datetime_local, |
|
27
|
|
|
reporting_end_datetime_local, |
|
28
|
|
|
period_type): |
|
29
|
|
|
#################################################################################################################### |
|
30
|
|
|
# Step 1: Validate the report data |
|
31
|
|
|
#################################################################################################################### |
|
32
|
|
|
if result is None: |
|
33
|
|
|
return None |
|
34
|
|
|
|
|
35
|
|
|
#################################################################################################################### |
|
36
|
|
|
# Step 2: Generate excel file from the report data |
|
37
|
|
|
#################################################################################################################### |
|
38
|
|
|
filename = generate_excel(result, |
|
39
|
|
|
name, |
|
40
|
|
|
reporting_start_datetime_local, |
|
41
|
|
|
reporting_end_datetime_local, |
|
42
|
|
|
period_type) |
|
43
|
|
|
|
|
44
|
|
|
#################################################################################################################### |
|
45
|
|
|
# Step 3: Encode the excel file to Base64 |
|
46
|
|
|
#################################################################################################################### |
|
47
|
|
|
try: |
|
48
|
|
|
with open(filename, 'rb') as binary_file: |
|
49
|
|
|
binary_file_data = binary_file.read() |
|
50
|
|
|
except IOError as ex: |
|
51
|
|
|
pass |
|
52
|
|
|
|
|
53
|
|
|
# Base64 encode the bytes |
|
54
|
|
|
base64_encoded_data = base64.b64encode(binary_file_data) |
|
|
|
|
|
|
55
|
|
|
# get the Base64 encoded data using human-readable characters. |
|
56
|
|
|
base64_message = base64_encoded_data.decode('utf-8') |
|
57
|
|
|
# delete the file from server |
|
58
|
|
|
try: |
|
59
|
|
|
os.remove(filename) |
|
60
|
|
|
except NotImplementedError as ex: |
|
61
|
|
|
pass |
|
62
|
|
|
return base64_message |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
def generate_excel(report, |
|
66
|
|
|
name, |
|
67
|
|
|
reporting_start_datetime_local, |
|
68
|
|
|
reporting_end_datetime_local, |
|
69
|
|
|
period_type): |
|
70
|
|
|
wb = Workbook() |
|
71
|
|
|
ws = wb.active |
|
72
|
|
|
|
|
73
|
|
|
# Row height |
|
74
|
|
|
ws.row_dimensions[1].height = 102 |
|
75
|
|
|
for i in range(2, 5 + 1): |
|
76
|
|
|
ws.row_dimensions[i].height = 42 |
|
77
|
|
|
|
|
78
|
|
|
# for i in range(2, 6 + 1): |
|
79
|
|
|
# ws.row_dimensions[i].height = 30 |
|
80
|
|
|
|
|
81
|
|
|
# Col width |
|
82
|
|
|
ws.column_dimensions['A'].width = 1.5 |
|
83
|
|
|
|
|
84
|
|
|
ws.column_dimensions['B'].width = 25.0 |
|
85
|
|
|
|
|
86
|
|
|
for i in range(ord('C'), ord('V')): |
|
87
|
|
|
ws.column_dimensions[chr(i)].width = 15.0 |
|
88
|
|
|
|
|
89
|
|
|
# Font |
|
90
|
|
|
name_font = Font(name='Constantia', size=15, bold=True) |
|
91
|
|
|
title_font = Font(name='宋体', size=15, bold=True) |
|
92
|
|
|
data_font = Font(name='Franklin Gothic Book', size=11) |
|
93
|
|
|
|
|
94
|
|
|
table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
|
95
|
|
|
f_border = Border(left=Side(border_style='medium', color='00000000'), |
|
96
|
|
|
right=Side(border_style='medium', color='00000000'), |
|
97
|
|
|
bottom=Side(border_style='medium', color='00000000'), |
|
98
|
|
|
top=Side(border_style='medium', color='00000000') |
|
99
|
|
|
) |
|
100
|
|
|
b_border = Border( |
|
101
|
|
|
bottom=Side(border_style='medium', color='00000000'), |
|
102
|
|
|
) |
|
103
|
|
|
|
|
104
|
|
|
b_c_alignment = Alignment(vertical='bottom', |
|
105
|
|
|
horizontal='center', |
|
106
|
|
|
text_rotation=0, |
|
107
|
|
|
wrap_text=True, |
|
108
|
|
|
shrink_to_fit=False, |
|
109
|
|
|
indent=0) |
|
110
|
|
|
c_c_alignment = Alignment(vertical='center', |
|
111
|
|
|
horizontal='center', |
|
112
|
|
|
text_rotation=0, |
|
113
|
|
|
wrap_text=True, |
|
114
|
|
|
shrink_to_fit=False, |
|
115
|
|
|
indent=0) |
|
116
|
|
|
b_r_alignment = Alignment(vertical='bottom', |
|
117
|
|
|
horizontal='right', |
|
118
|
|
|
text_rotation=0, |
|
119
|
|
|
wrap_text=True, |
|
120
|
|
|
shrink_to_fit=False, |
|
121
|
|
|
indent=0) |
|
122
|
|
|
c_r_alignment = Alignment(vertical='bottom', |
|
123
|
|
|
horizontal='center', |
|
124
|
|
|
text_rotation=0, |
|
125
|
|
|
wrap_text=True, |
|
126
|
|
|
shrink_to_fit=False, |
|
127
|
|
|
indent=0) |
|
128
|
|
|
|
|
129
|
|
|
# Img |
|
130
|
|
|
img = Image("excelexporters/myems.png") |
|
131
|
|
|
img.width = img.width * 0.85 |
|
132
|
|
|
img.height = img.height * 0.85 |
|
133
|
|
|
# img = Image("myems.png") |
|
134
|
|
|
ws.add_image(img, 'B1') |
|
135
|
|
|
|
|
136
|
|
|
# Title |
|
137
|
|
|
ws.row_dimensions[3].height = 60 |
|
138
|
|
|
|
|
139
|
|
|
ws['B3'].font = name_font |
|
140
|
|
|
ws['B3'].alignment = b_r_alignment |
|
141
|
|
|
ws['B3'] = 'Name:' |
|
142
|
|
|
ws['C3'].border = b_border |
|
143
|
|
|
ws['C3'].alignment = b_c_alignment |
|
144
|
|
|
ws['C3'].font = name_font |
|
145
|
|
|
ws['C3'] = name |
|
146
|
|
|
|
|
147
|
|
|
ws['D3'].font = name_font |
|
148
|
|
|
ws['D3'].alignment = b_r_alignment |
|
149
|
|
|
ws['D3'] = 'Period:' |
|
150
|
|
|
ws['E3'].border = b_border |
|
151
|
|
|
ws['E3'].alignment = b_c_alignment |
|
152
|
|
|
ws['E3'].font = name_font |
|
153
|
|
|
ws['E3'] = period_type |
|
154
|
|
|
|
|
155
|
|
|
ws['F3'].font = name_font |
|
156
|
|
|
ws['F3'].alignment = b_r_alignment |
|
157
|
|
|
ws['F3'] = 'Date:' |
|
158
|
|
|
ws['G3'].border = b_border |
|
159
|
|
|
ws['G3'].alignment = b_c_alignment |
|
160
|
|
|
ws['G3'].font = name_font |
|
161
|
|
|
ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
|
162
|
|
|
ws.merge_cells("G3:H3") |
|
163
|
|
|
if "reporting_period" not in report.keys() or \ |
|
164
|
|
|
"names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
|
165
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
|
166
|
|
|
wb.save(filename) |
|
167
|
|
|
|
|
168
|
|
|
return filename |
|
169
|
|
|
################################################ |
|
170
|
|
|
# First: 趋势 |
|
171
|
|
|
# 6: title |
|
172
|
|
|
# 7: table title |
|
173
|
|
|
# 8~ table_data |
|
174
|
|
|
################################################ |
|
175
|
|
|
has_data_flag = True |
|
176
|
|
|
reporting_period_data = report['reporting_period'] |
|
177
|
|
|
if "names" not in reporting_period_data.keys() or \ |
|
178
|
|
|
reporting_period_data['names'] is None or \ |
|
179
|
|
|
len(reporting_period_data['names']) == 0: |
|
180
|
|
|
has_data_flag = False |
|
181
|
|
|
|
|
182
|
|
|
if "timestamps" not in reporting_period_data.keys() or \ |
|
183
|
|
|
reporting_period_data['timestamps'] is None or \ |
|
184
|
|
|
len(reporting_period_data['timestamps']) == 0: |
|
185
|
|
|
has_data_flag = False |
|
186
|
|
|
|
|
187
|
|
|
if "values" not in reporting_period_data.keys() or \ |
|
188
|
|
|
reporting_period_data['values'] is None or \ |
|
189
|
|
|
len(reporting_period_data['values']) == 0: |
|
190
|
|
|
has_data_flag = False |
|
191
|
|
|
ca = reporting_period_data['names'] |
|
192
|
|
|
ca_len = len(ca) |
|
193
|
|
|
temp_max_row = 0 |
|
194
|
|
|
times = reporting_period_data['timestamps'] |
|
195
|
|
|
if has_data_flag: |
|
196
|
|
|
ws['B6'].font = title_font |
|
197
|
|
|
ws['B6'] = name + ' 趋势' |
|
198
|
|
|
|
|
199
|
|
|
ws.row_dimensions[7].height = 60 |
|
200
|
|
|
ws['B7'].fill = table_fill |
|
201
|
|
|
ws['B7'].font = title_font |
|
202
|
|
|
ws['B7'].border = f_border |
|
203
|
|
|
ws['B7'].alignment = c_c_alignment |
|
204
|
|
|
ws['B7'] = '日期时间' |
|
205
|
|
|
time = times[0] |
|
206
|
|
|
has_data = False |
|
207
|
|
|
max_row = 0 |
|
208
|
|
|
if len(time) > 0: |
|
209
|
|
|
has_data = True |
|
210
|
|
|
max_row = 8 + len(time) |
|
211
|
|
|
# print("max_row", max_row) |
|
212
|
|
|
temp_max_row = max_row |
|
213
|
|
|
if has_data: |
|
214
|
|
|
for i in range(0, len(time)): |
|
215
|
|
|
col = 'B' |
|
216
|
|
|
row = str(8 + i) |
|
217
|
|
|
# col = chr(ord('B') + i) |
|
218
|
|
|
ws[col + row].font = title_font |
|
219
|
|
|
ws[col + row].alignment = c_c_alignment |
|
220
|
|
|
ws[col + row] = time[i] |
|
221
|
|
|
ws[col + row].border = f_border |
|
222
|
|
|
|
|
223
|
|
|
for i in range(0, ca_len): |
|
224
|
|
|
# 38 title |
|
225
|
|
|
col = chr(ord('C') + i) |
|
226
|
|
|
|
|
227
|
|
|
ws[col + '7'].fill = table_fill |
|
228
|
|
|
ws[col + '7'].font = title_font |
|
229
|
|
|
ws[col + '7'].alignment = c_c_alignment |
|
230
|
|
|
ws[col + '7'] = reporting_period_data['names'][i] |
|
231
|
|
|
ws[col + '7'].border = f_border |
|
232
|
|
|
|
|
233
|
|
|
# 39 data |
|
234
|
|
|
time = times[i] |
|
235
|
|
|
time_len = len(time) |
|
236
|
|
|
|
|
237
|
|
|
for j in range(0, time_len): |
|
238
|
|
|
row = str(8 + j) |
|
239
|
|
|
# col = chr(ord('B') + i) |
|
240
|
|
|
ws[col + row].font = title_font |
|
241
|
|
|
ws[col + row].alignment = c_c_alignment |
|
242
|
|
|
ws[col + row] = round(reporting_period_data['values'][i][j], 3) |
|
243
|
|
|
ws[col + row].border = f_border |
|
244
|
|
|
# line |
|
245
|
|
|
# 39~: line |
|
246
|
|
|
line = LineChart() |
|
247
|
|
|
line.title = '趋势值 - ' + reporting_period_data['names'][i] |
|
248
|
|
|
labels = Reference(ws, min_col=2, min_row=8, max_row=max_row-1) |
|
249
|
|
|
line_data = Reference(ws, min_col=3 + i, min_row=7, max_row=max_row-1) |
|
250
|
|
|
line.add_data(line_data, titles_from_data=True) |
|
251
|
|
|
line.set_categories(labels) |
|
252
|
|
|
# line_data = line.series[0] |
|
253
|
|
|
# line_data.marker.symbol = "circle" |
|
254
|
|
|
line_data.smooth = True |
|
255
|
|
|
line.x_axis.crosses = 'min' |
|
256
|
|
|
line.height = 8.25 # cm 1.05*5 1.05cm = 30 pt |
|
257
|
|
|
line.width = 36 |
|
258
|
|
|
# pie.title = "Pies sold by category" |
|
259
|
|
|
line.dLbls = DataLabelList() |
|
260
|
|
|
# line.dLbls.showCatName = True # label show |
|
261
|
|
|
line.dLbls.dLblPos = 't' |
|
262
|
|
|
line.dLbls.showVal = False # val show |
|
263
|
|
|
line.dLbls.showPercent = False # percent show |
|
264
|
|
|
# s1 = CharacterProperties(sz=1800) # font size *100 |
|
265
|
|
|
chart_col = chr(ord('B')) |
|
266
|
|
|
chart_cell = chart_col + str(max_row + 2 + 6*i) |
|
267
|
|
|
print("chart_cell", chart_cell) |
|
268
|
|
|
ws.add_chart(line, chart_cell) |
|
269
|
|
|
else: |
|
270
|
|
|
pass |
|
271
|
|
|
|
|
272
|
|
|
for i in range(8, temp_max_row + 1 + 1 + + ca_len * 6): |
|
273
|
|
|
ws.row_dimensions[i].height = 42 |
|
274
|
|
|
|
|
275
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
|
276
|
|
|
wb.save(filename) |
|
277
|
|
|
|
|
278
|
|
|
return filename |
|
279
|
|
|
|