1
|
|
|
import base64 |
2
|
|
|
import uuid |
3
|
|
|
import os |
4
|
|
|
from openpyxl.chart import ( |
5
|
|
|
BarChart, |
6
|
|
|
Reference, |
7
|
|
|
) |
8
|
|
|
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font |
9
|
|
|
from openpyxl.drawing.image import Image |
10
|
|
|
from openpyxl import Workbook |
11
|
|
|
from openpyxl.chart.label import DataLabelList |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
#################################################################################################################### |
15
|
|
|
# PROCEDURES |
16
|
|
|
# Step 1: Validate the report data |
17
|
|
|
# Step 2: Generate excelexporters file |
18
|
|
|
# Step 3: Encode the excelexporters file to Base64 |
19
|
|
|
#################################################################################################################### |
20
|
|
|
|
21
|
|
|
def export(result, space_name): |
22
|
|
|
#################################################################################################################### |
23
|
|
|
# Step 1: Validate the report data |
24
|
|
|
#################################################################################################################### |
25
|
|
|
if result is None: |
26
|
|
|
return None |
27
|
|
|
|
28
|
|
|
#################################################################################################################### |
29
|
|
|
# Step 2: Generate excel file from the report data |
30
|
|
|
#################################################################################################################### |
31
|
|
|
filename = generate_excel(result, |
32
|
|
|
space_name) |
33
|
|
|
#################################################################################################################### |
34
|
|
|
# Step 3: Encode the excel file to Base64 |
35
|
|
|
#################################################################################################################### |
36
|
|
|
try: |
37
|
|
|
with open(filename, 'rb') as binary_file: |
38
|
|
|
binary_file_data = binary_file.read() |
39
|
|
|
except IOError as ex: |
40
|
|
|
pass |
41
|
|
|
|
42
|
|
|
# Base64 encode the bytes |
43
|
|
|
base64_encoded_data = base64.b64encode(binary_file_data) |
|
|
|
|
44
|
|
|
# get the Base64 encoded data using human-readable characters. |
45
|
|
|
base64_message = base64_encoded_data.decode('utf-8') |
46
|
|
|
# delete the file from server |
47
|
|
|
try: |
48
|
|
|
os.remove(filename) |
49
|
|
|
except NotImplementedError as ex: |
50
|
|
|
pass |
51
|
|
|
return base64_message |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def generate_excel(report, space_name): |
55
|
|
|
|
56
|
|
|
wb = Workbook() |
57
|
|
|
ws = wb.active |
58
|
|
|
|
59
|
|
|
# Row height |
60
|
|
|
ws.row_dimensions[1].height = 118 |
61
|
|
|
for i in range(2, 5000 + 1): |
62
|
|
|
ws.row_dimensions[i].height = 30 |
63
|
|
|
# Col width |
64
|
|
|
ws.column_dimensions['A'].width = 1 |
65
|
|
|
|
66
|
|
|
# Font |
67
|
|
|
name_font = Font(name='Constantia', size=15, bold=True) |
68
|
|
|
title_font = Font(name='宋体', size=15, bold=True) |
69
|
|
|
data_font = Font(name='Franklin Gothic Book', size=11) |
70
|
|
|
|
71
|
|
|
table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
72
|
|
|
f_border = Border(left=Side(border_style='medium', color='00000000'), |
73
|
|
|
right=Side(border_style='medium', color='00000000'), |
74
|
|
|
bottom=Side(border_style='medium', color='00000000'), |
75
|
|
|
top=Side(border_style='medium', color='00000000') |
76
|
|
|
) |
77
|
|
|
b_border = Border( |
78
|
|
|
bottom=Side(border_style='medium', color='00000000'), |
79
|
|
|
) |
80
|
|
|
|
81
|
|
|
b_c_alignment = Alignment(vertical='bottom', |
82
|
|
|
horizontal='center', |
83
|
|
|
text_rotation=0, |
84
|
|
|
wrap_text=False, |
85
|
|
|
shrink_to_fit=False, |
86
|
|
|
indent=0) |
87
|
|
|
c_c_alignment = Alignment(vertical='center', |
88
|
|
|
horizontal='center', |
89
|
|
|
text_rotation=0, |
90
|
|
|
wrap_text=False, |
91
|
|
|
shrink_to_fit=False, |
92
|
|
|
indent=0) |
93
|
|
|
b_r_alignment = Alignment(vertical='bottom', |
94
|
|
|
horizontal='right', |
95
|
|
|
text_rotation=0, |
96
|
|
|
wrap_text=False, |
97
|
|
|
shrink_to_fit=False, |
98
|
|
|
indent=0) |
99
|
|
|
c_r_alignment = Alignment(vertical='bottom', |
100
|
|
|
horizontal='center', |
101
|
|
|
text_rotation=0, |
102
|
|
|
wrap_text=False, |
103
|
|
|
shrink_to_fit=False, |
104
|
|
|
indent=0) |
105
|
|
|
for i in range(ord('B'), ord('F')): |
106
|
|
|
ws.column_dimensions[chr(i)].width = 30.0 |
107
|
|
|
|
108
|
|
|
# Img |
109
|
|
|
ws.merge_cells("B1:D1") |
110
|
|
|
ws.merge_cells("B2:E2") |
111
|
|
|
img = Image("excelexporters/myems.png") |
112
|
|
|
ws.add_image(img, 'B1') |
113
|
|
|
|
114
|
|
|
# Title |
115
|
|
|
ws['B3'].border = f_border |
116
|
|
|
ws['B3'].font = name_font |
117
|
|
|
ws['B3'].alignment = b_c_alignment |
118
|
|
|
ws['B3'] = '名称' |
119
|
|
|
|
120
|
|
|
ws['C3'].border = f_border |
121
|
|
|
ws['C3'].alignment = b_c_alignment |
122
|
|
|
ws['C3'].font = name_font |
123
|
|
|
ws['C3'] = '空间' |
124
|
|
|
|
125
|
|
|
ws['D3'].border = f_border |
126
|
|
|
ws['D3'].font = name_font |
127
|
|
|
ws['D3'].alignment = b_c_alignment |
128
|
|
|
ws['D3'] = '成本中心' |
129
|
|
|
|
130
|
|
|
ws['E3'].border = f_border |
131
|
|
|
ws['E3'].alignment = b_c_alignment |
132
|
|
|
ws['E3'].font = name_font |
133
|
|
|
ws['E3'] = '描述' |
134
|
|
|
|
135
|
|
|
current_row_number = 4 |
136
|
|
|
for i in range(0, len(report['equipments'])): |
137
|
|
|
|
138
|
|
|
ws['B' + str(current_row_number)].font = title_font |
|
|
|
|
139
|
|
|
ws['B' + str(current_row_number)].border = f_border |
140
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
141
|
|
|
ws['B' + str(current_row_number)] = report['equipments'][i]['equipment_name'] |
142
|
|
|
|
143
|
|
|
ws['C' + str(current_row_number)].font = title_font |
144
|
|
|
ws['C' + str(current_row_number)].border = f_border |
145
|
|
|
ws['C' + str(current_row_number)].alignment = c_c_alignment |
146
|
|
|
ws['C' + str(current_row_number)] = report['equipments'][i]['space_name'] |
147
|
|
|
|
148
|
|
|
ws['D' + str(current_row_number)].font = title_font |
149
|
|
|
ws['D' + str(current_row_number)].border = f_border |
150
|
|
|
ws['D' + str(current_row_number)].alignment = c_c_alignment |
151
|
|
|
ws['D' + str(current_row_number)] = report['equipments'][i]['cost_center_name'] |
152
|
|
|
|
153
|
|
|
ws['E' + str(current_row_number)].font = title_font |
154
|
|
|
ws['E' + str(current_row_number)].border = f_border |
155
|
|
|
ws['E' + str(current_row_number)].alignment = c_c_alignment |
156
|
|
|
ws['E' + str(current_row_number)] = report['equipments'][i]['description'] |
157
|
|
|
current_row_number += 1 |
158
|
|
|
|
159
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
160
|
|
|
wb.save(filename) |
161
|
|
|
|
162
|
|
|
return filename |
163
|
|
|
|