excelexporters.spacesaving   F
last analyzed

Complexity

Total Complexity 60

Size/Duplication

Total Lines 662
Duplicated Lines 37.76 %

Importance

Changes 0
Metric Value
eloc 494
dl 250
loc 662
rs 3.6
c 0
b 0
f 0
wmc 60

2 Functions

Rating   Name   Duplication   Size   Complexity  
F generate_excel() 250 597 55
B export() 0 39 5

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
    LineChart,
7
    BarChart,
8
    Reference,
9
)
10
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font
11
from openpyxl.drawing.image import Image
12
from openpyxl import Workbook
13
from openpyxl.chart.label import DataLabelList
14
15
16
####################################################################################################################
17
# PROCEDURES
18
# Step 1: Validate the report data
19
# Step 2: Generate excel file
20
# Step 3: Encode the excel file bytes to Base64
21
####################################################################################################################
22
23
24
def export(report,
25
           name,
26
           reporting_start_datetime_local,
27
           reporting_end_datetime_local,
28
           period_type):
29
    ####################################################################################################################
30
    # Step 1: Validate the report data
31
    ####################################################################################################################
32
    if report is None:
33
        return None
34
    print(report)
35
36
    ####################################################################################################################
37
    # Step 2: Generate excel file from the report data
38
    ####################################################################################################################
39
    filename = generate_excel(report,
40
                              name,
41
                              reporting_start_datetime_local,
42
                              reporting_end_datetime_local,
43
                              period_type)
44
    ####################################################################################################################
45
    # Step 3: Encode the excel file to Base64
46
    ####################################################################################################################
47
    try:
48
        with open(filename, 'rb') as binary_file:
49
            binary_file_data = binary_file.read()
50
    except IOError as ex:
51
        pass
52
53
    # Base64 encode the bytes
54
    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...
55
    # get the Base64 encoded data using human-readable characters.
56
    base64_message = base64_encoded_data.decode('utf-8')
57
    # delete the file from server
58
    try:
59
        os.remove(filename)
60
    except NotImplementedError as ex:
61
        pass
62
    return base64_message
63
64
65
def generate_excel(report,
66
                   name,
67
                   reporting_start_datetime_local,
68
                   reporting_end_datetime_local,
69
                   period_type):
70
    wb = Workbook()
71
    ws = wb.active
72
73
    # Row height
74
    ws.row_dimensions[1].height = 102
75
    for i in range(2, 2000 + 1):
76
        ws.row_dimensions[i].height = 42
77
78
    # Col width
79
    ws.column_dimensions['A'].width = 1.5
80
81
    ws.column_dimensions['B'].width = 25.0
82
83
    for i in range(ord('C'), ord('L')):
84
        ws.column_dimensions[chr(i)].width = 15.0
85
86
    # Font
87
    name_font = Font(name='Constantia', size=15, bold=True)
88
    title_font = Font(name='宋体', size=15, bold=True)
89
    data_font = Font(name='Franklin Gothic Book', size=11)
90
91
    table_fill = PatternFill(fill_type='solid', fgColor='1F497D')
92
    f_border = Border(left=Side(border_style='medium', color='00000000'),
93
                      right=Side(border_style='medium', color='00000000'),
94
                      bottom=Side(border_style='medium', color='00000000'),
95
                      top=Side(border_style='medium', color='00000000')
96
                      )
97
    b_border = Border(
98
        bottom=Side(border_style='medium', color='00000000'),
99
    )
100
101
    b_c_alignment = Alignment(vertical='bottom',
102
                              horizontal='center',
103
                              text_rotation=0,
104
                              wrap_text=True,
105
                              shrink_to_fit=False,
106
                              indent=0)
107
    c_c_alignment = Alignment(vertical='center',
108
                              horizontal='center',
109
                              text_rotation=0,
110
                              wrap_text=True,
111
                              shrink_to_fit=False,
112
                              indent=0)
113
    b_r_alignment = Alignment(vertical='bottom',
114
                              horizontal='right',
115
                              text_rotation=0,
116
                              wrap_text=True,
117
                              shrink_to_fit=False,
118
                              indent=0)
119
    c_r_alignment = Alignment(vertical='bottom',
120
                              horizontal='center',
121
                              text_rotation=0,
122
                              wrap_text=True,
123
                              shrink_to_fit=False,
124
                              indent=0)
125
126
    # Img
127
    img = Image("excelexporters/myems.png")
