diff --git a/myems-api/excelexporters/spacecost.py b/myems-api/excelexporters/spacecost.py index 2deb9950..5ad658ae 100644 --- a/myems-api/excelexporters/spacecost.py +++ b/myems-api/excelexporters/spacecost.py @@ -560,7 +560,10 @@ def generate_excel(report, ws.row_dimensions[current_row_number].height = 60 ws['B' + str(current_row_number)].fill = table_fill + ws['B' + str(current_row_number)].font = name_font + ws['B' + str(current_row_number)].alignment = c_c_alignment ws['B' + str(current_row_number)].border = f_border + ws['B' + str(current_row_number)] = '子空间' ca_len = len(child['energy_category_names']) col = '' @@ -568,14 +571,14 @@ def generate_excel(report, for i in range(0, ca_len): col = chr(ord('C') + i) ws[col + str(current_row_number)].fill = table_fill - ws[col + str(current_row_number)].font = title_font + ws[col + str(current_row_number)].font = name_font ws[col + str(current_row_number)].alignment = c_c_alignment ws[col + str(current_row_number)].border = f_border ws[col + str(current_row_number)] = child['energy_category_names'][i] + ' (' + child['units'][i] + ')' col = chr(ord(col) + 1) ws[col + str(current_row_number)].fill = table_fill - ws[col + str(current_row_number)].font = title_font + ws[col + str(current_row_number)].font = name_font ws[col + str(current_row_number)].alignment = c_c_alignment ws[col + str(current_row_number)].border = f_border ws[col + str(current_row_number)] = '总计 (' + report['reporting_period']['total_unit'] + ')' @@ -586,7 +589,7 @@ def generate_excel(report, current_row_number += 1 row = str(current_row_number) - ws['B' + row].font = name_font + ws['B' + row].font = title_font ws['B' + row].alignment = c_c_alignment ws['B' + row] = child['child_space_names_array'][0][i] ws['B' + row].border = f_border diff --git a/myems-api/excelexporters/spaceenergycategory.py b/myems-api/excelexporters/spaceenergycategory.py index 97d8a14d..9ad48e65 100644 --- a/myems-api/excelexporters/spaceenergycategory.py +++ b/myems-api/excelexporters/spaceenergycategory.py @@ -622,7 +622,10 @@ def generate_excel(report, ws.row_dimensions[current_row_number].height = 60 ws['B' + str(current_row_number)].fill = table_fill + ws['B' + str(current_row_number)].font = name_font + ws['B' + str(current_row_number)].alignment = c_c_alignment ws['B' + str(current_row_number)].border = f_border + ws['B' + str(current_row_number)] = '子空间' ca_len = len(child['energy_category_names']) col = '' @@ -630,7 +633,7 @@ def generate_excel(report, for i in range(0, ca_len): col = chr(ord('C') + i) ws[col + str(current_row_number)].fill = table_fill - ws[col + str(current_row_number)].font = title_font + ws[col + str(current_row_number)].font = name_font ws[col + str(current_row_number)].alignment = c_c_alignment ws[col + str(current_row_number)].border = f_border ws[col + str(current_row_number)] = child['energy_category_names'][i] + ' (' + child['units'][i] + ')' @@ -641,7 +644,7 @@ def generate_excel(report, current_row_number += 1 row = str(current_row_number) - ws['B' + row].font = name_font + ws['B' + row].font = title_font ws['B' + row].alignment = c_c_alignment ws['B' + row] = child['child_space_names_array'][0][i] ws['B' + row].border = f_border diff --git a/myems-api/excelexporters/spaceenergyitem.py b/myems-api/excelexporters/spaceenergyitem.py index 2292c3a8..e1d62d29 100644 --- a/myems-api/excelexporters/spaceenergyitem.py +++ b/myems-api/excelexporters/spaceenergyitem.py @@ -472,7 +472,10 @@ def generate_excel(report, ws.row_dimensions[current_row_number].height = 60 ws['B' + str(current_row_number)].fill = table_fill + ws['B' + str(current_row_number)].font = name_font + ws['B' + str(current_row_number)].alignment = c_c_alignment ws['B' + str(current_row_number)].border = f_border + ws['B' + str(current_row_number)] = '子空间' ca_len = len(child['energy_item_names']) for i in range(0, ca_len): @@ -490,7 +493,7 @@ def generate_excel(report, current_row_number += 1 row = str(current_row_number) - ws['B' + row].font = name_font + ws['B' + row].font = title_font ws['B' + row].alignment = c_c_alignment ws['B' + row] = child['child_space_names_array'][0][i] ws['B' + row].border = f_border diff --git a/myems-api/excelexporters/spaceincome.py b/myems-api/excelexporters/spaceincome.py index e388ca17..8f850218 100644 --- a/myems-api/excelexporters/spaceincome.py +++ b/myems-api/excelexporters/spaceincome.py @@ -1,6 +1,7 @@ import base64 import uuid import os +from decimal import Decimal from openpyxl.chart import ( PieChart, LineChart, @@ -291,9 +292,10 @@ def generate_excel(report, current_row_number += 1 ca_len = len(reporting_period_data['names']) - wssum = 0 + total = Decimal(0.0) for i in range(0, ca_len): - wssum = round(reporting_period_data['subtotals'][i], 2) + wssum + total = reporting_period_data['subtotals'][i] + total + for i in range(0, ca_len): ws['B' + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].alignment = c_c_alignment @@ -308,7 +310,8 @@ def generate_excel(report, ws['D' + str(current_row_number)].font = title_font ws['D' + str(current_row_number)].alignment = c_c_alignment ws['D' + str(current_row_number)].border = f_border - ws['D' + str(current_row_number)] = '{:.2%}'.format(round(reporting_period_data['subtotals'][i], 2) / wssum) + ws['D' + str(current_row_number)] = '{:.2%}'.format(reporting_period_data['subtotals'][i] / total) \ + if total > Decimal(0.0) else '-' current_row_number += 1 @@ -327,103 +330,29 @@ def generate_excel(report, s1.dLbls.showCatName = False s1.dLbls.showVal = True s1.dLbls.showPercent = True - table_cell = 'E' + str(table_start_row_number - 1) + table_cell = 'E' + str(table_start_row_number) ws.add_chart(pie, table_cell) if ca_len < 4: current_row_number = current_row_number - ca_len + 4 - else: - for i in range(13, 22 + 1): - ws.row_dimensions[i].height = 0.1 - - ################################## - current_row_number = 15 - - has_child_flag = True - - if "child_space" not in report.keys() or "energy_category_names" not in report['child_space'].keys() or \ - len(report['child_space']["energy_category_names"]) == 0 \ - or 'child_space_names_array' not in report['child_space'].keys() \ - or report['child_space']['energy_category_names'] is None \ - or len(report['child_space']['child_space_names_array']) == 0 \ - or len(report['child_space']['child_space_names_array'][0]) == 0: - has_child_flag = False - - if has_child_flag: - child = report['child_space'] - - ws['B' + str(current_row_number)].font = title_font - ws['B' + str(current_row_number)] = name + ' 子空间数据' - - current_row_number += 1 - table_start_row_number = current_row_number - - ws.row_dimensions[current_row_number].height = 60 - ws['B' + str(current_row_number)].fill = table_fill - ws['B' + str(current_row_number)].border = f_border - ca_len = len(child['energy_category_names']) - - col = '' - - for i in range(0, ca_len): - col = chr(ord('C') + i) - ws[col + str(current_row_number)].fill = table_fill - ws[col + str(current_row_number)].font = title_font - ws[col + str(current_row_number)].alignment = c_c_alignment - ws[col + str(current_row_number)].border = f_border - ws[col + str(current_row_number)] = child['energy_category_names'][i] + ' (' + child['units'][i] + ')' - - col = chr(ord(col) + 1) - ws[col + str(current_row_number)].fill = table_fill - ws[col + str(current_row_number)].font = title_font - ws[col + str(current_row_number)].alignment = c_c_alignment - ws[col + str(current_row_number)].border = f_border - ws[col + str(current_row_number)] = '总计 (' + report['reporting_period']['total_unit'] + ')' - - space_len = len(child['child_space_names_array'][0]) - - for i in range(0, space_len): - current_row_number += 1 - row = str(current_row_number) - - ws['B' + row].font = name_font - ws['B' + row].alignment = c_c_alignment - ws['B' + row] = child['child_space_names_array'][0][i] - ws['B' + row].border = f_border - - col = '' - every_day_sum = 0 - - for j in range(0, ca_len): - col = chr(ord('C') + j) - ws[col + row].font = name_font - ws[col + row].alignment = c_c_alignment - every_day_sum += child['subtotals_array'][j][i] - ws[col + row] = round(child['subtotals_array'][j][i], 2) - ws[col + row].border = f_border - - col = chr(ord(col) + 1) - ws[col + row].font = name_font - ws[col + row].alignment = c_c_alignment - ws[col + row] = round(every_day_sum, 2) - ws[col + row].border = f_border current_row_number += 1 - current_row_number += 1 + ################################################# - ############################################# reporting_period_data = report['reporting_period'] times = reporting_period_data['timestamps'] has_detail_data_flag = True ca_len = len(report['reporting_period']['names']) table_row = (current_row_number + 1) + ca_len * 6 + current_end_row_number = current_row_number if "timestamps" not in reporting_period_data.keys() or \ reporting_period_data['timestamps'] is None or \ len(reporting_period_data['timestamps']) == 0: has_detail_data_flag = False if has_detail_data_flag: + current_end_row_number += ca_len * 6 + 1 ws['B' + str(current_row_number)].font = title_font ws['B' + str(current_row_number)] = name + ' 详细数据' @@ -439,6 +368,7 @@ def generate_excel(report, if len(time) > 0: has_data = True max_row = table_row + len(time) + current_end_row_number += (len(time) + 3) if has_data: for i in range(0, len(time)): @@ -517,7 +447,7 @@ def generate_excel(report, ws[col + str(table_row)] = '总计 (' + report['reporting_period']['total_unit'] + ')' ws[col + str(table_row)].border = f_border - total_sum = 0 + total_sum = Decimal(0.0) for j in range(0, len(time)): row = str(table_row + 1 + j) @@ -534,9 +464,120 @@ def generate_excel(report, ws[col + row] = round(total_sum, 2) ws[col + row].border = f_border - else: - for i in range(37, 69 + 1): - ws.row_dimensions[i].height = 0.1 + current_row_number = current_end_row_number + + ################################## + + has_child_flag = True + + if "child_space" not in report.keys() or "energy_category_names" not in report['child_space'].keys() or \ + len(report['child_space']["energy_category_names"]) == 0 \ + or 'child_space_names_array' not in report['child_space'].keys() \ + or report['child_space']['energy_category_names'] is None \ + or len(report['child_space']['child_space_names_array']) == 0 \ + or len(report['child_space']['child_space_names_array'][0]) == 0: + has_child_flag = False + + if has_child_flag: + child = report['child_space'] + + ws['B' + str(current_row_number)].font = title_font + ws['B' + str(current_row_number)] = name + ' 子空间数据' + + current_row_number += 1 + table_start_row_number = current_row_number + + ws.row_dimensions[current_row_number].height = 60 + ws['B' + str(current_row_number)].fill = table_fill + ws['B' + str(current_row_number)].font = name_font + ws['B' + str(current_row_number)].alignment = c_c_alignment + ws['B' + str(current_row_number)].border = f_border + ws['B' + str(current_row_number)] = '子空间' + ca_len = len(child['energy_category_names']) + + col = '' + + for i in range(0, ca_len): + col = chr(ord('C') + i) + ws[col + str(current_row_number)].fill = table_fill + ws[col + str(current_row_number)].font = name_font + ws[col + str(current_row_number)].alignment = c_c_alignment + ws[col + str(current_row_number)].border = f_border + ws[col + str(current_row_number)] = child['energy_category_names'][i] + ' (' + child['units'][i] + ')' + + col = chr(ord(col) + 1) + ws[col + str(current_row_number)].fill = table_fill + ws[col + str(current_row_number)].font = name_font + ws[col + str(current_row_number)].alignment = c_c_alignment + ws[col + str(current_row_number)].border = f_border + ws[col + str(current_row_number)] = '总计 (' + report['reporting_period']['total_unit'] + ')' + + space_len = len(child['child_space_names_array'][0]) + + for i in range(0, space_len): + current_row_number += 1 + row = str(current_row_number) + + ws['B' + row].font = title_font + ws['B' + row].alignment = c_c_alignment + ws['B' + row] = child['child_space_names_array'][0][i] + ws['B' + row].border = f_border + + col = '' + every_day_sum = Decimal(0.0) + + for j in range(0, ca_len): + col = chr(ord('C') + j) + ws[col + row].font = name_font + ws[col + row].alignment = c_c_alignment + every_day_sum += child['subtotals_array'][j][i] + ws[col + row] = round(child['subtotals_array'][j][i], 2) + ws[col + row].border = f_border + + col = chr(ord(col) + 1) + ws[col + row].font = name_font + ws[col + row].alignment = c_c_alignment + ws[col + row] = round(every_day_sum, 2) + ws[col + row].border = f_border + + table_end_row_number = current_row_number + + current_row_number += 1 + + chart_start_row_number = current_row_number + + # Pie + for i in range(0, ca_len): + pie = PieChart() + labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) + pie_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, + max_row=table_end_row_number) + pie.add_data(pie_data, titles_from_data=True) + pie.set_categories(labels) + pie.height = 6.6 + pie.width = 8 + pie.title = ws.cell(column=3 + i, row=table_start_row_number).value + s1 = pie.series[0] + s1.dLbls = DataLabelList() + s1.dLbls.showCatName = False + s1.dLbls.showVal = True + s1.dLbls.showPercent = True + chart_cell = '' + if i % 2 == 0: + chart_cell = 'B' + str(chart_start_row_number) + else: + chart_cell = 'E' + str(chart_start_row_number) + chart_start_row_number += 5 + ws.add_chart(pie, chart_cell) + + current_row_number = chart_start_row_number + + if ca_len % 2 == 1: + current_row_number += 5 + + current_row_number += 1 + + ############################################# filename = str(uuid.uuid4()) + '.xlsx' wb.save(filename) @@ -545,7 +586,7 @@ def generate_excel(report, def reporting_period_values_every_day_sum(reporting_period_data, every_day_index, ca_len): - every_day_sum = 0 + every_day_sum = Decimal(0.0) for i in range(0, ca_len): every_day_sum += reporting_period_data['values'][i][every_day_index]