Passed
Push — master ( f72fc8...32a04c )
by Guangyu
02:51 queued 12s
created

excelexporters.spacesaving   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 616
Duplicated Lines 6.33 %

Importance

Changes 0
Metric Value
eloc 459
dl 39
loc 616
rs 5.04
c 0
b 0
f 0
wmc 57

2 Functions

Rating   Name   Duplication   Size   Complexity  
B export() 39 39 5
F generate_excel() 0 552 52

How to fix   Duplicated Code    Complexity   

Duplicated Code

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:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like excelexporters.spacesaving 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,
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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)
0 ignored issues
show
introduced by
The variable binary_file_data does not seem to be defined for all execution paths.
Loading history...
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'
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable str does not seem to be defined.
Loading history...
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
    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
        ws['B' + str(current_row_number)].font = title_font
280
        ws['B' + str(current_row_number)].alignment = c_c_alignment
281
        ws['B' + str(current_row_number)].border = f_border
282
        ws['B' + str(current_row_number)] = '环比'
283
284
        col = 'C'
285
286
        for i in range(0, ca_len):
287
            ws[col + str(current_row_number)].font = name_font
288
            ws[col + str(current_row_number)].alignment = c_c_alignment
289
            ws[col + str(current_row_number)].border = f_border
290
            ws[col + str(current_row_number)] = str(
291
                round(reporting_period_data['increment_rates_saving'][i] * 100, 2)) + '%' \
292
                if reporting_period_data['increment_rates_saving'][i] is not None else '-'
293
294
            col = chr(ord(col) + 1)
295
296
        ws[col + str(current_row_number)].font = name_font
297
        ws[col + str(current_row_number)].alignment = c_c_alignment
298
        ws[col + str(current_row_number)].border = f_border
299
        ws[col + str(current_row_number)] = str(
300
            round(reporting_period_data['increment_rate_in_kgce_saving'] * 100, 2)) + '%' \
301
            if reporting_period_data['increment_rate_in_kgce_saving'] is not None else '-'
302
303
        col = chr(ord(col) + 1)
304
305
        ws[col + str(current_row_number)].font = name_font
306
        ws[col + str(current_row_number)].alignment = c_c_alignment
307
        ws[col + str(current_row_number)].border = f_border
308
        ws[col + str(current_row_number)] = str(
309
            round(reporting_period_data['increment_rate_in_kgco2e_saving'] * 100, 2)) + '%' \
310
            if reporting_period_data['increment_rate_in_kgco2e_saving'] is not None else '-'
311
312
        col = chr(ord(col) + 1)
313
314
        current_row_number += 2
315
316
        ws['B' + str(current_row_number)].font = title_font
317
        ws['B' + str(current_row_number)] = name + ' 吨标准煤(TCE)占比'
318
319
        current_row_number += 1
320
        table_start_row_number = current_row_number
321
        chart_start_row_number = current_row_number
322
323
        ws['B' + str(current_row_number)].fill = table_fill
324
325
        ws['C' + str(current_row_number)].fill = table_fill
326
        ws['C' + str(current_row_number)].font = name_small_font
327
        ws['C' + str(current_row_number)].alignment = c_c_alignment
328
        ws['C' + str(current_row_number)].border = f_border
329
        ws['C' + str(current_row_number)] = '吨标准煤(TCE)占比'
330
331
        current_row_number += 1
332
333
        for i in range(0, ca_len):
334
            ws['B' + str(current_row_number)].font = title_font
335
            ws['B' + str(current_row_number)].alignment = c_c_alignment
336
            ws['B' + str(current_row_number)].border = f_border
337
            ws['B' + str(current_row_number)] = reporting_period_data['names'][i]
338
339
            ws['C' + str(current_row_number)].font = name_font
340
            ws['C' + str(current_row_number)].alignment = c_c_alignment
341
            ws['C' + str(current_row_number)].border = f_border
342
            ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals_in_kgce_saving'][i], 2)
343
344
            current_row_number += 1
345
346
        table_end_row_number = current_row_number - 1
347
348
        if ca_len < 4:
349
            current_row_number = current_row_number - ca_len + 4
350
351
        current_row_number += 1
352
353
        pie = PieChart()
354
        pie.title = '吨标准煤(TCE)占比'
355
        labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
356
        pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number)
357
        pie.add_data(pie_data, titles_from_data=True)
358
        pie.set_categories(labels)
359
        pie.height = 5.25
360
        pie.width = 9
361
        s1 = pie.series[0]
362
        s1.dLbls = DataLabelList()
363
        s1.dLbls.showCatName = False
364
        s1.dLbls.showVal = True
365
        s1.dLbls.showPercent = True
366
        ws.add_chart(pie, 'D' + str(chart_start_row_number))
367
368
        ws['B' + str(current_row_number)].font = title_font
369
        ws['B' + str(current_row_number)] = name + ' 吨二氧化碳排放(TCO2E)占比'