128
    img.width = img.width * 0.85
129
    img.height = img.height * 0.85
130
    # img = Image("myems.png")
131
    ws.add_image(img, 'B1')
132
133
    # Title
134
    ws.row_dimensions[3].height = 60
135
136
    ws['B3'].font = name_font
137
    ws['B3'].alignment = b_r_alignment
138
    ws['B3'] = 'Name:'
139
    ws['C3'].border = b_border
140
    ws['C3'].alignment = b_c_alignment
141
    ws['C3'].font = name_font
142
    ws['C3'] = name
143
144
    ws['D3'].font = name_font
145
    ws['D3'].alignment = b_r_alignment
146
    ws['D3'] = 'Period:'
147
    ws['E3'].border = b_border
148
    ws['E3'].alignment = b_c_alignment
149
    ws['E3'].font = name_font
150
    ws['E3'] = period_type
151
152
    ws['F3'].font = name_font
153
    ws['F3'].alignment = b_r_alignment
154
    ws['F3'] = 'Date:'
155
    ws['G3'].alignment = b_c_alignment
156
    ws['G3'].font = name_font
157
    ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local
158
    ws.merge_cells("G3:H3")
159
160
    if "reporting_period" not in report.keys() or \
161
            "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0:
162
        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...
163
        wb.save(filename)
164
165
        return filename
166
167
    ##################################
168
169
    current_row_number = 6
170
171
    reporting_period_data = report['reporting_period']
172
173
    has_names_data_flag = True
174
175
    if "names" not in reporting_period_data.keys() or \
176
            reporting_period_data['names'] is None or \
177
            len(reporting_period_data['names']) == 0:
178
        has_names_data_flag = False
179
180 View Code Duplication
    if has_names_data_flag:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
181
        ws['B' + str(current_row_number)].font = title_font
182
        ws['B' + str(current_row_number)] = name + ' 报告期节约'
183
184
        current_row_number += 1
185
186
        category = reporting_period_data['names']
187
        ca_len = len(category)
188
189
        ws.row_dimensions[current_row_number].height = 75
190
        ws['B' + str(current_row_number)].fill = table_fill
191
        ws['B' + str(current_row_number)].border = f_border
192
193
        col = 'C'
194
195
        for i in range(0, ca_len):
196
            ws[col + str(current_row_number)].fill = table_fill
197
            ws[col + str(current_row_number)].font = name_font
198
            ws[col + str(current_row_number)].alignment = c_c_alignment
199
            ws[col + str(current_row_number)].border = f_border
200
            ws[col + str(current_row_number)] = \
201
                reporting_period_data['names'][i] + " (基线-实际) (" + reporting_period_data['units'][i] + ")"
202
203
            col = chr(ord(col) + 1)
204
205
        ws[col + str(current_row_number)].fill = table_fill
206
        ws[col + str(current_row_number)].font = name_font
207
        ws[col + str(current_row_number)].alignment = c_c_alignment
208
        ws[col + str(current_row_number)].border = f_border
209
        ws[col + str(current_row_number)] = '吨标准煤 (基线-实际) (TCE)'
210
211
        col = chr(ord(col) + 1)
212
213
        ws[col + str(current_row_number)].fill = table_fill
214
        ws[col + str(current_row_number)].font = name_font
215
        ws[col + str(current_row_number)].alignment = c_c_alignment
216
        ws[col + str(current_row_number)].border = f_border
217
        ws[col + str(current_row_number)] = '吨二氧化碳排放 (基线-实际) (TCO2E)'
218
219
        col = chr(ord(col) + 1)
220
221
        current_row_number += 1
222
223
        ws['B' + str(current_row_number)].font = title_font
224
        ws['B' + str(current_row_number)].alignment = c_c_alignment
225
        ws['B' + str(current_row_number)].border = f_border
226
        ws['B' + str(current_row_number)] = '节约'
227
228
        col = 'C'
229
230
        for i in range(0, ca_len):
231
            ws[col + str(current_row_number)].font = name_font
232
            ws[col + str(current_row_number)].alignment = c_c_alignment
233
            ws[col + str(current_row_number)].border = f_border
234
            ws[col + str(current_row_number)] = round(reporting_period_data['subtotals_saving'][i], 2)
235
236
            col = chr(ord(col) + 1)
237
238
        ws[col + str(current_row_number)].font = name_font
239
        ws[col + str(current_row_number)].alignment = c_c_alignment
