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 Border, Side, Alignment, Font |
||
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 | 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 | print(str(ex)) |
||
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 | print(str(ex)) |
||
49 | return base64_message |
||
50 | |||
51 | |||
52 | def generate_excel(report, space_name, language): |
||
53 | trans = get_translation(language) |
||
54 | trans.install() |
||
55 | _ = trans.gettext |
||
56 | wb = Workbook() |
||
57 | ws = wb.active |
||
58 | ws.title = "EquipmentTracking" |
||
59 | |||
60 | # Row height |
||
61 | ws.row_dimensions[1].height = 118 |
||
62 | for i in range(2, 5000 + 1): |
||
63 | ws.row_dimensions[i].height = 30 |
||
64 | # Col width |
||
65 | ws.column_dimensions['A'].width = 1 |
||
66 | |||
67 | # Font |
||
68 | name_font = Font(name='Arial', size=15, bold=True) |
||
69 | title_font = Font(name='Arial', size=15, bold=True) |
||
70 | |||
71 | f_border = Border(left=Side(border_style='medium'), |
||
72 | right=Side(border_style='medium'), |
||
73 | bottom=Side(border_style='medium'), |
||
74 | top=Side(border_style='medium') |
||
75 | ) |
||
76 | |||
77 | b_c_alignment = Alignment(vertical='bottom', |
||
78 | horizontal='center', |
||
79 | text_rotation=0, |
||
80 | wrap_text=True, |
||
81 | shrink_to_fit=False, |
||
82 | indent=0) |
||
83 | c_c_alignment = Alignment(vertical='center', |
||
84 | horizontal='center', |
||
85 | text_rotation=0, |
||
86 | wrap_text=True, |
||
87 | shrink_to_fit=False, |
||
88 | indent=0) |
||
89 | for i in range(ord('B'), ord('F')): |
||
90 | ws.column_dimensions[chr(i)].width = 30.0 |
||
91 | |||
92 | # Img |
||
93 | img = Image("excelexporters/myems.png") |
||
94 | ws.add_image(img, 'A1') |
||
95 | |||
96 | # Title |
||
97 | ws['B3'].border = f_border |
||
98 | ws['B3'].font = name_font |
||
99 | ws['B3'].alignment = b_c_alignment |
||
100 | ws['B3'] = _('Name') + ':' |
||
101 | |||
102 | ws['C3'].border = f_border |
||
103 | ws['C3'].alignment = b_c_alignment |
||
104 | ws['C3'].font = name_font |
||
105 | ws['C3'] = _('Space') + ':' |
||
106 | |||
107 | ws['D3'].border = f_border |
||
108 | ws['D3'].font = name_font |
||
109 | ws['D3'].alignment = b_c_alignment |
||
110 | ws['D3'] = _('Cost Center') + ':' |
||
111 | |||
112 | ws['E3'].border = f_border |
||
113 | ws['E3'].alignment = b_c_alignment |
||
114 | ws['E3'].font = name_font |
||
115 | ws['E3'] = _('Description') + ':' |
||
116 | |||
117 | current_row_number = 4 |
||
118 | for i in range(0, len(report['equipments'])): |
||
119 | |||
120 | ws['B' + str(current_row_number)].font = title_font |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
121 | ws['B' + str(current_row_number)].border = f_border |
||
122 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
123 | ws['B' + str(current_row_number)] = report['equipments'][i]['equipment_name'] |
||
124 | |||
125 | ws['C' + str(current_row_number)].font = title_font |
||
126 | ws['C' + str(current_row_number)].border = f_border |
||
127 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
128 | ws['C' + str(current_row_number)] = report['equipments'][i]['space_name'] |
||
129 | |||
130 | ws['D' + str(current_row_number)].font = title_font |
||
131 | ws['D' + str(current_row_number)].border = f_border |
||
132 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
133 | ws['D' + str(current_row_number)] = report['equipments'][i]['cost_center_name'] |
||
134 | |||
135 | ws['E' + str(current_row_number)].font = title_font |
||
136 | ws['E' + str(current_row_number)].border = f_border |
||
137 | ws['E' + str(current_row_number)].alignment = c_c_alignment |
||
138 | ws['E' + str(current_row_number)] = report['equipments'][i]['description'] |
||
139 | current_row_number += 1 |
||
140 | |||
141 | filename = str(uuid.uuid4()) + '.xlsx' |
||
142 | wb.save(filename) |
||
143 | |||
144 | return filename |
||
145 |