Total Complexity | 47 |
Total Lines | 549 |
Duplicated Lines | 70.67 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like excelexporters.tenantsaving often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import base64 |
||
2 | import uuid |
||
3 | import os |
||
4 | from openpyxl.chart import ( |
||
5 | PieChart, |
||
6 | BarChart, |
||
7 | Reference, |
||
8 | ) |
||
9 | from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
||
10 | from openpyxl.drawing.image import Image |
||
11 | from openpyxl import Workbook |
||
12 | from openpyxl.chart.label import DataLabelList |
||
13 | |||
14 | |||
15 | #################################################################################################################### |
||
16 | # PROCEDURES |
||
17 | # Step 1: Validate the report data |
||
18 | # Step 2: Generate excel file |
||
19 | # Step 3: Encode the excel file bytes to Base64 |
||
20 | #################################################################################################################### |
||
21 | |||
22 | |||
23 | View Code Duplication | def export(report, |
|
|
|||
24 | name, |
||
25 | reporting_start_datetime_local, |
||
26 | reporting_end_datetime_local, |
||
27 | period_type): |
||
28 | #################################################################################################################### |
||
29 | # Step 1: Validate the report data |
||
30 | #################################################################################################################### |
||
31 | if report is None: |
||
32 | return None |
||
33 | print(report) |
||
34 | |||
35 | #################################################################################################################### |
||
36 | # Step 2: Generate excel file from the report data |
||
37 | #################################################################################################################### |
||
38 | filename = generate_excel(report, |
||
39 | name, |
||
40 | reporting_start_datetime_local, |
||
41 | reporting_end_datetime_local, |
||
42 | period_type) |
||
43 | #################################################################################################################### |
||
44 | # Step 3: Encode the excel file to Base64 |
||
45 | #################################################################################################################### |
||
46 | try: |
||
47 | with open(filename, 'rb') as binary_file: |
||
48 | binary_file_data = binary_file.read() |
||
49 | except IOError as ex: |
||
50 | pass |
||
51 | |||
52 | # Base64 encode the bytes |
||
53 | base64_encoded_data = base64.b64encode(binary_file_data) |
||
54 | # get the Base64 encoded data using human-readable characters. |
||
55 | base64_message = base64_encoded_data.decode('utf-8') |
||
56 | # delete the file from server |
||
57 | try: |
||
58 | os.remove(filename) |
||
59 | except NotImplementedError as ex: |
||
60 | pass |
||
61 | return base64_message |
||
62 | |||
63 | |||
64 | def generate_excel(report, |
||
65 | name, |
||
66 | reporting_start_datetime_local, |
||
67 | reporting_end_datetime_local, |
||
68 | period_type): |
||
69 | wb = Workbook() |
||
70 | ws = wb.active |
||
71 | |||
72 | # Row height |
||
73 | ws.row_dimensions[1].height = 118 |
||
74 | for i in range(2, 2000 + 1): |
||
75 | ws.row_dimensions[i].height = 30 |
||
76 | |||
77 | # Col width |
||
78 | ws.column_dimensions['A'].width = 1.5 |
||
79 | |||
80 | ws.column_dimensions['B'].width = 25.0 |
||
81 | |||
82 | for i in range(ord('C'), ord('I')): |
||
83 | ws.column_dimensions[chr(i)].width = 25.0 |
||
84 | |||
85 | # Font |
||
86 | name_font = Font(name='Constantia', size=15, bold=True) |
||
87 | name_small_font = Font(name='Constantia', size=10, bold=True) |
||
88 | title_font = Font(name='宋体', size=15, bold=True) |
||
89 | title_small_font = Font(name='宋体', size=10, bold=True) |
||
90 | data_font = Font(name='Franklin Gothic Book', size=11) |
||
91 | |||
92 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
||
93 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
||
94 | right=Side(border_style='medium', color='00000000'), |
||
95 | bottom=Side(border_style='medium', color='00000000'), |
||
96 | top=Side(border_style='medium', color='00000000') |
||
97 | ) |
||
98 | b_border = Border( |
||
99 | bottom=Side(border_style='medium', color='00000000'), |
||
100 | ) |
||
101 | |||
102 | b_c_alignment = Alignment(vertical='bottom', |
||
103 | horizontal='center', |
||
104 | text_rotation=0, |
||
105 | wrap_text=False, |
||
106 | shrink_to_fit=False, |
||
107 | indent=0) |
||
108 | c_c_alignment = Alignment(vertical='center', |
||
109 | horizontal='center', |
||
110 | text_rotation=0, |
||
111 | wrap_text=False, |
||
112 | shrink_to_fit=False, |
||
113 | indent=0) |
||
114 | b_r_alignment = Alignment(vertical='bottom', |
||
115 | horizontal='right', |
||
116 | text_rotation=0, |
||
117 | wrap_text=False, |
||
118 | shrink_to_fit=False, |
||
119 | indent=0) |
||
120 | c_r_alignment = Alignment(vertical='bottom', |
||
121 | horizontal='center', |
||
122 | text_rotation=0, |
||
123 | wrap_text=False, |
||
124 | shrink_to_fit=False, |
||
125 | indent=0) |
||
126 | # Img |
||
127 | img = Image("excelexporters/myems.png") |
||
128 | # img = Image("myems.png") |
||
129 | ws.add_image(img, 'B1') |
||
130 | |||
131 | # Title |
||
132 | ws['B3'].font = name_font |
||
133 | ws['B3'].alignment = b_r_alignment |
||
134 | ws['B3'] = 'Name:' |
||
135 | ws['C3'].border = b_border |
||
136 | ws['C3'].alignment = b_c_alignment |
||
137 | ws['C3'].font = name_font |
||
138 | ws['C3'] = name |
||
139 | |||
140 | ws['D3'].font = name_font |
||
141 | ws['D3'].alignment = b_r_alignment |
||
142 | ws['D3'] = 'Period:' |
||
143 | ws['E3'].border = b_border |
||
144 | ws['E3'].alignment = b_c_alignment |
||
145 | ws['E3'].font = name_font |
||
146 | ws['E3'] = period_type |
||
147 | |||
148 | ws['F3'].font = name_font |
||
149 | ws['F3'].alignment = b_r_alignment |
||
150 | ws['F3'] = 'Date:' |
||
151 | ws.merge_cells("G3:J3") |
||
152 | for i in range(ord('G'), ord('K')): |
||
153 | ws[chr(i) + '3'].border = b_border |
||
154 | ws['G3'].alignment = b_c_alignment |
||
155 | ws['G3'].font = name_font |
||
156 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
||
157 | |||
158 | if "reporting_period" not in report.keys() or \ |
||
159 | "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
||
160 | filename = str(uuid.uuid4()) + '.xlsx' |
||
161 | wb.save(filename) |
||
162 | |||
163 | return filename |
||
164 | |||
165 | ################################## |
||
166 | |||
167 | current_row_number = 6 |
||
168 | |||
169 | reporting_period_data = report['reporting_period'] |
||
170 | |||
171 | has_names_data_flag = True |
||
172 | |||
173 | if "names" not in reporting_period_data.keys() or \ |
||
174 | reporting_period_data['names'] is None or \ |
||
175 | len(reporting_period_data['names']) == 0: |
||
176 | has_names_data_flag = False |
||
177 | |||
178 | View Code Duplication | if has_names_data_flag: |
|
179 | ws['B' + str(current_row_number)].font = title_font |
||
180 | ws['B' + str(current_row_number)] = name + ' 报告期节约' |
||
181 | |||
182 | current_row_number += 1 |
||
183 | |||
184 | category = reporting_period_data['names'] |
||
185 | ca_len = len(category) |
||
186 | |||
187 | ws['B' + str(current_row_number)].fill = table_fill |
||
188 | |||
189 | col = 'C' |
||
190 | |||
191 | for i in range(0, ca_len): |
||
192 | ws[col + str(current_row_number)].fill = table_fill |
||
193 | ws[col + str(current_row_number)].font = name_small_font |
||
194 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
195 | ws[col + str(current_row_number)].border = f_border |
||
196 | ws[col + str(current_row_number)] = \ |
||
197 | reporting_period_data['names'][i] + " (基线-实际) (" + reporting_period_data['units'][i] + ")" |
||
198 | |||
199 | col = chr(ord(col) + 1) |
||
200 | |||
201 | ws[col + str(current_row_number)].fill = table_fill |
||
202 | ws[col + str(current_row_number)].font = name_small_font |
||
203 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
204 | ws[col + str(current_row_number)].border = f_border |
||
205 | ws[col + str(current_row_number)] = '吨标准煤 (基线-实际) (TCE)' |
||
206 | |||
207 | col = chr(ord(col) + 1) |
||
208 | |||
209 | ws[col + str(current_row_number)].fill = table_fill |
||
210 | ws[col + str(current_row_number)].font = name_small_font |
||
211 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
212 | ws[col + str(current_row_number)].border = f_border |
||
213 | ws[col + str(current_row_number)] = '吨二氧化碳排放 (基线-实际) (TCO2E)' |
||
214 | |||
215 | col = chr(ord(col) + 1) |
||
216 | |||
217 | current_row_number += 1 |
||
218 | |||
219 | ws['B' + str(current_row_number)].font = title_font |
||
220 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
221 | ws['B' + str(current_row_number)].border = f_border |
||
222 | ws['B' + str(current_row_number)] = '节约' |
||
223 | |||
224 | col = 'C' |
||
225 | |||
226 | for i in range(0, ca_len): |
||
227 | ws[col + str(current_row_number)].font = name_font |
||
228 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
229 | ws[col + str(current_row_number)].border = f_border |
||
230 | ws[col + str(current_row_number)] = round(reporting_period_data['subtotals_saving'][i], 2) |
||
231 | |||
232 | col = chr(ord(col) + 1) |
||
233 | |||
234 | ws[col + str(current_row_number)].font = name_font |
||
235 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
236 | ws[col + str(current_row_number)].border = f_border |
||
237 | ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgce_saving'], 2) |
||
238 | |||
239 | col = chr(ord(col) + 1) |
||
240 | |||
241 | ws[col + str(current_row_number)].font = name_font |
||
242 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
243 | ws[col + str(current_row_number)].border = f_border |
||
244 | ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgco2e_saving'], 2) |
||
245 | |||
246 | col = chr(ord(col) + 1) |
||
247 | |||
248 | current_row_number += 1 |
||
249 | |||
250 | ws['B' + str(current_row_number)].font = title_font |
||
251 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
252 | ws['B' + str(current_row_number)].border = f_border |
||
253 | ws['B' + str(current_row_number)] = '单位面积值' |
||
254 | |||
255 | col = 'C' |
||
256 | |||
257 | for i in range(0, ca_len): |
||
258 | ws[col + str(current_row_number)].font = name_font |
||
259 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
260 | ws[col + str(current_row_number)].border = f_border |
||
261 | ws[col + str(current_row_number)] = round(reporting_period_data['subtotals_per_unit_area_saving'][i], 2) |
||
262 | |||
263 | col = chr(ord(col) + 1) |
||
264 | |||
265 | ws[col + str(current_row_number)].font = name_font |
||
266 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
267 | ws[col + str(current_row_number)].border = f_border |
||
268 | ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgco2e_per_unit_area_saving'], 2) |
||
269 | |||
270 | col = chr(ord(col) + 1) |
||
271 | |||
272 | ws[col + str(current_row_number)].font = name_font |
||
273 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
274 | ws[col + str(current_row_number)].border = f_border |
||
275 | ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgce_per_unit_area_saving'], 2) |
||
276 | |||
277 | col = chr(ord(col) + 1) |
||
278 | |||
279 | current_row_number += 1 |
||
280 | |||
281 | ws['B' + str(current_row_number)].font = title_font |
||
282 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
283 | ws['B' + str(current_row_number)].border = f_border |
||
284 | ws['B' + str(current_row_number)] = '环比' |
||
285 | |||
286 | col = 'C' |
||
287 | |||
288 | for i in range(0, ca_len): |
||
289 | ws[col + str(current_row_number)].font = name_font |
||
290 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
291 | ws[col + str(current_row_number)].border = f_border |
||
292 | ws[col + str(current_row_number)] = str( |
||
293 | round(reporting_period_data['increment_rates_saving'][i] * 100, 2)) + '%' \ |
||
294 | if reporting_period_data['increment_rates_saving'][i] is not None else '-' |
||
295 | |||
296 | col = chr(ord(col) + 1) |
||
297 | |||
298 | ws[col + str(current_row_number)].font = name_font |
||
299 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
300 | ws[col + str(current_row_number)].border = f_border |
||
301 | ws[col + str(current_row_number)] = str( |
||
302 | round(reporting_period_data['increment_rate_in_kgce_saving'] * 100, 2)) + '%' \ |
||
303 | if reporting_period_data['increment_rate_in_kgce_saving'] is not None else '-' |
||
304 | |||
305 | col = chr(ord(col) + 1) |
||
306 | |||
307 | ws[col + str(current_row_number)].font = name_font |
||
308 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
309 | ws[col + str(current_row_number)].border = f_border |
||
310 | ws[col + str(current_row_number)] = str( |
||
311 | round(reporting_period_data['increment_rate_in_kgco2e_saving'] * 100, 2)) + '%' \ |
||
312 | if reporting_period_data['increment_rate_in_kgco2e_saving'] is not None else '-' |
||
313 | |||
314 | col = chr(ord(col) + 1) |
||
315 | |||
316 | current_row_number += 2 |
||
317 | |||
318 | ws['B' + str(current_row_number)].font = title_font |
||
319 | ws['B' + str(current_row_number)] = name + ' 吨标准煤(TCE)占比' |
||
320 | |||
321 | current_row_number += 1 |
||
322 | table_start_row_number = current_row_number |
||
323 | chart_start_row_number = current_row_number |
||
324 | |||
325 | ws['B' + str(current_row_number)].fill = table_fill |
||
326 | |||
327 | ws['C' + str(current_row_number)].fill = table_fill |
||
328 | ws['C' + str(current_row_number)].font = name_small_font |
||
329 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
330 | ws['C' + str(current_row_number)].border = f_border |
||
331 | ws['C' + str(current_row_number)] = '吨标准煤(TCE)占比' |
||
332 | |||
333 | current_row_number += 1 |
||
334 | |||
335 | for i in range(0, ca_len): |
||
336 | ws['B' + str(current_row_number)].font = title_font |
||
337 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
338 | ws['B' + str(current_row_number)].border = f_border |
||
339 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
||
340 | |||
341 | ws['C' + str(current_row_number)].font = name_font |
||
342 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
343 | ws['C' + str(current_row_number)].border = f_border |
||
344 | ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals_in_kgce_saving'][i], 2) |
||
345 | |||
346 | current_row_number += 1 |
||
347 | |||
348 | table_end_row_number = current_row_number - 1 |
||
349 | |||
350 | if ca_len < 4: |
||
351 | current_row_number = current_row_number - ca_len + 4 |
||
352 | |||
353 | current_row_number += 1 |
||
354 | |||
355 | pie = PieChart() |
||
356 | pie.title = '吨标准煤(TCE)占比' |
||
357 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
358 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
||
359 | pie.add_data(pie_data, titles_from_data=True) |
||
360 | pie.set_categories(labels) |
||
361 | pie.height = 5.25 |
||
362 | pie.width = 9 |
||
363 | s1 = pie.series[0] |
||
364 | s1.dLbls = DataLabelList() |
||
365 | s1.dLbls.showCatName = False |
||
366 | s1.dLbls.showVal = True |
||
367 | s1.dLbls.showPercent = True |
||
368 | ws.add_chart(pie, 'D' + str(chart_start_row_number)) |
||
369 | |||
370 | ws['B' + str(current_row_number)].font = title_font |
||
371 | ws['B' + str(current_row_number)] = name + ' 吨二氧化碳排放(TCO2E)占比' |
||
372 | |||
373 | current_row_number += 1 |
||
374 | table_start_row_number = current_row_number |
||
375 | chart_start_row_number = current_row_number |
||
376 | |||
377 | ws['B' + str(current_row_number)].fill = table_fill |
||
378 | |||
379 | ws['C' + str(current_row_number)].fill = table_fill |
||
380 | ws['C' + str(current_row_number)].font = name_small_font |
||
381 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
382 | ws['C' + str(current_row_number)].border = f_border |
||
383 | ws['C' + str(current_row_number)] = '吨二氧化碳排放(TCO2E)占比' |
||
384 | |||
385 | current_row_number += 1 |
||
386 | |||
387 | for i in range(0, ca_len): |
||
388 | ws['B' + str(current_row_number)].font = title_font |
||
389 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
390 | ws['B' + str(current_row_number)].border = f_border |
||
391 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
||
392 | |||
393 | ws['C' + str(current_row_number)].font = name_font |
||
394 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
395 | ws['C' + str(current_row_number)].border = f_border |
||
396 | ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals_in_kgco2e_saving'][i], 2) |
||
397 | |||
398 | current_row_number += 1 |
||
399 | |||
400 | table_end_row_number = current_row_number - 1 |
||
401 | |||
402 | if ca_len < 4: |
||
403 | current_row_number = current_row_number - ca_len + 4 |
||
404 | |||
405 | current_row_number += 1 |
||
406 | |||
407 | pie = PieChart() |
||
408 | pie.title = '吨二氧化碳排放(TCO2E)占比' |
||
409 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
410 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
||
411 | pie.add_data(pie_data, titles_from_data=True) |
||
412 | pie.set_categories(labels) |
||
413 | pie.height = 5.25 |
||
414 | pie.width = 9 |
||
415 | s1 = pie.series[0] |
||
416 | s1.dLbls = DataLabelList() |
||
417 | s1.dLbls.showCatName = False |
||
418 | s1.dLbls.showVal = True |
||
419 | s1.dLbls.showPercent = True |
||
420 | ws.add_chart(pie, 'D' + str(chart_start_row_number)) |
||
421 | |||
422 | ############################################# |
||
423 | |||
424 | has_values_saving_data = True |
||
425 | has_timestamps_data = True |
||
426 | |||
427 | if 'values_saving' not in reporting_period_data.keys() or \ |
||
428 | reporting_period_data['values_saving'] is None or \ |
||
429 | len(reporting_period_data['values_saving']) == 0: |
||
430 | has_values_saving_data = False |
||
431 | |||
432 | if 'timestamps' not in reporting_period_data.keys() or \ |
||
433 | reporting_period_data['timestamps'] is None or \ |
||
434 | len(reporting_period_data['timestamps']) == 0 or \ |
||
435 | len(reporting_period_data['timestamps'][0]) == 0: |
||
436 | has_timestamps_data = False |
||
437 | |||
438 | View Code Duplication | if has_values_saving_data and has_timestamps_data: |
|
439 | ca_len = len(reporting_period_data['names']) |
||
440 | time = reporting_period_data['timestamps'][0] |
||
441 | |||
442 | ws['B' + str(current_row_number)].font = title_font |
||
443 | ws['B' + str(current_row_number)] = name + ' 详细数据' |
||
444 | |||
445 | current_row_number += 1 |
||
446 | |||
447 | chart_start_row_number = current_row_number |
||
448 | |||
449 | current_row_number += ca_len * 5 |
||
450 | table_start_row_number = current_row_number |
||
451 | |||
452 | ws['B' + str(current_row_number)].fill = table_fill |
||
453 | ws['B' + str(current_row_number)].font = title_font |
||
454 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
455 | ws['B' + str(current_row_number)].border = f_border |
||
456 | ws['B' + str(current_row_number)] = '日期时间' |
||
457 | |||
458 | col = 'C' |
||
459 | |||
460 | for i in range(0, ca_len): |
||
461 | ws[col + str(current_row_number)].fill = table_fill |
||
462 | ws[col + str(current_row_number)].font = title_font |
||
463 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
464 | ws[col + str(current_row_number)].border = f_border |
||
465 | ws[col + str(current_row_number)] = \ |
||
466 | reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
467 | col = chr(ord(col) + 1) |
||
468 | |||
469 | current_row_number += 1 |
||
470 | |||
471 | for i in range(0, len(time)): |
||
472 | ws['B' + str(current_row_number)].font = title_font |
||
473 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
474 | ws['B' + str(current_row_number)].border = f_border |
||
475 | ws['B' + str(current_row_number)] = time[i] |
||
476 | |||
477 | col = 'C' |
||
478 | for j in range(0, ca_len): |
||
479 | ws[col + str(current_row_number)].font = title_font |
||
480 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
481 | ws[col + str(current_row_number)].border = f_border |
||
482 | ws[col + str(current_row_number)] = round(reporting_period_data['values_saving'][j][i], 2) \ |
||
483 | if reporting_period_data['values_saving'][j][i] is not None else 0.00 |
||
484 | col = chr(ord(col) + 1) |
||
485 | |||
486 | current_row_number += 1 |
||
487 | |||
488 | table_end_row_number = current_row_number - 1 |
||
489 | |||
490 | ws['B' + str(current_row_number)].font = title_font |
||
491 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
492 | ws['B' + str(current_row_number)].border = f_border |
||
493 | ws['B' + str(current_row_number)] = '小计' |
||
494 | |||
495 | col = 'C' |
||
496 | |||
497 | for i in range(0, ca_len): |
||
498 | ws[col + str(current_row_number)].font = title_font |
||
499 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
500 | ws[col + str(current_row_number)].border = f_border |
||
501 | ws[col + str(current_row_number)] = round(reporting_period_data['subtotals_saving'][i], 2) |
||
502 | col = chr(ord(col) + 1) |
||
503 | |||
504 | current_row_number += 2 |
||
505 | |||
506 | format_time_width_number = 1.0 |
||
507 | min_len_number = 1.0 |
||
508 | min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0 |
||
509 | |||
510 | if period_type == 'hourly': |
||
511 | format_time_width_number = 4.0 |
||
512 | min_len_number = 2 |
||
513 | min_width_number = 12.0 |
||
514 | elif period_type == 'daily': |
||
515 | format_time_width_number = 2.5 |
||
516 | min_len_number = 4 |
||
517 | min_width_number = 14.0 |
||
518 | elif period_type == 'monthly': |
||
519 | format_time_width_number = 2.1 |
||
520 | min_len_number = 4 |
||
521 | min_width_number = 12.4 |
||
522 | elif period_type == 'yearly': |
||
523 | format_time_width_number = 1.5 |
||
524 | min_len_number = 5 |
||
525 | min_width_number = 11.5 |
||
526 | |||
527 | for i in range(0, ca_len): |
||
528 | bar = BarChart() |
||
529 | bar.title = \ |
||
530 | reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
531 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
532 | bar_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number) |
||
533 | bar.add_data(bar_data, titles_from_data=True) |
||
534 | bar.set_categories(labels) |
||
535 | bar.height = 5.25 |
||
536 | bar.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number |
||
537 | bar.dLbls = DataLabelList() |
||
538 | bar.dLbls.showVal = True |
||
539 | bar.dLbls.showPercent = True |
||
540 | chart_col = 'B' |
||
541 | chart_cell = chart_col + str(chart_start_row_number) |
||
542 | chart_start_row_number += 5 |
||
543 | ws.add_chart(bar, chart_cell) |
||
544 | |||
545 | filename = str(uuid.uuid4()) + '.xlsx' |
||
546 | wb.save(filename) |
||
547 | |||
548 | return filename |
||
549 |