240
        ws[col + str(current_row_number)].border = f_border
241
        ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgce_saving'] / 1000, 2)
242
243
        col = chr(ord(col) + 1)
244
245
        ws[col + str(current_row_number)].font = name_font
246
        ws[col + str(current_row_number)].alignment = c_c_alignment
247
        ws[col + str(current_row_number)].border = f_border
248
        ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgco2e_saving'] / 1000, 2)
249
250
        col = chr(ord(col) + 1)
251
252
        current_row_number += 1
253
254
        ws['B' + str(current_row_number)].font = title_font
255
        ws['B' + str(current_row_number)].alignment = c_c_alignment
256
        ws['B' + str(current_row_number)].border = f_border
257
        ws['B' + str(current_row_number)] = '单位面积值'
258
259
        col = 'C'
260
261
        for i in range(0, ca_len):
262
            ws[col + str(current_row_number)].font = name_font
263
            ws[col + str(current_row_number)].alignment = c_c_alignment
264
            ws[col + str(current_row_number)].border = f_border
265
            ws[col + str(current_row_number)] = round(reporting_period_data['subtotals_per_unit_area_saving'][i], 2)
266
267
            col = chr(ord(col) + 1)
268
269
        ws[col + str(current_row_number)].font = name_font
270
        ws[col + str(current_row_number)].alignment = c_c_alignment
271
        ws[col + str(current_row_number)].border = f_border
272
        ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgce_per_unit_area_saving'] / 1000, 2)
273
274
        col = chr(ord(col) + 1)
275
276
        ws[col + str(current_row_number)].font = name_font
277
        ws[col + str(current_row_number)].alignment = c_c_alignment
278
        ws[col + str(current_row_number)].border = f_border
279
        ws[col + str(current_row_number)] = \
280
            round(reporting_period_data['total_in_kgco2e_per_unit_area_saving'] / 1000, 2)
281
282
        col = chr(ord(col) + 1)
283
284
        current_row_number += 1
285
286
        ws['B' + str(current_row_number)].font = title_font
287
        ws['B' + str(current_row_number)].alignment = c_c_alignment
288
        ws['B' + str(current_row_number)].border = f_border
289
        ws['B' + str(current_row_number)] = '环比'
290
291
        col = 'C'
292
293
        for i in range(0, ca_len):
294
            ws[col + str(current_row_number)].font = name_font
295
            ws[col + str(current_row_number)].alignment = c_c_alignment
296
            ws[col + str(current_row_number)].border = f_border
297
            ws[col + str(current_row_number)] = str(
298
                round(reporting_period_data['increment_rates_saving'][i] * 100, 2)) + '%' \
299
                if reporting_period_data['increment_rates_saving'][i] is not None else '-'
300
301
            col = chr(ord(col) + 1)
302
303
        ws[col + str(current_row_number)].font = name_font
304
        ws[col + str(current_row_number)].alignment = c_c_alignment
305
        ws[col + str(current_row_number)].border = f_border
306
        ws[col + str(current_row_number)] = str(
307
            round(reporting_period_data['increment_rate_in_kgce_saving'] * 100, 2)) + '%' \
308
            if reporting_period_data['increment_rate_in_kgce_saving'] is not None else '-'
309
310
        col = chr(ord(col) + 1)
311
312
        ws[col + str(current_row_number)].font = name_font
313
        ws[col + str(current_row_number)].alignment = c_c_alignment
314
        ws[col + str(current_row_number)].border = f_border
315
        ws[col + str(current_row_number)] = str(
316
            round(reporting_period_data['increment_rate_in_kgco2e_saving'] * 100, 2)) + '%' \
317
            if reporting_period_data['increment_rate_in_kgco2e_saving'] is not None else '-'
318
319
        col = chr(ord(col) + 1)
320
321
        current_row_number += 2
322
323
        ws['B' + str(current_row_number)].font = title_font
324
        ws['B' + str(current_row_number)] = name + ' 吨标准煤(TCE)占比'
325
326
        current_row_number += 1
327
        table_start_row_number = current_row_number
328
        chart_start_row_number = current_row_number
329
330
        ws.row_dimensions[current_row_number].height = 60
331
        ws['B' + str(current_row_number)].fill = table_fill
332
        ws['B' + str(current_row_number)].border = f_border
333
334
        ws['C' + str(current_row_number)].fill = table_fill
335
        ws['C' + str(current_row_number)].font = name_font