370
371
        current_row_number += 1
372
        table_start_row_number = current_row_number
373
        chart_start_row_number = current_row_number
374
375
        ws['B' + str(current_row_number)].fill = table_fill
376
377
        ws['C' + str(current_row_number)].fill = table_fill
378
        ws['C' + str(current_row_number)].font = name_small_font
379
        ws['C' + str(current_row_number)].alignment = c_c_alignment
380
        ws['C' + str(current_row_number)].border = f_border
381
        ws['C' + str(current_row_number)] = '吨二氧化碳排放(TCO2E)占比'
382
383
        current_row_number += 1
384
385
        for i in range(0, ca_len):
386
            ws['B' + str(current_row_number)].font = title_font
387
            ws['B' + str(current_row_number)].alignment = c_c_alignment
388
            ws['B' + str(current_row_number)].border = f_border
389
            ws['B' + str(current_row_number)] = reporting_period_data['names'][i]
390
391
            ws['C' + str(current_row_number)].font = name_font
392
            ws['C' + str(current_row_number)].alignment = c_c_alignment
393
            ws['C' + str(current_row_number)].border = f_border
394
            ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals_in_kgco2e_saving'][i], 2)
395
396
            current_row_number += 1
397
398
        table_end_row_number = current_row_number - 1
399
400
        if ca_len < 4:
401
            current_row_number = current_row_number - ca_len + 4
402
403
        current_row_number += 1
404
405
        pie = PieChart()
406
        pie.title = '吨二氧化碳排放(TCO2E)占比'
407
        labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
408
        pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number)
409
        pie.add_data(pie_data, titles_from_data=True)
410
        pie.set_categories(labels)
411
        pie.height = 5.25
412
        pie.width = 9
413
        s1 = pie.series[0]
414
        s1.dLbls = DataLabelList()
415
        s1.dLbls.showCatName = False
416
        s1.dLbls.showVal = True
417
        s1.dLbls.showPercent = True
418
        ws.add_chart(pie, 'D' + str(chart_start_row_number))
419
420
    #############################################
421
422
    has_child_space_data_flag = True
423
424
    if 'child_space' not in report.keys() or \
425
            report['child_space'] is None or \
426
            'energy_category_names' not in report['child_space'].keys() or \
427
            report['child_space']['energy_category_names'] is None or \
428
            len(report['child_space']['energy_category_names']) == 0:
429
        has_child_space_data_flag = False
430
431
    if has_child_space_data_flag:
432
        child_space_data = report['child_space']
433
        ca_len = len(child_space_data['energy_category_names'])
434
435
        ws['B' + str(current_row_number)].font = title_font
436
        ws['B' + str(current_row_number)] = name + ' 子空间数据'
437
438
        current_row_number += 1
439
        table_start_row_number = current_row_number
440
441
        ws['B' + str(current_row_number)].fill = table_fill
442
        ws['B' + str(current_row_number)].font = name_font
443
        ws['B' + str(current_row_number)].alignment = c_c_alignment
444
        ws['B' + str(current_row_number)].border = f_border
445
        ws['B' + str(current_row_number)] = '子空间'
446
447
        col = 'C'
448
449
        for i in range(0, ca_len):
450
            ws[col + str(current_row_number)].fill = table_fill
451
            ws[col + str(current_row_number)].font = name_font
452
            ws[col + str(current_row_number)].alignment = c_c_alignment
453
            ws[col + str(current_row_number)].border = f_border
454
            ws[col + str(current_row_number)] = \
455
                child_space_data['energy_category_names'][i] + " (" + child_space_data['units'][i] + ")"
456
            col = chr(ord(col) + 1)
457
458
        current_row_number += 1
459
        ca_child_len = len(child_space_data['child_space_names_array'][0])
460
461
        for i in range(0, ca_child_len):
462
            ws['B' + str(current_row_number)].font = title_font
463
            ws['B' + str(current_row_number)].alignment = c_c_alignment
464
            ws['B' + str(current_row_number)].border = f_border
465
            ws['B' + str(current_row_number)] = child_space_data['child_space_names_array'][0][i]
466
            current_row_number += 1
467
468
        current_row_number -= ca_child_len
469
470
        for i in range(0, ca_child_len):
471
            col = 'C'
472
            for j in range(0, ca_len):
473
                ws[col + str(current_row_number)].font = name_font
474
                ws[col + str(current_row_number)].alignment = c_c_alignment
475
                ws[col + str(current_row_number)].border = f_border
476
                ws[col + str(current_row_number)] = child_space_data['subtotals_saving_array'][j][i]
477
                col = chr(ord(col) + 1)
478
479
            current_row_number += 1
480
481
        table_end_row_number = current_row_number - 1
482
483
        col = 'B'
484
485
        for i in range(0, ca_len):
486
            pie = PieChart()
487
            labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
488
            pie_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
489
            pie.add_data(pie_data, titles_from_data=True)
