Improved the SpaceEnergyCategory Execl Exporter
parent
26ff9762b9
commit
3d46116678
|
@ -395,9 +395,16 @@ def generate_excel(report,
|
||||||
has_child_flag = True
|
has_child_flag = True
|
||||||
# Judge if the space has child space, if not, delete it.
|
# Judge if the space has child space, if not, delete it.
|
||||||
if "child_space" not in report.keys() or "energy_category_names" not in report['child_space'].keys() or \
|
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:
|
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
|
has_child_flag = False
|
||||||
|
|
||||||
|
current_row_number = 19
|
||||||
|
|
||||||
if has_child_flag:
|
if has_child_flag:
|
||||||
child = report['child_space']
|
child = report['child_space']
|
||||||
child_spaces = child['child_space_names_array'][0]
|
child_spaces = child['child_space_names_array'][0]
|
||||||
|
@ -411,6 +418,8 @@ def generate_excel(report,
|
||||||
ws['B20'].border = f_border
|
ws['B20'].border = f_border
|
||||||
ca_len = len(child['energy_category_names'])
|
ca_len = len(child['energy_category_names'])
|
||||||
|
|
||||||
|
table_start_row_number = 20
|
||||||
|
|
||||||
for i in range(0, ca_len):
|
for i in range(0, ca_len):
|
||||||
row = chr(ord('C') + i)
|
row = chr(ord('C') + i)
|
||||||
ws[row + '20'].fill = table_fill
|
ws[row + '20'].fill = table_fill
|
||||||
|
@ -420,6 +429,7 @@ def generate_excel(report,
|
||||||
ws[row + '20'] = child['energy_category_names'][i] + ' (' + child['units'][i] + ')'
|
ws[row + '20'] = child['energy_category_names'][i] + ' (' + child['units'][i] + ')'
|
||||||
|
|
||||||
space_len = len(child['child_space_names_array'][0])
|
space_len = len(child['child_space_names_array'][0])
|
||||||
|
|
||||||
for i in range(0, space_len):
|
for i in range(0, space_len):
|
||||||
row = str(i + 21)
|
row = str(i + 21)
|
||||||
|
|
||||||
|
@ -434,58 +444,74 @@ def generate_excel(report,
|
||||||
ws[col + row].alignment = c_c_alignment
|
ws[col + row].alignment = c_c_alignment
|
||||||
ws[col + row] = round(child['subtotals_array'][j][i], 2)
|
ws[col + row] = round(child['subtotals_array'][j][i], 2)
|
||||||
ws[col + row].border = f_border
|
ws[col + row].border = f_border
|
||||||
# pie
|
|
||||||
# 25~30: pie
|
table_end_row_number = 20 + space_len
|
||||||
pie = PieChart()
|
chart_start_row_number = 20 + space_len + 1
|
||||||
pie.title = ws.cell(column=3 + j, row=20).value
|
|
||||||
labels = Reference(ws, min_col=2, min_row=21, max_row=24)
|
for i in range(0, ca_len):
|
||||||
pie_data = Reference(ws, min_col=3 + j, min_row=20, max_row=24)
|
# pie
|
||||||
pie.add_data(pie_data, titles_from_data=True)
|
# 25~30: pie
|
||||||
pie.set_categories(labels)
|
pie = PieChart()
|
||||||
pie.height = 6.6 # cm 1.05*5 1.05cm = 30 pt
|
pie.title = ws.cell(column=3 + i, row=table_start_row_number).value
|
||||||
pie.width = 8
|
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
|
||||||
# pie.title = "Pies sold by category"
|
pie_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
|
||||||
s1 = pie.series[0]
|
pie.add_data(pie_data, titles_from_data=True)
|
||||||
s1.dLbls = DataLabelList()
|
pie.set_categories(labels)
|
||||||
s1.dLbls.showCatName = True # 标签显示
|
pie.height = 6.6 # cm 1.05*5 1.05cm = 30 pt
|
||||||
s1.dLbls.showVal = True # 数量显示
|
pie.width = 8
|
||||||
s1.dLbls.showPercent = True # 百分比显示
|
# pie.title = "Pies sold by category"
|
||||||
# s1 = CharacterProperties(sz=1800) # 图表中字体大小 *100
|
s1 = pie.series[0]
|
||||||
chart_col = chr(ord('B') + 2 * j)
|
s1.dLbls = DataLabelList()
|
||||||
chart_cell = chart_col + '25'
|
s1.dLbls.showCatName = False # 标签显示
|
||||||
ws.add_chart(pie, chart_cell)
|
s1.dLbls.showVal = True # 数量显示
|
||||||
else:
|
s1.dLbls.showPercent = True # 百分比显示
|
||||||
for i in range(19, 36 + 1):
|
# s1 = CharacterProperties(sz=1800) # 图表中字体大小 *100
|
||||||
ws.row_dimensions[i].height = 0.1
|
chart_cell = ''
|
||||||
for i in range(30, 35 + 1):
|
if i % 2 == 0:
|
||||||
ws.row_dimensions[i].height = 0.1
|
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)
|
||||||
|
# chart_col = chr(ord('B') + 2 * j)
|
||||||
|
# chart_cell = chart_col + '25'
|
||||||
|
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
|
||||||
|
|
||||||
################################################
|
################################################
|
||||||
# Fourth: 能耗详情
|
# Fourth: 能耗详情
|
||||||
# 37: title
|
# current_row_number: title
|
||||||
# 38~ 38+ca_len*6-1: line
|
# current_row_number+1 ~ current_row_number+1+ca_len*6-1: line
|
||||||
# 38+ca_len*6: table title
|
# current_row_number+1+ca_len*6: table title
|
||||||
# 38+ca_len*6~: table_data
|
# current_row_number+1+ca_len*6~: table_data
|
||||||
################################################
|
################################################
|
||||||
reporting_period_data = report['reporting_period']
|
reporting_period_data = report['reporting_period']
|
||||||
times = reporting_period_data['timestamps']
|
times = reporting_period_data['timestamps']
|
||||||
has_detail_data_flag = True
|
has_detail_data_flag = True
|
||||||
ca_len = len(report['reporting_period']['names'])
|
ca_len = len(report['reporting_period']['names'])
|
||||||
table_row = 38 + ca_len*6
|
table_row = current_row_number + 1 + ca_len*6
|
||||||
|
chart_start_row_number = current_row_number + 1
|
||||||
if "timestamps" not in reporting_period_data.keys() or \
|
if "timestamps" not in reporting_period_data.keys() or \
|
||||||
reporting_period_data['timestamps'] is None or \
|
reporting_period_data['timestamps'] is None or \
|
||||||
len(reporting_period_data['timestamps']) == 0:
|
len(reporting_period_data['timestamps']) == 0:
|
||||||
has_detail_data_flag = False
|
has_detail_data_flag = False
|
||||||
|
|
||||||
if has_detail_data_flag:
|
if has_detail_data_flag:
|
||||||
ws['B37'].font = title_font
|
ws['B' + str(current_row_number)].font = title_font
|
||||||
ws['B37'] = name+' 能耗详情'
|
ws['B' + str(current_row_number)] = name+' 详细数据'
|
||||||
|
|
||||||
ws.row_dimensions[table_row].height = 60
|
ws.row_dimensions[table_row].height = 60
|
||||||
ws['B'+str(table_row)].fill = table_fill
|
ws['B'+str(table_row)].fill = table_fill
|
||||||
ws['B' + str(table_row)].font = title_font
|
ws['B' + str(table_row)].font = title_font
|
||||||
ws['B'+str(table_row)].border = f_border
|
ws['B'+str(table_row)].border = f_border
|
||||||
ws['B'+str(table_row)].alignment = c_c_alignment
|
ws['B'+str(table_row)].alignment = c_c_alignment
|
||||||
ws['B'+str(table_row)] = '时间'
|
ws['B'+str(table_row)] = '日期时间'
|
||||||
time = times[0]
|
time = times[0]
|
||||||
has_data = False
|
has_data = False
|
||||||
max_row = 0
|
max_row = 0
|
||||||
|
@ -526,31 +552,45 @@ def generate_excel(report,
|
||||||
ws[col + row].alignment = c_c_alignment
|
ws[col + row].alignment = c_c_alignment
|
||||||
ws[col + row] = round(reporting_period_data['values'][i][j], 2)
|
ws[col + row] = round(reporting_period_data['values'][i][j], 2)
|
||||||
ws[col + row].border = f_border
|
ws[col + row].border = f_border
|
||||||
|
|
||||||
|
current_row_number = table_row + 1 + len(times[0])
|
||||||
|
|
||||||
|
ws['B' + str(current_row_number)].font = title_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)] = '小计'
|
||||||
|
|
||||||
|
for i in range(0, ca_len):
|
||||||
|
col = chr(ord('C') + i)
|
||||||
|
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)] = reporting_period_data['subtotals'][i]
|
||||||
|
|
||||||
# line
|
# line
|
||||||
# 39~: line
|
# 39~: line
|
||||||
line = LineChart()
|
line = LineChart()
|
||||||
line.title = '报告期消耗 - '
|
line.title = '报告期消耗 - ' + ws.cell(column=3+i, row=table_row).value
|
||||||
labels = Reference(ws, min_col=2, min_row=table_row+1, max_row=max_row + 1)
|
labels = Reference(ws, min_col=2, min_row=table_row+1, max_row=max_row)
|
||||||
line_data = Reference(ws, min_col=3 + i, min_row=table_row, max_row=max_row + 1) # openpyxl bug
|
line_data = Reference(ws, min_col=3 + i, min_row=table_row, max_row=max_row) # openpyxl bug
|
||||||
line.add_data(line_data, titles_from_data=True)
|
line.add_data(line_data, titles_from_data=True)
|
||||||
line.set_categories(labels)
|
line.set_categories(labels)
|
||||||
line_data = line.series[0]
|
line_data = line.series[0]
|
||||||
line_data.marker.symbol = "circle"
|
line_data.marker.symbol = "circle"
|
||||||
line_data.smooth = False
|
line_data.smooth = True
|
||||||
|
line.x_axis.crosses = 'min'
|
||||||
line.height = 8.25 # cm 1.05*5 1.05cm = 30 pt
|
line.height = 8.25 # cm 1.05*5 1.05cm = 30 pt
|
||||||
line.width = 24
|
line.width = 24
|
||||||
# pie.title = "Pies sold by category"
|
# pie.title = "Pies sold by category"
|
||||||
line.dLbls = DataLabelList()
|
line.dLbls = DataLabelList()
|
||||||
|
line.dLbls.dLblPos = 't'
|
||||||
# line.dLbls.showCatName = True # label show
|
# line.dLbls.showCatName = True # label show
|
||||||
line.dLbls.showVal = True # val show
|
line.dLbls.showVal = True # val show
|
||||||
line.dLbls.showPercent = True # percent show
|
line.dLbls.showPercent = True # percent show
|
||||||
# s1 = CharacterProperties(sz=1800) # font size *100
|
# s1 = CharacterProperties(sz=1800) # font size *100
|
||||||
chart_col = 'B'
|
chart_col = 'B'
|
||||||
chart_cell = chart_col + str(38 + 6*i)
|
chart_cell = chart_col + str(chart_start_row_number + 6*i)
|
||||||
ws.add_chart(line, chart_cell)
|
ws.add_chart(line, chart_cell)
|
||||||
else:
|
|
||||||
for i in range(37, 69 + 1):
|
|
||||||
ws.row_dimensions[i].height = 0.1
|
|
||||||
|
|
||||||
filename = str(uuid.uuid4()) + '.xlsx'
|
filename = str(uuid.uuid4()) + '.xlsx'
|
||||||
wb.save(filename)
|
wb.save(filename)
|
||||||
|
|
Loading…
Reference in New Issue