336
        ws['C' + str(current_row_number)].alignment = c_c_alignment
337
        ws['C' + str(current_row_number)].border = f_border
338
        ws['C' + str(current_row_number)] = '吨标准煤(TCE)占比'
339
340
        current_row_number += 1
341
342
        for i in range(0, ca_len):
343
            ws['B' + str(current_row_number)].font = title_font
344
            ws['B' + str(current_row_number)].alignment = c_c_alignment
345
            ws['B' + str(current_row_number)].border = f_border
346
            ws['B' + str(current_row_number)] = reporting_period_data['names'][i]
347
348
            ws['C' + str(current_row_number)].font = name_font
349
            ws['C' + str(current_row_number)].alignment = c_c_alignment
350
            ws['C' + str(current_row_number)].border = f_border
351
            ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals_in_kgce_saving'][i] / 1000, 3)
352
353
            current_row_number += 1
354
355
        table_end_row_number = current_row_number - 1
356
357
        if ca_len < 4:
358
            current_row_number = current_row_number - ca_len + 4
359
360
        current_row_number += 1
361
362
        pie = PieChart()
363
        pie.title = name + ' 吨标准煤(TCE)占比'
364
        labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
365
        pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number)
366
        pie.add_data(pie_data, titles_from_data=True)
367
        pie.set_categories(labels)
368
        pie.height = 7.25
369
        pie.width = 9
370
        s1 = pie.series[0]
371
        s1.dLbls = DataLabelList()
372
        s1.dLbls.showCatName = False
373
        s1.dLbls.showVal = True
374
        s1.dLbls.showPercent = True
375
        ws.add_chart(pie, 'D' + str(chart_start_row_number))
376
377
        ws['B' + str(current_row_number)].font = title_font
378
        ws['B' + str(current_row_number)] = name + ' 吨二氧化碳排放(TCO2E)占比'
379
380
        current_row_number += 1
381
        table_start_row_number = current_row_number
382
        chart_start_row_number = current_row_number
383
384
        ws.row_dimensions[current_row_number].height = 60
385
        ws['B' + str(current_row_number)].fill = table_fill
386
        ws['B' + str(current_row_number)].border = f_border
387
388
        ws['C' + str(current_row_number)].fill = table_fill
389
        ws['C' + str(current_row_number)].font = name_font
390
        ws['C' + str(current_row_number)].alignment = c_c_alignment
391
        ws['C' + str(current_row_number)].border = f_border
392
        ws['C' + str(current_row_number)] = '吨二氧化碳排放(TCO2E)占比'
393
394
        current_row_number += 1
395
396
        for i in range(0, ca_len):
397
            ws['B' + str(current_row_number)].font = title_font
398
            ws['B' + str(current_row_number)].alignment = c_c_alignment
399
            ws['B' + str(current_row_number)].border = f_border
400
            ws['B' + str(current_row_number)] = reporting_period_data['names'][i]
401
402
            ws['C' + str(current_row_number)].font = name_font
403
            ws['C' + str(current_row_number)].alignment = c_c_alignment
404
            ws['C' + str(current_row_number)].border = f_border
405
            ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals_in_kgco2e_saving'][i] / 1000, 3)
406
407
            current_row_number += 1
408
409
        table_end_row_number = current_row_number - 1
410
411
        if ca_len < 4:
412
            current_row_number = current_row_number - ca_len + 4
413
414
        current_row_number += 1
415
416
        pie = PieChart()
417
        pie.title = name + ' 吨二氧化碳排放(TCO2E)占比'
418
        labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
419
        pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number)
420
        pie.add_data(pie_data, titles_from_data=True)
421
        pie.set_categories(labels)
422
        pie.height = 7.25
423
        pie.width = 9
424
        s1 = pie.series[0]
425
        s1.dLbls = DataLabelList()
426
        s1.dLbls.showCatName = False
427
        s1.dLbls.showVal = True
428
        s1.dLbls.showPercent = True
429
        ws.add_chart(pie, 'D' + str(chart_start_row_number))
430
431
    #############################################
432
433
    has_child_space_data_flag = True
434
435
    if 'child_space' not in report.keys() or \
436
            report['child_space'] is None or \
437
            'energy_category_names' not in report['child_space'].keys() or \
438
            report['child_space']['energy_category_names'] is None or \
439
            len(report['child_space']['energy_category_names']) == 0:
440
        has_child_space_data_flag = False
441
442
    if has_child_space_data_flag:
