1 | import base64 |
||
2 | from core.utilities import get_translation |
||
3 | import os |
||
4 | import uuid |
||
5 | from openpyxl import Workbook |
||
6 | from openpyxl.drawing.image import Image |
||
7 | from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
||
8 | from openpyxl.utils import column_index_from_string, get_column_letter |
||
9 | |||
10 | |||
11 | ######################################################################################################################## |
||
12 | # PROCEDURES |
||
13 | # Step 1: Validate the report data |
||
14 | # Step 2: Generate excel file from the report data |
||
15 | # Step 3: Encode the excel file to Base64 |
||
16 | ######################################################################################################################## |
||
17 | |||
18 | def export(result, space_name, reporting_start_datetime_local, reporting_end_datetime_local, language): |
||
19 | #################################################################################################################### |
||
20 | # Step 1: Validate the report data |
||
21 | #################################################################################################################### |
||
22 | if result is None: |
||
23 | return None |
||
24 | |||
25 | #################################################################################################################### |
||
26 | # Step 2: Generate excel file from the report data |
||
27 | #################################################################################################################### |
||
28 | filename = generate_excel(result, |
||
29 | space_name, |
||
30 | reporting_start_datetime_local, |
||
31 | reporting_end_datetime_local, |
||
32 | language) |
||
33 | #################################################################################################################### |
||
34 | # Step 3: Encode the excel file to Base64 |
||
35 | #################################################################################################################### |
||
36 | binary_file_data = b'' |
||
37 | try: |
||
38 | with open(filename, 'rb') as binary_file: |
||
39 | binary_file_data = binary_file.read() |
||
40 | except IOError as ex: |
||
41 | print(str(ex)) |
||
42 | |||
43 | # Base64 encode the bytes |
||
44 | base64_encoded_data = base64.b64encode(binary_file_data) |
||
45 | # get the Base64 encoded data using human-readable characters. |
||
46 | base64_message = base64_encoded_data.decode('utf-8') |
||
47 | # delete the file from server |
||
48 | try: |
||
49 | os.remove(filename) |
||
50 | except NotImplementedError as ex: |
||
51 | print(str(ex)) |
||
52 | return base64_message |
||
53 | |||
54 | |||
55 | def generate_excel(report, space_name, reporting_start_datetime_local, reporting_end_datetime_local, language): |
||
56 | |||
57 | trans = get_translation(language) |
||
58 | trans.install() |
||
59 | _ = trans.gettext |
||
60 | |||
61 | wb = Workbook() |
||
62 | ws = wb.active |
||
63 | ws.title = "TenantBatch" |
||
64 | |||
65 | # Row height |
||
66 | ws.row_dimensions[1].height = 102 |
||
67 | for i in range(2, 5 + 1): |
||
68 | ws.row_dimensions[i].height = 42 |
||
69 | |||
70 | for i in range(6, len(report['tenants']) + 15): |
||
71 | ws.row_dimensions[i].height = 60 |
||
72 | |||
73 | # Col width |
||
74 | ws.column_dimensions['A'].width = 1.5 |
||
75 | |||
76 | ws.column_dimensions['B'].width = 25.0 |
||
77 | |||
78 | for i in range(ord('C'), ord('L')): |
||
79 | ws.column_dimensions[chr(i)].width = 15.0 |
||
80 | |||
81 | # Font |
||
82 | name_font = Font(name='Arial', size=15, bold=True) |
||
83 | title_font = Font(name='Arial', size=15, bold=True) |
||
84 | |||
85 | table_fill = PatternFill(fill_type='solid', fgColor='90ee90') |
||
86 | f_border = Border(left=Side(border_style='medium'), |
||
87 | right=Side(border_style='medium'), |
||
88 | bottom=Side(border_style='medium'), |
||
89 | top=Side(border_style='medium') |
||
90 | ) |
||
91 | b_border = Border( |
||
92 | bottom=Side(border_style='medium'), |
||
93 | ) |
||
94 | |||
95 | b_c_alignment = Alignment(vertical='bottom', |
||
96 | horizontal='center', |
||
97 | text_rotation=0, |
||
98 | wrap_text=True, |
||
99 | shrink_to_fit=False, |
||
100 | indent=0) |
||
101 | c_c_alignment = Alignment(vertical='center', |
||
102 | horizontal='center', |
||
103 | text_rotation=0, |
||
104 | wrap_text=True, |
||
105 | shrink_to_fit=False, |
||
106 | indent=0) |
||
107 | b_r_alignment = Alignment(vertical='bottom', |
||
108 | horizontal='right', |
||
109 | text_rotation=0, |
||
110 | wrap_text=True, |
||
111 | shrink_to_fit=False, |
||
112 | indent=0) |
||
113 | |||
114 | # Img |
||
115 | img = Image("excelexporters/myems.png") |
||
116 | ws.add_image(img, 'A1') |
||
117 | |||
118 | # Title |
||
119 | ws['B3'].alignment = b_r_alignment |
||
120 | ws['B3'] = _('Space') + ':' |
||
121 | ws['C3'].border = b_border |
||
122 | ws['C3'].alignment = b_c_alignment |
||
123 | ws['C3'] = space_name |
||
124 | |||
125 | ws['B4'].alignment = b_r_alignment |
||
126 | ws['B4'] = _('Reporting Start Datetime') + ':' |
||
127 | ws['C4'].border = b_border |
||
128 | ws['C4'].alignment = b_c_alignment |
||
129 | ws['C4'] = reporting_start_datetime_local |
||
130 | |||
131 | ws['B5'].alignment = b_r_alignment |
||
132 | ws['B5'] = _('Reporting End Datetime') + ':' |
||
133 | ws['C5'].border = b_border |
||
134 | ws['C5'].alignment = b_c_alignment |
||
135 | ws['C5'] = reporting_end_datetime_local |
||
136 | |||
137 | # Title |
||
138 | ws['B7'].border = f_border |
||
139 | ws['B7'].font = name_font |
||
140 | ws['B7'].alignment = c_c_alignment |
||
141 | ws['B7'].fill = table_fill |
||
142 | ws['B7'] = _('Name') + ':' |
||
143 | |||
144 | ws['C7'].border = f_border |
||
145 | ws['C7'].alignment = c_c_alignment |
||
146 | ws['C7'].font = name_font |
||
147 | ws['C7'].fill = table_fill |
||
148 | ws['C7'] = _('Space') + ':' |
||
149 | |||
150 | ca_len = len(report['energycategories']) |
||
151 | |||
152 | for i in range(0, ca_len): |
||
153 | col = get_column_letter(column_index_from_string('D') + i * 2) |
||
154 | ws[col + '7'].fill = table_fill |
||
155 | ws[col + '7'].font = name_font |
||
156 | ws[col + '7'].alignment = c_c_alignment |
||
157 | ws[col + '7'] = report['energycategories'][i]['name'] + \ |
||
158 | " (" + report['energycategories'][i]['unit_of_measure'] + ")" |
||
159 | ws[col + '7'].border = f_border |
||
160 | |||
161 | col = get_column_letter(column_index_from_string(col) + 1) |
||
162 | ws[col + '7'].fill = table_fill |
||
163 | ws[col + '7'].font = name_font |
||
164 | ws[col + '7'].alignment = c_c_alignment |
||
165 | ws[col + '7'] = report['energycategories'][i]['name'] + \ |
||
166 | " " + _('Maximum Load') + " (" + report['energycategories'][i]['unit_of_measure'] + ")" |
||
167 | ws[col + '7'].border = f_border |
||
168 | |||
169 | current_row_number = 8 |
||
170 | for i in range(0, len(report['tenants'])): |
||
171 | |||
172 | ws['B' + str(current_row_number)].font = title_font |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
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['tenants'][i]['tenant_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['tenants'][i]['space_name'] |
||
181 | |||
182 | ca_len = len(report['tenants'][i]['values']) |
||
183 | for j in range(0, ca_len): |
||
184 | col = get_column_letter(column_index_from_string('D') + j * 2) |
||
185 | ws[col + str(current_row_number)].font = title_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)] = report['tenants'][i]['values'][j] |
||
189 | |||
190 | col = get_column_letter(column_index_from_string(col) + 1) |
||
191 | ws[col + str(current_row_number)].font = title_font |
||
192 | ws[col + str(current_row_number)].border = f_border |
||
193 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
194 | ws[col + str(current_row_number)] = report['tenants'][i]['maximum'][j] |
||
195 | current_row_number += 1 |
||
196 | |||
197 | filename = str(uuid.uuid4()) + '.xlsx' |
||
198 | wb.save(filename) |
||
199 | |||
200 | return filename |
||
201 |