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