443
        child_space_data = report['child_space']
444
        ca_len = len(child_space_data['energy_category_names'])
445
446
        ws['B' + str(current_row_number)].font = title_font
447
        ws['B' + str(current_row_number)] = name + ' 子空间数据'
448
449
        current_row_number += 1
450
        table_start_row_number = current_row_number
451
452
        ws.row_dimensions[current_row_number].height = 60
453
        ws['B' + str(current_row_number)].fill = table_fill
454
        ws['B' + str(current_row_number)].font = name_font
455
        ws['B' + str(current_row_number)].alignment = c_c_alignment
456
        ws['B' + str(current_row_number)].border = f_border
457
        ws['B' + str(current_row_number)] = '子空间'
458
459
        col = 'C'
460
461
        for i in range(0, ca_len):
462
            ws[col + str(current_row_number)].fill = table_fill
463
            ws[col + str(current_row_number)].font = name_font
464
            ws[col + str(current_row_number)].alignment = c_c_alignment
465
            ws[col + str(current_row_number)].border = f_border
466
            ws[col + str(current_row_number)] = \
467
                child_space_data['energy_category_names'][i] + " (" + child_space_data['units'][i] + ")"
468
            col = chr(ord(col) + 1)
469
470
        current_row_number += 1
471
        ca_child_len = len(child_space_data['child_space_names_array'][0])
472
473
        for i in range(0, ca_child_len):
474
            ws['B' + str(current_row_number)].font = title_font
475
            ws['B' + str(current_row_number)].alignment = c_c_alignment
476
            ws['B' + str(current_row_number)].border = f_border
477
            ws['B' + str(current_row_number)] = child_space_data['child_space_names_array'][0][i]
478
            current_row_number += 1
479
480
        current_row_number -= ca_child_len
481
482
        for i in range(0, ca_child_len):
483
            col = 'C'
484
            for j in range(0, ca_len):
485
                ws[col + str(current_row_number)].font = name_font
486
                ws[col + str(current_row_number)].alignment = c_c_alignment
487
                ws[col + str(current_row_number)].border = f_border
488
                ws[col + str(current_row_number)] = round(child_space_data['subtotals_saving_array'][j][i], 2)
489
                col = chr(ord(col) + 1)
490
491
            current_row_number += 1
492
493
        table_end_row_number = current_row_number - 1
494
495
        col = 'B'
496
497
        for i in range(0, ca_len):
498
            pie = PieChart()
499
            labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
500
            pie_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
501
            pie.add_data(pie_data, titles_from_data=True)
502
            pie.set_categories(labels)
503
            pie.title = reporting_period_data['names'][i] + " (" + \
504
                                                            reporting_period_data['units'][i] + ")"
505
            pie.height = 6.6
506
            pie.width = 9
507
            s1 = pie.series[0]
508
            s1.dLbls = DataLabelList()
509
            s1.dLbls.showCatName = False
510
            s1.dLbls.showVal = True
511
            s1.dLbls.showPercent = True
512
            chart_cell = ''
513
            if i % 2 == 0:
514
                chart_cell = 'B' + str(current_row_number)
515
            else:
516
                chart_cell = 'E' + str(current_row_number)
517
                current_row_number += 5
518
            ws.add_chart(pie, chart_cell)
519
            # ws.add_chart(pie, col + str(current_row_number))
520
            # col = chr(ord(col) + 2)
521
522
        if ca_len % 2 == 1:
523
            current_row_number += 5
524
525
        current_row_number += 1
526
527
    ################################
528
529
    has_values_saving_data = True
530
    has_timestamps_data = True
531
532
    if 'values_saving' not in reporting_period_data.keys() or \
533
            reporting_period_data['values_saving'] is None or \
534
            len(reporting_period_data['values_saving']) == 0:
535
        has_values_saving_data = False
536
537
    if 'timestamps' not in reporting_period_data.keys() or \
538
            reporting_period_data['timestamps'] is None or \
539
            len(reporting_period_data['timestamps']) == 0 or \
540
            len(reporting_period_data['timestamps'][0]) == 0:
541
        has_timestamps_data = False
542
543
    if has_values_saving_data and has_timestamps_data:
544
        ca_len = len(reporting_period_data['names'])
545
        time = reporting_period_data['timestamps'][0]
546
547
        ws['B' + str(current_row_number)].font = title_font
548
        ws['B' + str(current_row_number)] = name + ' 详细数据'
