1
|
|
|
import base64 |
2
|
|
|
import uuid |
3
|
|
|
import os |
4
|
|
|
from openpyxl.styles import PatternFill, 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, reporting_start_datetime_local, reporting_end_datetime_local, 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
|
|
|
reporting_start_datetime_local, |
30
|
|
|
reporting_end_datetime_local, |
31
|
|
|
language) |
32
|
|
|
#################################################################################################################### |
33
|
|
|
# Step 3: Encode the excel file to Base64 |
34
|
|
|
#################################################################################################################### |
35
|
|
|
binary_file_data = b'' |
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
|
|
View Code Duplication |
def generate_excel(report, space_name, reporting_start_datetime_local, reporting_end_datetime_local, language): |
|
|
|
|
55
|
|
|
|
56
|
|
|
locale_path = './i18n/' |
57
|
|
|
if language == 'zh_CN': |
58
|
|
|
trans = gettext.translation('myems', locale_path, languages=['zh_CN']) |
59
|
|
|
elif language == 'de': |
60
|
|
|
trans = gettext.translation('myems', locale_path, languages=['de']) |
61
|
|
|
elif language == 'en': |
62
|
|
|
trans = gettext.translation('myems', locale_path, languages=['en']) |
63
|
|
|
else: |
64
|
|
|
trans = gettext.translation('myems', locale_path, languages=['en']) |
65
|
|
|
trans.install() |
66
|
|
|
_ = trans.gettext |
67
|
|
|
|
68
|
|
|
wb = Workbook() |
69
|
|
|
ws = wb.active |
70
|
|
|
ws.title = "CombinedEquipmentBatch" |
71
|
|
|
|
72
|
|
|
# Row height |
73
|
|
|
ws.row_dimensions[1].height = 102 |
74
|
|
|
for i in range(2, 5 + 1): |
75
|
|
|
ws.row_dimensions[i].height = 42 |
76
|
|
|
|
77
|
|
|
for i in range(6, len(report['combined_equipments']) + 15): |
78
|
|
|
ws.row_dimensions[i].height = 60 |
79
|
|
|
|
80
|
|
|
# Col width |
81
|
|
|
ws.column_dimensions['A'].width = 1.5 |
82
|
|
|
|
83
|
|
|
ws.column_dimensions['B'].width = 25.0 |
84
|
|
|
|
85
|
|
|
for i in range(ord('C'), ord('L')): |
86
|
|
|
ws.column_dimensions[chr(i)].width = 15.0 |
87
|
|
|
|
88
|
|
|
# Font |
89
|
|
|
name_font = Font(name='Arial', size=15, bold=True) |
90
|
|
|
title_font = Font(name='Arial', size=15, bold=True) |
91
|
|
|
data_font = Font(name='Franklin Gothic Book', size=11) |
92
|
|
|
|
93
|
|
|
table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
94
|
|
|
f_border = Border(left=Side(border_style='medium', color='00000000'), |
95
|
|
|
right=Side(border_style='medium', color='00000000'), |
96
|
|
|
bottom=Side(border_style='medium', color='00000000'), |
97
|
|
|
top=Side(border_style='medium', color='00000000') |
98
|
|
|
) |
99
|
|
|
b_border = Border( |
100
|
|
|
bottom=Side(border_style='medium', color='00000000'), |
101
|
|
|
) |
102
|
|
|
|
103
|
|
|
b_c_alignment = Alignment(vertical='bottom', |
104
|
|
|
horizontal='center', |
105
|
|
|
text_rotation=0, |
106
|
|
|
wrap_text=True, |
107
|
|
|
shrink_to_fit=False, |
108
|
|
|
indent=0) |
109
|
|
|
c_c_alignment = Alignment(vertical='center', |
110
|
|
|
horizontal='center', |
111
|
|
|
text_rotation=0, |
112
|
|
|
wrap_text=True, |
113
|
|
|
shrink_to_fit=False, |
114
|
|
|
indent=0) |
115
|
|
|
b_r_alignment = Alignment(vertical='bottom', |
116
|
|
|
horizontal='right', |
117
|
|
|
text_rotation=0, |
118
|
|
|
wrap_text=True, |
119
|
|
|
shrink_to_fit=False, |
120
|
|
|
indent=0) |
121
|
|
|
|
122
|
|
|
# Img |
123
|
|
|
img = Image("excelexporters/myems.png") |
124
|
|
|
ws.add_image(img, 'A1') |
125
|
|
|
|
126
|
|
|
# Title |
127
|
|
|
ws['B3'].alignment = b_r_alignment |
128
|
|
|
ws['B3'] = _('Space') + ':' |
129
|
|
|
ws['C3'].border = b_border |
130
|
|
|
ws['C3'].alignment = b_c_alignment |
131
|
|
|
ws['C3'] = space_name |
132
|
|
|
|
133
|
|
|
ws['B4'].alignment = b_r_alignment |
134
|
|
|
ws['B4'] = _('Reporting Start Datetime') + ':' |
135
|
|
|
ws['C4'].border = b_border |
136
|
|
|
ws['C4'].alignment = b_c_alignment |
137
|
|
|
ws['C4'] = reporting_start_datetime_local |
138
|
|
|
|
139
|
|
|
ws['B5'].alignment = b_r_alignment |
140
|
|
|
ws['B5'] = _('Reporting End Datetime') + ':' |
141
|
|
|
ws['C5'].border = b_border |
142
|
|
|
ws['C5'].alignment = b_c_alignment |
143
|
|
|
ws['C5'] = reporting_end_datetime_local |
144
|
|
|
|
145
|
|
|
# Title |
146
|
|
|
ws['B6'].border = f_border |
147
|
|
|
ws['B6'].font = name_font |
148
|
|
|
ws['B6'].alignment = c_c_alignment |
149
|
|
|
ws['B6'].fill = table_fill |
150
|
|
|
ws['B6'] = _('Name') |
151
|
|
|
|
152
|
|
|
ws['C6'].border = f_border |
153
|
|
|
ws['C6'].alignment = c_c_alignment |
154
|
|
|
ws['C6'].font = name_font |
155
|
|
|
ws['C6'].fill = table_fill |
156
|
|
|
ws['C6'] = _('Space') |
157
|
|
|
|
158
|
|
|
ca_len = len(report['energycategories']) |
159
|
|
|
|
160
|
|
|
for i in range(0, ca_len): |
161
|
|
|
col = chr(ord('D') + i) |
162
|
|
|
ws[col + '6'].fill = table_fill |
163
|
|
|
ws[col + '6'].font = name_font |
164
|
|
|
ws[col + '6'].alignment = c_c_alignment |
165
|
|
|
ws[col + '6'] = report['energycategories'][i]['name'] + \ |
166
|
|
|
" (" + report['energycategories'][i]['unit_of_measure'] + ")" |
167
|
|
|
ws[col + '6'].border = f_border |
168
|
|
|
|
169
|
|
|
current_row_number = 7 |
170
|
|
|
for i in range(0, len(report['combined_equipments'])): |
171
|
|
|
|
172
|
|
|
ws['B' + str(current_row_number)].font = title_font |
|
|
|
|
173
|
|
|
ws['B' + str(current_row_number)].border = f_border |
174
|
|
|
ws['B' + str(current_row_number)].alignment = c_c_alignment |
175
|
|
|
ws['B' + str(current_row_number)] = report['combined_equipments'][i]['combined_equipment_name'] |
176
|
|
|
|
177
|
|
|
ws['C' + str(current_row_number)].font = title_font |
178
|
|
|
ws['C' + str(current_row_number)].border = f_border |
179
|
|
|
ws['C' + str(current_row_number)].alignment = c_c_alignment |
180
|
|
|
ws['C' + str(current_row_number)] = report['combined_equipments'][i]['space_name'] |
181
|
|
|
|
182
|
|
|
ca_len = len(report['combined_equipments'][i]['values']) |
183
|
|
|
for j in range(0, ca_len): |
184
|
|
|
col = chr(ord('D') + j) |
185
|
|
|
ws[col + str(current_row_number)].font = data_font |
186
|
|
|
ws[col + str(current_row_number)].border = f_border |
187
|
|
|
ws[col + str(current_row_number)].alignment = c_c_alignment |
188
|
|
|
ws[col + str(current_row_number)] = round(report['combined_equipments'][i]['values'][j], 2) |
189
|
|
|
|
190
|
|
|
current_row_number += 1 |
191
|
|
|
|
192
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
193
|
|
|
wb.save(filename) |
194
|
|
|
|
195
|
|
|
return filename |
196
|
|
|
|