490
            pie.set_categories(labels)
491
            pie.title = reporting_period_data['names'][i] + " (" + \
492
                                                            reporting_period_data['units'][i] + ")"
493
            pie.height = 5.25
494
            pie.width = 9
495
            s1 = pie.series[0]
496
            s1.dLbls = DataLabelList()
497
            s1.dLbls.showCatName = False
498
            s1.dLbls.showVal = True
499
            s1.dLbls.showPercent = True
500
            ws.add_chart(pie, col + str(current_row_number))
501
            col = chr(ord(col) + 2)
502
503
        current_row_number += 6
504
505
    ################################
506
507
    has_values_saving_data = True
508
    has_timestamps_data = True
509
510
    if 'values_saving' not in reporting_period_data.keys() or \
511
            reporting_period_data['values_saving'] is None or \
512
            len(reporting_period_data['values_saving']) == 0:
513
        has_values_saving_data = False
514
515
    if 'timestamps' not in reporting_period_data.keys() or \
516
            reporting_period_data['timestamps'] is None or \
517
            len(reporting_period_data['timestamps']) == 0 or \
518
            len(reporting_period_data['timestamps'][0]) == 0:
519
        has_timestamps_data = False
520
521
    if has_values_saving_data and has_timestamps_data:
522
        ca_len = len(reporting_period_data['names'])
523
        time = reporting_period_data['timestamps'][0]
524
525
        ws['B' + str(current_row_number)].font = title_font
526
        ws['B' + str(current_row_number)] = name + ' 详细数据'
527
528
        current_row_number += 1
529
530
        chart_start_row_number = current_row_number
531
532
        current_row_number += ca_len * 5
533
        table_start_row_number = current_row_number
534
535
        ws['B' + str(current_row_number)].fill = table_fill
536
        ws['B' + str(current_row_number)].font = title_font
537
        ws['B' + str(current_row_number)].alignment = c_c_alignment
538
        ws['B' + str(current_row_number)].border = f_border
539
        ws['B' + str(current_row_number)] = '日期时间'
540
541
        col = 'C'
542
543
        for i in range(0, ca_len):
544
            ws[col + str(current_row_number)].fill = table_fill
545
            ws[col + str(current_row_number)].font = title_font
546
            ws[col + str(current_row_number)].alignment = c_c_alignment
547
            ws[col + str(current_row_number)].border = f_border
548
            ws[col + str(current_row_number)] = \
549
                reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
550
            col = chr(ord(col) + 1)
551
552
        current_row_number += 1
553
554
        for i in range(0, len(time)):
555
            ws['B' + str(current_row_number)].font = title_font
556
            ws['B' + str(current_row_number)].alignment = c_c_alignment
557
            ws['B' + str(current_row_number)].border = f_border
558
            ws['B' + str(current_row_number)] = time[i]
559
560
            col = 'C'
561
            for j in range(0, ca_len):
562
                ws[col + str(current_row_number)].font = title_font
563
                ws[col + str(current_row_number)].alignment = c_c_alignment
564
                ws[col + str(current_row_number)].border = f_border
565
                ws[col + str(current_row_number)] = round(reporting_period_data['values_saving'][j][i], 2) \
566
                    if reporting_period_data['values_saving'][j][i] is not None else 0.00
567
                col = chr(ord(col) + 1)
568
569
            current_row_number += 1
570
571
        table_end_row_number = current_row_number - 1
572
573
        format_time_width_number = 1.0
574
        min_len_number = 1.0
575
        min_width_number = 11.0  # format_time_width_number * min_len_number + 4 and min_width_number > 11.0
576
577
        if period_type == 'hourly':
578
            format_time_width_number = 4.0
579
            min_len_number = 2
580
            min_width_number = 12.0
581
        elif period_type == 'daily':
582
            format_time_width_number = 2.5
583
            min_len_number = 4
584
            min_width_number = 14.0
585
        elif period_type == 'monthly':
586
            format_time_width_number = 2.1
587
            min_len_number = 4
588
            min_width_number = 12.4
589
        elif period_type == 'yearly':
590
            format_time_width_number = 1.5
591
            min_len_number = 5
592
            min_width_number = 11.5
593
594
        for i in range(0, ca_len):
595
            bar = BarChart()
596
            bar.title = \
597
                reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
598
            labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
599
            bar_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
600
            bar.add_data(bar_data, titles_from_data=True)
601
            bar.set_categories(labels)
602
            bar.height = 5.25
603
            bar.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
604
            bar.dLbls = DataLabelList()
605
            bar.dLbls.showVal = True
606
            bar.dLbls.showPercent = True
607
            chart_col = 'B'
608
            chart_cell = chart_col + str(chart_start_row_number)
609
            chart_start_row_number += 5
610
            ws.add_chart(bar, chart_cell)
611
612
    filename = str(uuid.uuid4()) + '.xlsx'
613
    wb.save(filename)
614
615
    return filename
616