549
550
        current_row_number += 1
551
552
        chart_start_row_number = current_row_number
553
554
        current_row_number += ca_len * 6
555
        table_start_row_number = current_row_number
556
557
        ws.row_dimensions[current_row_number].height = 60
558
        ws['B' + str(current_row_number)].fill = table_fill
559
        ws['B' + str(current_row_number)].font = title_font
560
        ws['B' + str(current_row_number)].alignment = c_c_alignment
561
        ws['B' + str(current_row_number)].border = f_border
562
        ws['B' + str(current_row_number)] = '日期时间'
563
564
        col = 'C'
565
566
        for i in range(0, ca_len):
567
            ws[col + str(current_row_number)].fill = table_fill
568
            ws[col + str(current_row_number)].font = title_font
569
            ws[col + str(current_row_number)].alignment = c_c_alignment
570
            ws[col + str(current_row_number)].border = f_border
571
            ws[col + str(current_row_number)] = \
572
                reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
573
            col = chr(ord(col) + 1)
574
575
        current_row_number += 1
576
577
        for i in range(0, len(time)):
578
            ws['B' + str(current_row_number)].font = title_font
579
            ws['B' + str(current_row_number)].alignment = c_c_alignment
580
            ws['B' + str(current_row_number)].border = f_border
581
            ws['B' + str(current_row_number)] = time[i]
582
583
            col = 'C'
584
            for j in range(0, ca_len):
585
                ws[col + str(current_row_number)].font = title_font
586
                ws[col + str(current_row_number)].alignment = c_c_alignment
587
                ws[col + str(current_row_number)].border = f_border
588
                ws[col + str(current_row_number)] = round(reporting_period_data['values_saving'][j][i], 2) \
589
                    if reporting_period_data['values_saving'][j][i] is not None else 0.00
590
                col = chr(ord(col) + 1)
591
592
            current_row_number += 1
593
594
        table_end_row_number = current_row_number - 1
595
596
        ws['B' + str(current_row_number)].font = title_font
597
        ws['B' + str(current_row_number)].alignment = c_c_alignment
598
        ws['B' + str(current_row_number)].border = f_border
599
        ws['B' + str(current_row_number)] = '小计'
600
601
        col = 'C'
602
603
        for i in range(0, ca_len):
604
            ws[col + str(current_row_number)].font = title_font
605
            ws[col + str(current_row_number)].alignment = c_c_alignment
606
            ws[col + str(current_row_number)].border = f_border
607
            ws[col + str(current_row_number)] = round(reporting_period_data['subtotals_saving'][i], 2)
608
            col = chr(ord(col) + 1)
609
610
        current_row_number += 2
611
612
        format_time_width_number = 1.0
613
        min_len_number = 1.0
614
        min_width_number = 11.0  # format_time_width_number * min_len_number + 4 and min_width_number > 11.0
615
616
        if period_type == 'hourly':
617
            format_time_width_number = 4.0
618
            min_len_number = 2
619
            min_width_number = 12.0
620
        elif period_type == 'daily':
621
            format_time_width_number = 2.5
622
            min_len_number = 4
623
            min_width_number = 14.0
624
        elif period_type == 'monthly':
625
            format_time_width_number = 2.1
626
            min_len_number = 4
627
            min_width_number = 12.4
628
        elif period_type == 'yearly':
629
            format_time_width_number = 1.5
630
            min_len_number = 5
631
            min_width_number = 11.5
632
633
        for i in range(0, ca_len):
634
            line = LineChart()
635
            line.title = '报告期节约 - ' + \
636
                reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
637
            labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
638
            line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
639
            line.add_data(line_data, titles_from_data=True)
640
            line.set_categories(labels)
641
            line_data = line.series[0]
642
            line_data.marker.symbol = "circle"
643
            line_data.smooth = True
644
            line.height = 8.25
645
            line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
646
            if line.width > 24:
647
                line.width = 24
648
            line.x_axis.crosses = 'min'
649
            line.dLbls = DataLabelList()
650
            line.dLbls.dLblPos = 't'
651
            line.dLbls.showVal = True
652
            line.dLbls.showPercent = False
653
            chart_col = 'B'
654
            chart_cell = chart_col + str(chart_start_row_number)
655
            chart_start_row_number += 6
656
            ws.add_chart(line, chart_cell)
657
658
    filename = str(uuid.uuid4()) + '.xlsx'
659
    wb.save(filename)
660
661
    return filename
662