updated excel exporters of combinedequipmentenergyitem, combinedequipmentoutput, equipmentenergyitem, equipmentoutput, shopfloorenergyitem, spaceoutput, storeenergyitem and tenantenergytime

Merge branch 'PR6' into develop
pull/6/MERGE
13621160019@163.com 2021-03-10 19:00:36 +08:00
commit 6c2d66019f
8 changed files with 888 additions and 939 deletions

View File

@ -222,23 +222,6 @@ def generate_excel(report,
current_row_number += 1 current_row_number += 1
# 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)] = '单位面积值'
#
# col = 'C'
#
# for i in range(0, ca_len):
# 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)] = round(reporting_period_data['subtotals_per_unit_area'][i], 2)
#
# col = chr(ord(col) + 1)
#
# current_row_number += 1
ws['B' + str(current_row_number)].font = title_font 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)].alignment = c_c_alignment
ws['B' + str(current_row_number)].border = f_border ws['B' + str(current_row_number)].border = f_border
@ -318,136 +301,136 @@ def generate_excel(report,
current_row_number += 1 current_row_number += 1
##################################### #####################################
has_values_data = True has_values_data = True
has_timestamps_data = True has_timestamps_data = True
if 'values' not in reporting_period_data.keys() or \ if 'values' not in reporting_period_data.keys() or \
reporting_period_data['values'] is None or \ reporting_period_data['values'] is None or \
len(reporting_period_data['values']) == 0: len(reporting_period_data['values']) == 0:
has_values_data = False has_values_data = False
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 or \ len(reporting_period_data['timestamps']) == 0 or \
len(reporting_period_data['timestamps'][0]) == 0: len(reporting_period_data['timestamps'][0]) == 0:
has_timestamps_data = False has_timestamps_data = False
if has_values_data and has_timestamps_data: if has_values_data and has_timestamps_data:
ca_len = len(reporting_period_data['names']) ca_len = len(reporting_period_data['names'])
time = reporting_period_data['timestamps'][0] time = reporting_period_data['timestamps'][0]
ws['B' + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].font = title_font
ws['B' + str(current_row_number)] = name + ' 详细数据' ws['B' + str(current_row_number)] = name + ' 详细数据'
current_row_number += 1 current_row_number += 1
chart_start_row_number = current_row_number chart_start_row_number = current_row_number
current_row_number += ca_len * 6 current_row_number += ca_len * 6
table_start_row_number = current_row_number table_start_row_number = current_row_number
ws.row_dimensions[current_row_number].height = 60 ws.row_dimensions[current_row_number].height = 60
ws['B' + str(current_row_number)].fill = table_fill ws['B' + str(current_row_number)].fill = table_fill
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)] = '日期时间'
col = 'C'
for i in range(0, ca_len):
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)] = \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
col = chr(ord(col) + 1)
current_row_number += 1
for i in range(0, len(time)):
ws['B' + str(current_row_number)].font = title_font 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)].alignment = c_c_alignment
ws['B' + str(current_row_number)].border = f_border ws['B' + str(current_row_number)].border = f_border
ws['B' + str(current_row_number)] = '日期时间' ws['B' + str(current_row_number)] = time[i]
col = 'C' col = 'C'
for j in range(0, ca_len):
for i in range(0, ca_len):
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 = title_font
ws[col + str(current_row_number)].alignment = c_c_alignment 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)].border = f_border
ws[col + str(current_row_number)] = \ ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" if reporting_period_data['values'][j][i] is not None else 0.00
col = chr(ord(col) + 1) col = chr(ord(col) + 1)
current_row_number += 1 current_row_number += 1
for i in range(0, len(time)): table_end_row_number = current_row_number - 1
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)] = time[i]
col = 'C' ws['B' + str(current_row_number)].font = title_font
for j in range(0, ca_len): ws['B' + str(current_row_number)].alignment = c_c_alignment
ws[col + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].border = f_border
ws[col + str(current_row_number)].alignment = c_c_alignment ws['B' + str(current_row_number)] = '小计'
ws[col + str(current_row_number)].border = f_border
ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
if reporting_period_data['values'][j][i] is not None else 0.00
col = chr(ord(col) + 1)
current_row_number += 1 col = 'C'
table_end_row_number = current_row_number - 1 for i in range(0, ca_len):
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)] = round(reporting_period_data['subtotals'][i], 2)
col = chr(ord(col) + 1)
ws['B' + str(current_row_number)].font = title_font current_row_number += 2
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)] = '小计'
col = 'C' format_time_width_number = 1.0
min_len_number = 1.0
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0
for i in range(0, ca_len): if period_type == 'hourly':
ws[col + str(current_row_number)].font = title_font format_time_width_number = 4.0
ws[col + str(current_row_number)].alignment = c_c_alignment min_len_number = 2
ws[col + str(current_row_number)].border = f_border min_width_number = 12.0
ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) elif period_type == 'daily':
col = chr(ord(col) + 1) format_time_width_number = 2.5
min_len_number = 4
min_width_number = 14.0
elif period_type == 'monthly':
format_time_width_number = 2.1
min_len_number = 4
min_width_number = 12.4
elif period_type == 'yearly':
format_time_width_number = 1.5
min_len_number = 5
min_width_number = 11.5
current_row_number += 2 for i in range(0, ca_len):
line = LineChart()
format_time_width_number = 1.0 line.title = '报告期消耗 - ' + \
min_len_number = 1.0 reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0 labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
if period_type == 'hourly': line.add_data(line_data, titles_from_data=True)
format_time_width_number = 4.0 line.set_categories(labels)
min_len_number = 2 line_data = line.series[0]
min_width_number = 12.0 line_data.marker.symbol = "circle"
elif period_type == 'daily': line_data.smooth = True
format_time_width_number = 2.5 line.x_axis.crosses = 'min'
min_len_number = 4 line.height = 8.25
min_width_number = 14.0 line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
elif period_type == 'monthly': if line.width > 24:
format_time_width_number = 2.1 line.width = 24
min_len_number = 4 line.dLbls = DataLabelList()
min_width_number = 12.4 line.dLbls.dLblPos = 't'
elif period_type == 'yearly': line.dLbls.showVal = True
format_time_width_number = 1.5 line.dLbls.showPercent = False
min_len_number = 5 chart_col = 'B'
min_width_number = 11.5 chart_cell = chart_col + str(chart_start_row_number)
chart_start_row_number += 6
for i in range(0, ca_len): ws.add_chart(line, chart_cell)
line = LineChart()
line.title = '报告期消耗 - ' + \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
line.add_data(line_data, titles_from_data=True)
line.set_categories(labels)
line_data = line.series[0]
line_data.marker.symbol = "circle"
line_data.smooth = True
line.x_axis.crosses = 'min'
line.height = 8.25
line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
if line.width > 24:
line.width = 24
line.dLbls = DataLabelList()
line.dLbls.dLblPos = 't'
line.dLbls.showVal = True
line.dLbls.showPercent = False
chart_col = 'B'
chart_cell = chart_col + str(chart_start_row_number)
chart_start_row_number += 6
ws.add_chart(line, chart_cell)
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)

View File

@ -240,136 +240,136 @@ def generate_excel(report,
current_row_number += 2 current_row_number += 2
##################################### #####################################
has_values_data = True has_values_data = True
has_timestamps_data = True has_timestamps_data = True
if 'values' not in reporting_period_data.keys() or \ if 'values' not in reporting_period_data.keys() or \
reporting_period_data['values'] is None or \ reporting_period_data['values'] is None or \
len(reporting_period_data['values']) == 0: len(reporting_period_data['values']) == 0:
has_values_data = False has_values_data = False
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 or \ len(reporting_period_data['timestamps']) == 0 or \
len(reporting_period_data['timestamps'][0]) == 0: len(reporting_period_data['timestamps'][0]) == 0:
has_timestamps_data = False has_timestamps_data = False
if has_values_data and has_timestamps_data: if has_values_data and has_timestamps_data:
ca_len = len(reporting_period_data['names']) ca_len = len(reporting_period_data['names'])
time = reporting_period_data['timestamps'][0] time = reporting_period_data['timestamps'][0]
ws['B' + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].font = title_font
ws['B' + str(current_row_number)] = name + ' 详细数据' ws['B' + str(current_row_number)] = name + ' 详细数据'
current_row_number += 1 current_row_number += 1
chart_start_row_number = current_row_number chart_start_row_number = current_row_number
current_row_number += ca_len * 6 current_row_number += ca_len * 6
table_start_row_number = current_row_number table_start_row_number = current_row_number
ws.row_dimensions[current_row_number].height = 60 ws.row_dimensions[current_row_number].height = 60
ws['B' + str(current_row_number)].fill = table_fill ws['B' + str(current_row_number)].fill = table_fill
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)] = '日期时间'
col = 'C'
for i in range(0, ca_len):
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)] = \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
col = chr(ord(col) + 1)
current_row_number += 1
for i in range(0, len(time)):
ws['B' + str(current_row_number)].font = title_font 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)].alignment = c_c_alignment
ws['B' + str(current_row_number)].border = f_border ws['B' + str(current_row_number)].border = f_border
ws['B' + str(current_row_number)] = '日期时间' ws['B' + str(current_row_number)] = time[i]
col = 'C' col = 'C'
for j in range(0, ca_len):
for i in range(0, ca_len):
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 = title_font
ws[col + str(current_row_number)].alignment = c_c_alignment 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)].border = f_border
ws[col + str(current_row_number)] = \ ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" if reporting_period_data['values'][j][i] is not None else 0.00
col = chr(ord(col) + 1) col = chr(ord(col) + 1)
current_row_number += 1 current_row_number += 1
for i in range(0, len(time)): table_end_row_number = current_row_number - 1
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)] = time[i]
col = 'C' ws['B' + str(current_row_number)].font = title_font
for j in range(0, ca_len): ws['B' + str(current_row_number)].alignment = c_c_alignment
ws[col + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].border = f_border
ws[col + str(current_row_number)].alignment = c_c_alignment ws['B' + str(current_row_number)] = '小计'
ws[col + str(current_row_number)].border = f_border
ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
if reporting_period_data['values'][j][i] is not None else 0.00
col = chr(ord(col) + 1)
current_row_number += 1 col = 'C'
table_end_row_number = current_row_number - 1 for i in range(0, ca_len):
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)] = round(reporting_period_data['subtotals'][i], 2)
col = chr(ord(col) + 1)
ws['B' + str(current_row_number)].font = title_font current_row_number += 2
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)] = '小计'
col = 'C' format_time_width_number = 1.0
min_len_number = 1.0
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0
for i in range(0, ca_len): if period_type == 'hourly':
ws[col + str(current_row_number)].font = title_font format_time_width_number = 4.0
ws[col + str(current_row_number)].alignment = c_c_alignment min_len_number = 2
ws[col + str(current_row_number)].border = f_border min_width_number = 12.0
ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) elif period_type == 'daily':
col = chr(ord(col) + 1) format_time_width_number = 2.5
min_len_number = 4
min_width_number = 14.0
elif period_type == 'monthly':
format_time_width_number = 2.1
min_len_number = 4
min_width_number = 12.4
elif period_type == 'yearly':
format_time_width_number = 1.5
min_len_number = 5
min_width_number = 11.5
current_row_number += 2 for i in range(0, ca_len):
line = LineChart()
format_time_width_number = 1.0 line.title = '报告期产出 - ' + \
min_len_number = 1.0 reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0 labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
if period_type == 'hourly': line.add_data(line_data, titles_from_data=True)
format_time_width_number = 4.0 line.set_categories(labels)
min_len_number = 2 line_data = line.series[0]
min_width_number = 12.0 line_data.marker.symbol = "circle"
elif period_type == 'daily': line_data.smooth = True
format_time_width_number = 2.5 line.x_axis.crosses = 'min'
min_len_number = 4 line.height = 8.25
min_width_number = 14.0 line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
elif period_type == 'monthly': if line.width > 24:
format_time_width_number = 2.1 line.width = 24
min_len_number = 4 line.dLbls = DataLabelList()
min_width_number = 12.4 line.dLbls.dLblPos = 't'
elif period_type == 'yearly': line.dLbls.showVal = True
format_time_width_number = 1.5 line.dLbls.showPercent = False
min_len_number = 5 chart_col = 'B'
min_width_number = 11.5 chart_cell = chart_col + str(chart_start_row_number)
chart_start_row_number += 6
for i in range(0, ca_len): ws.add_chart(line, chart_cell)
line = LineChart()
line.title = '报告期产出 - ' + \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
line.add_data(line_data, titles_from_data=True)
line.set_categories(labels)
line_data = line.series[0]
line_data.marker.symbol = "circle"
line_data.smooth = True
line.x_axis.crosses = 'min'
line.height = 8.25
line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
if line.width > 24:
line.width = 24
line.dLbls = DataLabelList()
line.dLbls.dLblPos = 't'
line.dLbls.showVal = True
line.dLbls.showPercent = False
chart_col = 'B'
chart_cell = chart_col + str(chart_start_row_number)
chart_start_row_number += 6
ws.add_chart(line, chart_cell)
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)

View File

@ -222,23 +222,6 @@ def generate_excel(report,
current_row_number += 1 current_row_number += 1
# 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)] = '单位面积值'
#
# col = 'C'
#
# for i in range(0, ca_len):
# 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)] = round(reporting_period_data['subtotals_per_unit_area'][i], 2)
#
# col = chr(ord(col) + 1)
#
# current_row_number += 1
ws['B' + str(current_row_number)].font = title_font 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)].alignment = c_c_alignment
ws['B' + str(current_row_number)].border = f_border ws['B' + str(current_row_number)].border = f_border
@ -318,136 +301,136 @@ def generate_excel(report,
current_row_number += 1 current_row_number += 1
#################################### ####################################
has_values_data = True has_values_data = True
has_timestamps_data = True has_timestamps_data = True
if 'values' not in reporting_period_data.keys() or \ if 'values' not in reporting_period_data.keys() or \
reporting_period_data['values'] is None or \ reporting_period_data['values'] is None or \
len(reporting_period_data['values']) == 0: len(reporting_period_data['values']) == 0:
has_values_data = False has_values_data = False
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 or \ len(reporting_period_data['timestamps']) == 0 or \
len(reporting_period_data['timestamps'][0]) == 0: len(reporting_period_data['timestamps'][0]) == 0:
has_timestamps_data = False has_timestamps_data = False
if has_values_data and has_timestamps_data: if has_values_data and has_timestamps_data:
ca_len = len(reporting_period_data['names']) ca_len = len(reporting_period_data['names'])
time = reporting_period_data['timestamps'][0] time = reporting_period_data['timestamps'][0]
ws['B' + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].font = title_font
ws['B' + str(current_row_number)] = name + ' 详细数据' ws['B' + str(current_row_number)] = name + ' 详细数据'
current_row_number += 1 current_row_number += 1
chart_start_row_number = current_row_number chart_start_row_number = current_row_number
current_row_number += ca_len * 6 current_row_number += ca_len * 6
table_start_row_number = current_row_number table_start_row_number = current_row_number
ws.row_dimensions[current_row_number].height = 60 ws.row_dimensions[current_row_number].height = 60
ws['B' + str(current_row_number)].fill = table_fill ws['B' + str(current_row_number)].fill = table_fill
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)] = '日期时间'
col = 'C'
for i in range(0, ca_len):
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)] = \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
col = chr(ord(col) + 1)
current_row_number += 1
for i in range(0, len(time)):
ws['B' + str(current_row_number)].font = title_font 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)].alignment = c_c_alignment
ws['B' + str(current_row_number)].border = f_border ws['B' + str(current_row_number)].border = f_border
ws['B' + str(current_row_number)] = '日期时间' ws['B' + str(current_row_number)] = time[i]
col = 'C' col = 'C'
for j in range(0, ca_len):
for i in range(0, ca_len):
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 = title_font
ws[col + str(current_row_number)].alignment = c_c_alignment 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)].border = f_border
ws[col + str(current_row_number)] = \ ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" if reporting_period_data['values'][j][i] is not None else 0.00
col = chr(ord(col) + 1) col = chr(ord(col) + 1)
current_row_number += 1 current_row_number += 1
for i in range(0, len(time)): table_end_row_number = current_row_number - 1
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)] = time[i]
col = 'C' ws['B' + str(current_row_number)].font = title_font
for j in range(0, ca_len): ws['B' + str(current_row_number)].alignment = c_c_alignment
ws[col + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].border = f_border
ws[col + str(current_row_number)].alignment = c_c_alignment ws['B' + str(current_row_number)] = '小计'
ws[col + str(current_row_number)].border = f_border
ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
if reporting_period_data['values'][j][i] is not None else 0.00
col = chr(ord(col) + 1)
current_row_number += 1 col = 'C'
table_end_row_number = current_row_number - 1 for i in range(0, ca_len):
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)] = round(reporting_period_data['subtotals'][i], 2)
col = chr(ord(col) + 1)
ws['B' + str(current_row_number)].font = title_font current_row_number += 2
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)] = '小计'
col = 'C' format_time_width_number = 1.0
min_len_number = 1.0
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0
for i in range(0, ca_len): if period_type == 'hourly':
ws[col + str(current_row_number)].font = title_font format_time_width_number = 4.0
ws[col + str(current_row_number)].alignment = c_c_alignment min_len_number = 2
ws[col + str(current_row_number)].border = f_border min_width_number = 12.0
ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) elif period_type == 'daily':
col = chr(ord(col) + 1) format_time_width_number = 2.5
min_len_number = 4
min_width_number = 14.0
elif period_type == 'monthly':
format_time_width_number = 2.1
min_len_number = 4
min_width_number = 12.4
elif period_type == 'yearly':
format_time_width_number = 1.5
min_len_number = 5
min_width_number = 11.5
current_row_number += 2 for i in range(0, ca_len):
line = LineChart()
format_time_width_number = 1.0 line.title = '报告期消耗 - ' + \
min_len_number = 1.0 reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0 labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
if period_type == 'hourly': line.add_data(line_data, titles_from_data=True)
format_time_width_number = 4.0 line.set_categories(labels)
min_len_number = 2 line_data = line.series[0]
min_width_number = 12.0 line_data.marker.symbol = "circle"
elif period_type == 'daily': line_data.smooth = True
format_time_width_number = 2.5 line.x_axis.crosses = 'min'
min_len_number = 4 line.height = 8.25
min_width_number = 14.0 line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
elif period_type == 'monthly': if line.width > 24:
format_time_width_number = 2.1 line.width = 24
min_len_number = 4 line.dLbls = DataLabelList()
min_width_number = 12.4 line.dLbls.dLblPos = 't'
elif period_type == 'yearly': line.dLbls.showVal = True
format_time_width_number = 1.5 line.dLbls.showPercent = False
min_len_number = 5 chart_col = 'B'
min_width_number = 11.5 chart_cell = chart_col + str(chart_start_row_number)
chart_start_row_number += 6
for i in range(0, ca_len): ws.add_chart(line, chart_cell)
line = LineChart()
line.title = '报告期消耗 - ' + \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
line.add_data(line_data, titles_from_data=True)
line.set_categories(labels)
line_data = line.series[0]
line_data.marker.symbol = "circle"
line_data.smooth = True
line.x_axis.crosses = 'min'
line.height = 8.25
line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
if line.width > 24:
line.width = 24
line.dLbls = DataLabelList()
line.dLbls.dLblPos = 't'
line.dLbls.showVal = True
line.dLbls.showPercent = False
chart_col = 'B'
chart_cell = chart_col + str(chart_start_row_number)
chart_start_row_number += 6
ws.add_chart(line, chart_cell)
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)

View File

@ -221,23 +221,6 @@ def generate_excel(report,
current_row_number += 1 current_row_number += 1
# 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)] = '单位面积值'
#
# col = 'C'
#
# for i in range(0, ca_len):
# 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)] = round(reporting_period_data['subtotals_per_unit_area'][i], 2)
#
# col = chr(ord(col) + 1)
#
# current_row_number += 1
ws['B' + str(current_row_number)].font = title_font 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)].alignment = c_c_alignment
ws['B' + str(current_row_number)].border = f_border ws['B' + str(current_row_number)].border = f_border
@ -257,136 +240,136 @@ def generate_excel(report,
current_row_number += 2 current_row_number += 2
#################################### ####################################
has_values_data = True has_values_data = True
has_timestamps_data = True has_timestamps_data = True
if 'values' not in reporting_period_data.keys() or \ if 'values' not in reporting_period_data.keys() or \
reporting_period_data['values'] is None or \ reporting_period_data['values'] is None or \
len(reporting_period_data['values']) == 0: len(reporting_period_data['values']) == 0:
has_values_data = False has_values_data = False
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 or \ len(reporting_period_data['timestamps']) == 0 or \
len(reporting_period_data['timestamps'][0]) == 0: len(reporting_period_data['timestamps'][0]) == 0:
has_timestamps_data = False has_timestamps_data = False
if has_values_data and has_timestamps_data: if has_values_data and has_timestamps_data:
ca_len = len(reporting_period_data['names']) ca_len = len(reporting_period_data['names'])
time = reporting_period_data['timestamps'][0] time = reporting_period_data['timestamps'][0]
ws['B' + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].font = title_font
ws['B' + str(current_row_number)] = name + ' 详细数据' ws['B' + str(current_row_number)] = name + ' 详细数据'
current_row_number += 1 current_row_number += 1
chart_start_row_number = current_row_number chart_start_row_number = current_row_number
current_row_number += ca_len * 6 current_row_number += ca_len * 6
table_start_row_number = current_row_number table_start_row_number = current_row_number
ws.row_dimensions[current_row_number].height = 60 ws.row_dimensions[current_row_number].height = 60
ws['B' + str(current_row_number)].fill = table_fill ws['B' + str(current_row_number)].fill = table_fill
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)] = '日期时间'
col = 'C'
for i in range(0, ca_len):
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)] = \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
col = chr(ord(col) + 1)
current_row_number += 1
for i in range(0, len(time)):
ws['B' + str(current_row_number)].font = title_font 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)].alignment = c_c_alignment
ws['B' + str(current_row_number)].border = f_border ws['B' + str(current_row_number)].border = f_border
ws['B' + str(current_row_number)] = '日期时间' ws['B' + str(current_row_number)] = time[i]
col = 'C' col = 'C'
for j in range(0, ca_len):
for i in range(0, ca_len):
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 = title_font
ws[col + str(current_row_number)].alignment = c_c_alignment 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)].border = f_border
ws[col + str(current_row_number)] = \ ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" if reporting_period_data['values'][j][i] is not None else 0.00
col = chr(ord(col) + 1) col = chr(ord(col) + 1)
current_row_number += 1 current_row_number += 1
for i in range(0, len(time)): table_end_row_number = current_row_number - 1
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)] = time[i]
col = 'C' ws['B' + str(current_row_number)].font = title_font
for j in range(0, ca_len): ws['B' + str(current_row_number)].alignment = c_c_alignment
ws[col + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].border = f_border
ws[col + str(current_row_number)].alignment = c_c_alignment ws['B' + str(current_row_number)] = '小计'
ws[col + str(current_row_number)].border = f_border
ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
if reporting_period_data['values'][j][i] is not None else 0.00
col = chr(ord(col) + 1)
current_row_number += 1 col = 'C'
table_end_row_number = current_row_number - 1 for i in range(0, ca_len):
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)] = round(reporting_period_data['subtotals'][i], 2)
col = chr(ord(col) + 1)
ws['B' + str(current_row_number)].font = title_font current_row_number += 2
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)] = '小计'
col = 'C' format_time_width_number = 1.0
min_len_number = 1.0
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0
for i in range(0, ca_len): if period_type == 'hourly':
ws[col + str(current_row_number)].font = title_font format_time_width_number = 4.0
ws[col + str(current_row_number)].alignment = c_c_alignment min_len_number = 2
ws[col + str(current_row_number)].border = f_border min_width_number = 12.0
ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) elif period_type == 'daily':
col = chr(ord(col) + 1) format_time_width_number = 2.5
min_len_number = 4
min_width_number = 14.0
elif period_type == 'monthly':
format_time_width_number = 2.1
min_len_number = 4
min_width_number = 12.4
elif period_type == 'yearly':
format_time_width_number = 1.5
min_len_number = 5
min_width_number = 11.5
current_row_number += 2 for i in range(0, ca_len):
line = LineChart()
format_time_width_number = 1.0 line.title = '报告期产出 - ' + \
min_len_number = 1.0 reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0 labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
if period_type == 'hourly': line.add_data(line_data, titles_from_data=True)
format_time_width_number = 4.0 line.set_categories(labels)
min_len_number = 2 line_data = line.series[0]
min_width_number = 12.0 line_data.marker.symbol = "circle"
elif period_type == 'daily': line_data.smooth = True
format_time_width_number = 2.5 line.x_axis.crosses = 'min'
min_len_number = 4 line.height = 8.25
min_width_number = 14.0 line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
elif period_type == 'monthly': if line.width > 24:
format_time_width_number = 2.1 line.width = 24
min_len_number = 4 line.dLbls = DataLabelList()
min_width_number = 12.4 line.dLbls.dLblPos = 't'
elif period_type == 'yearly': line.dLbls.showVal = True
format_time_width_number = 1.5 line.dLbls.showPercent = False
min_len_number = 5 chart_col = 'B'
min_width_number = 11.5 chart_cell = chart_col + str(chart_start_row_number)
chart_start_row_number += 6
for i in range(0, ca_len): ws.add_chart(line, chart_cell)
line = LineChart()
line.title = '报告期产出 - ' + \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
line.add_data(line_data, titles_from_data=True)
line.set_categories(labels)
line_data = line.series[0]
line_data.marker.symbol = "circle"
line_data.smooth = True
line.x_axis.crosses = 'min'
line.height = 8.25
line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
if line.width > 24:
line.width = 24
line.dLbls = DataLabelList()
line.dLbls.dLblPos = 't'
line.dLbls.showVal = True
line.dLbls.showPercent = False
chart_col = 'B'
chart_cell = chart_col + str(chart_start_row_number)
chart_start_row_number += 6
ws.add_chart(line, chart_cell)
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)

View File

@ -318,136 +318,136 @@ def generate_excel(report,
current_row_number += 1 current_row_number += 1
##################################### #####################################
has_values_data = True has_values_data = True
has_timestamps_data = True has_timestamps_data = True
if 'values' not in reporting_period_data.keys() or \ if 'values' not in reporting_period_data.keys() or \
reporting_period_data['values'] is None or \ reporting_period_data['values'] is None or \
len(reporting_period_data['values']) == 0: len(reporting_period_data['values']) == 0:
has_values_data = False has_values_data = False
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 or \ len(reporting_period_data['timestamps']) == 0 or \
len(reporting_period_data['timestamps'][0]) == 0: len(reporting_period_data['timestamps'][0]) == 0:
has_timestamps_data = False has_timestamps_data = False
if has_values_data and has_timestamps_data: if has_values_data and has_timestamps_data:
ca_len = len(reporting_period_data['names']) ca_len = len(reporting_period_data['names'])
time = reporting_period_data['timestamps'][0] time = reporting_period_data['timestamps'][0]
ws['B' + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].font = title_font
ws['B' + str(current_row_number)] = name + ' 详细数据' ws['B' + str(current_row_number)] = name + ' 详细数据'
current_row_number += 1 current_row_number += 1
chart_start_row_number = current_row_number chart_start_row_number = current_row_number
current_row_number += ca_len * 6 current_row_number += ca_len * 6
table_start_row_number = current_row_number table_start_row_number = current_row_number
ws.row_dimensions[current_row_number].height = 60 ws.row_dimensions[current_row_number].height = 60
ws['B' + str(current_row_number)].fill = table_fill ws['B' + str(current_row_number)].fill = table_fill
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)] = '日期时间'
col = 'C'
for i in range(0, ca_len):
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)] = \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
col = chr(ord(col) + 1)
current_row_number += 1
for i in range(0, len(time)):
ws['B' + str(current_row_number)].font = title_font 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)].alignment = c_c_alignment
ws['B' + str(current_row_number)].border = f_border ws['B' + str(current_row_number)].border = f_border
ws['B' + str(current_row_number)] = '日期时间' ws['B' + str(current_row_number)] = time[i]
col = 'C' col = 'C'
for j in range(0, ca_len):
for i in range(0, ca_len):
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 = title_font
ws[col + str(current_row_number)].alignment = c_c_alignment 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)].border = f_border
ws[col + str(current_row_number)] = \ ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" if reporting_period_data['values'][j][i] is not None else 0.00
col = chr(ord(col) + 1) col = chr(ord(col) + 1)
current_row_number += 1 current_row_number += 1
for i in range(0, len(time)): table_end_row_number = current_row_number - 1
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)] = time[i]
col = 'C' ws['B' + str(current_row_number)].font = title_font
for j in range(0, ca_len): ws['B' + str(current_row_number)].alignment = c_c_alignment
ws[col + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].border = f_border
ws[col + str(current_row_number)].alignment = c_c_alignment ws['B' + str(current_row_number)] = '小计'
ws[col + str(current_row_number)].border = f_border
ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
if reporting_period_data['values'][j][i] is not None else 0.00
col = chr(ord(col) + 1)
current_row_number += 1 col = 'C'
table_end_row_number = current_row_number - 1 for i in range(0, ca_len):
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)] = round(reporting_period_data['subtotals'][i], 2)
col = chr(ord(col) + 1)
ws['B' + str(current_row_number)].font = title_font current_row_number += 2
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)] = '小计'
col = 'C' format_time_width_number = 1.0
min_len_number = 1.0
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0
for i in range(0, ca_len): if period_type == 'hourly':
ws[col + str(current_row_number)].font = title_font format_time_width_number = 4.0
ws[col + str(current_row_number)].alignment = c_c_alignment min_len_number = 2
ws[col + str(current_row_number)].border = f_border min_width_number = 12.0
ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) elif period_type == 'daily':
col = chr(ord(col) + 1) format_time_width_number = 2.5
min_len_number = 4
min_width_number = 14.0
elif period_type == 'monthly':
format_time_width_number = 2.1
min_len_number = 4
min_width_number = 12.4
elif period_type == 'yearly':
format_time_width_number = 1.5
min_len_number = 5
min_width_number = 11.5
current_row_number += 2 for i in range(0, ca_len):
line = LineChart()
format_time_width_number = 1.0 line.title = '报告期消耗 - ' + \
min_len_number = 1.0 reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0 labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
if period_type == 'hourly': line.add_data(line_data, titles_from_data=True)
format_time_width_number = 4.0 line.set_categories(labels)
min_len_number = 2 line_data = line.series[0]
min_width_number = 12.0 line_data.marker.symbol = "circle"
elif period_type == 'daily': line_data.smooth = True
format_time_width_number = 2.5 line.x_axis.crosses = 'min'
min_len_number = 4 line.height = 8.25
min_width_number = 14.0 line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
elif period_type == 'monthly': if line.width > 24:
format_time_width_number = 2.1 line.width = 24
min_len_number = 4 line.dLbls = DataLabelList()
min_width_number = 12.4 line.dLbls.dLblPos = 't'
elif period_type == 'yearly': line.dLbls.showVal = True
format_time_width_number = 1.5 line.dLbls.showPercent = False
min_len_number = 5 chart_col = 'B'
min_width_number = 11.5 chart_cell = chart_col + str(chart_start_row_number)
chart_start_row_number += 6
for i in range(0, ca_len): ws.add_chart(line, chart_cell)
line = LineChart()
line.title = '报告期消耗 - ' + \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
line.add_data(line_data, titles_from_data=True)
line.set_categories(labels)
line_data = line.series[0]
line_data.marker.symbol = "circle"
line_data.smooth = True
line.x_axis.crosses = 'min'
line.height = 8.25
line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
if line.width > 24:
line.width = 24
line.dLbls = DataLabelList()
line.dLbls.dLblPos = 't'
line.dLbls.showVal = True
line.dLbls.showPercent = False
chart_col = 'B'
chart_cell = chart_col + str(chart_start_row_number)
chart_start_row_number += 6
ws.add_chart(line, chart_cell)
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)

View File

@ -257,224 +257,224 @@ def generate_excel(report,
current_row_number += 2 current_row_number += 2
####################### #######################
has_values_data = True has_values_data = True
has_timestamps_data = True has_timestamps_data = True
if 'values' not in reporting_period_data.keys() or \ if 'values' not in reporting_period_data.keys() or \
reporting_period_data['values'] is None or \ reporting_period_data['values'] is None or \
len(reporting_period_data['values']) == 0: len(reporting_period_data['values']) == 0:
has_values_data = False has_values_data = False
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 or \ len(reporting_period_data['timestamps']) == 0 or \
len(reporting_period_data['timestamps'][0]) == 0: len(reporting_period_data['timestamps'][0]) == 0:
has_timestamps_data = False has_timestamps_data = False
if has_values_data and has_timestamps_data: if has_values_data and has_timestamps_data:
ca_len = len(reporting_period_data['names']) ca_len = len(reporting_period_data['names'])
time = reporting_period_data['timestamps'][0] time = reporting_period_data['timestamps'][0]
ws['B' + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].font = title_font
ws['B' + str(current_row_number)] = name + ' 详细数据' ws['B' + str(current_row_number)] = name + ' 详细数据'
current_row_number += 1 current_row_number += 1
chart_start_row_number = current_row_number chart_start_row_number = current_row_number
current_row_number += ca_len * 6 current_row_number += ca_len * 6
table_start_row_number = current_row_number table_start_row_number = current_row_number
ws.row_dimensions[current_row_number].height = 60 ws.row_dimensions[current_row_number].height = 60
ws['B' + str(current_row_number)].fill = table_fill ws['B' + str(current_row_number)].fill = table_fill
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)] = '日期时间'
col = 'C'
for i in range(0, ca_len):
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)] = \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
col = chr(ord(col) + 1)
current_row_number += 1
for i in range(0, len(time)):
ws['B' + str(current_row_number)].font = title_font 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)].alignment = c_c_alignment
ws['B' + str(current_row_number)].border = f_border ws['B' + str(current_row_number)].border = f_border
ws['B' + str(current_row_number)] = '日期时间' ws['B' + str(current_row_number)] = time[i]
col = 'C' col = 'C'
for j in range(0, ca_len):
for i in range(0, ca_len):
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 = title_font
ws[col + str(current_row_number)].alignment = c_c_alignment 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)].border = f_border
ws[col + str(current_row_number)] = \ ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" if reporting_period_data['values'][j][i] is not None else 0.00
col = chr(ord(col) + 1) col = chr(ord(col) + 1)
current_row_number += 1 current_row_number += 1
for i in range(0, len(time)): table_end_row_number = current_row_number - 1
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)] = time[i]
col = 'C' ws['B' + str(current_row_number)].font = title_font
for j in range(0, ca_len): ws['B' + str(current_row_number)].alignment = c_c_alignment
ws[col + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].border = f_border
ws[col + str(current_row_number)].alignment = c_c_alignment ws['B' + str(current_row_number)] = '小计'
ws[col + str(current_row_number)].border = f_border
ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
if reporting_period_data['values'][j][i] is not None else 0.00
col = chr(ord(col) + 1)
current_row_number += 1 col = 'C'
table_end_row_number = current_row_number - 1 for i in range(0, ca_len):
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)] = round(reporting_period_data['subtotals'][i], 2)
col = chr(ord(col) + 1)
ws['B' + str(current_row_number)].font = title_font current_row_number += 2
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)] = '小计'
col = 'C' format_time_width_number = 1.0
min_len_number = 1.0
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0
for i in range(0, ca_len): if period_type == 'hourly':
ws[col + str(current_row_number)].font = title_font format_time_width_number = 4.0
ws[col + str(current_row_number)].alignment = c_c_alignment min_len_number = 2
ws[col + str(current_row_number)].border = f_border min_width_number = 12.0
ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) elif period_type == 'daily':
col = chr(ord(col) + 1) format_time_width_number = 2.5
min_len_number = 4
min_width_number = 14.0
elif period_type == 'monthly':
format_time_width_number = 2.1
min_len_number = 4
min_width_number = 12.4
elif period_type == 'yearly':
format_time_width_number = 1.5
min_len_number = 5
min_width_number = 11.5
current_row_number += 2 for i in range(0, ca_len):
line = LineChart()
line.title = '报告期产出 - ' + \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
line.add_data(line_data, titles_from_data=True)
line.set_categories(labels)
line_data = line.series[0]
line_data.marker.symbol = "circle"
line_data.smooth = True
line.x_axis.crosses = 'min'
line.height = 8.25
line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
if line.width > 24:
line.width = 24
line.dLbls = DataLabelList()
line.dLbls.dLblPos = 't'
line.dLbls.showVal = True
line.dLbls.showPercent = False
chart_col = 'B'
chart_cell = chart_col + str(chart_start_row_number)
chart_start_row_number += 6
ws.add_chart(line, chart_cell)
format_time_width_number = 1.0 #####################################
min_len_number = 1.0
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0
if period_type == 'hourly': has_child_flag = True
format_time_width_number = 4.0
min_len_number = 2
min_width_number = 12.0
elif period_type == 'daily':
format_time_width_number = 2.5
min_len_number = 4
min_width_number = 14.0
elif period_type == 'monthly':
format_time_width_number = 2.1
min_len_number = 4
min_width_number = 12.4
elif period_type == 'yearly':
format_time_width_number = 1.5
min_len_number = 5
min_width_number = 11.5
for i in range(0, ca_len): if "child_space" not in report.keys() or "energy_category_names" not in report['child_space'].keys() or \
line = LineChart() len(report['child_space']["energy_category_names"]) == 0 \
line.title = '报告期产出 - ' + \ or 'child_space_names_array' not in report['child_space'].keys() \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" or report['child_space']['child_space_names_array'] is None \
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) or len(report['child_space']['child_space_names_array']) == 0 \
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number) or len(report['child_space']['child_space_names_array'][0]) == 0:
line.add_data(line_data, titles_from_data=True) has_child_flag = False
line.set_categories(labels)
line_data = line.series[0]
line_data.marker.symbol = "circle"
line_data.smooth = True
line.x_axis.crosses = 'min'
line.height = 8.25
line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
if line.width > 24:
line.width = 24
line.dLbls = DataLabelList()
line.dLbls.dLblPos = 't'
line.dLbls.showVal = True
line.dLbls.showPercent = False
chart_col = 'B'
chart_cell = chart_col + str(chart_start_row_number)
chart_start_row_number += 6
ws.add_chart(line, chart_cell)
##################################### if has_child_flag:
child = report['child_space']
has_child_flag = True ws['B' + str(current_row_number)].font = title_font
ws['B' + str(current_row_number)] = name + ' 子空间数据'
if "child_space" not in report.keys() or "energy_category_names" not in report['child_space'].keys() or \ current_row_number += 1
len(report['child_space']["energy_category_names"]) == 0 \ table_start_row_number = current_row_number
or 'child_space_names_array' not in report['child_space'].keys() \
or report['child_space']['child_space_names_array'] 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: ws.row_dimensions[current_row_number].height = 60
child = report['child_space'] 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'])
ws['B' + str(current_row_number)].font = title_font for i in range(0, ca_len):
ws['B' + str(current_row_number)] = name + ' 子空间数据' row = chr(ord('C') + i)
ws[row + str(current_row_number)].fill = table_fill
ws[row + str(current_row_number)].font = name_font
ws[row + str(current_row_number)].alignment = c_c_alignment
ws[row + str(current_row_number)].border = f_border
ws[row + str(current_row_number)] = \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
space_len = len(child['child_space_names_array'][0])
for i in range(0, space_len):
current_row_number += 1 current_row_number += 1
table_start_row_number = current_row_number row = str(current_row_number)
ws.row_dimensions[current_row_number].height = 60 ws['B' + row].font = name_font
ws['B' + str(current_row_number)].fill = table_fill ws['B' + row].alignment = c_c_alignment
ws['B' + str(current_row_number)].border = f_border ws['B' + row] = child['child_space_names_array'][0][i]
ca_len = len(child['energy_category_names']) ws['B' + row].border = f_border
for i in range(0, ca_len): for j in range(0, ca_len):
row = chr(ord('C') + i) col = chr(ord('C') + j)
ws[row + str(current_row_number)].fill = table_fill ws[col + row].font = name_font
ws[row + str(current_row_number)].font = name_font ws[col + row].alignment = c_c_alignment
ws[row + str(current_row_number)].alignment = c_c_alignment ws[col + row] = round(child['subtotals_array'][j][i], 2)
ws[row + str(current_row_number)].border = f_border ws[col + row].border = f_border
ws[row + str(current_row_number)] = \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
space_len = len(child['child_space_names_array'][0]) table_end_row_number = current_row_number
current_row_number += 1
pie_start_row_number = current_row_number
for i in range(0, space_len): # Pie
current_row_number += 1 for i in range(0, ca_len):
row = str(current_row_number) 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(pie_start_row_number)
else:
chart_cell = 'E' + str(pie_start_row_number)
pie_start_row_number += 5
ws.add_chart(pie, chart_cell)
ws['B' + row].font = name_font current_row_number = pie_start_row_number
ws['B' + row].alignment = c_c_alignment if ca_len % 2 == 1:
ws['B' + row] = child['child_space_names_array'][0][i] current_row_number += 5
ws['B' + row].border = f_border
for j in range(0, ca_len): current_row_number += 1
col = chr(ord('C') + j)
ws[col + row].font = name_font
ws[col + row].alignment = c_c_alignment
ws[col + row] = round(child['subtotals_array'][j][i], 2)
ws[col + row].border = f_border
table_end_row_number = current_row_number
current_row_number += 1
pie_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(pie_start_row_number)
else:
chart_cell = 'E' + str(pie_start_row_number)
pie_start_row_number += 5
ws.add_chart(pie, chart_cell)
current_row_number = pie_start_row_number
if ca_len % 2 == 1:
current_row_number += 5
current_row_number += 1
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)

View File

@ -318,136 +318,136 @@ def generate_excel(report,
current_row_number += 1 current_row_number += 1
####################### #######################
has_values_data = True has_values_data = True
has_timestamps_data = True has_timestamps_data = True
if 'values' not in reporting_period_data.keys() or \ if 'values' not in reporting_period_data.keys() or \
reporting_period_data['values'] is None or \ reporting_period_data['values'] is None or \
len(reporting_period_data['values']) == 0: len(reporting_period_data['values']) == 0:
has_values_data = False has_values_data = False
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 or \ len(reporting_period_data['timestamps']) == 0 or \
len(reporting_period_data['timestamps'][0]) == 0: len(reporting_period_data['timestamps'][0]) == 0:
has_timestamps_data = False has_timestamps_data = False
if has_values_data and has_timestamps_data: if has_values_data and has_timestamps_data:
ca_len = len(reporting_period_data['names']) ca_len = len(reporting_period_data['names'])
time = reporting_period_data['timestamps'][0] time = reporting_period_data['timestamps'][0]
ws['B' + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].font = title_font
ws['B' + str(current_row_number)] = name + ' 详细数据' ws['B' + str(current_row_number)] = name + ' 详细数据'
current_row_number += 1 current_row_number += 1
chart_start_row_number = current_row_number chart_start_row_number = current_row_number
current_row_number += ca_len * 6 current_row_number += ca_len * 6
table_start_row_number = current_row_number table_start_row_number = current_row_number
ws.row_dimensions[current_row_number].height = 60 ws.row_dimensions[current_row_number].height = 60
ws['B' + str(current_row_number)].fill = table_fill ws['B' + str(current_row_number)].fill = table_fill
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)] = '日期时间'
col = 'C'
for i in range(0, ca_len):
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)] = \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
col = chr(ord(col) + 1)
current_row_number += 1
for i in range(0, len(time)):
ws['B' + str(current_row_number)].font = title_font 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)].alignment = c_c_alignment
ws['B' + str(current_row_number)].border = f_border ws['B' + str(current_row_number)].border = f_border
ws['B' + str(current_row_number)] = '日期时间' ws['B' + str(current_row_number)] = time[i]
col = 'C' col = 'C'
for j in range(0, ca_len):
for i in range(0, ca_len):
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 = title_font
ws[col + str(current_row_number)].alignment = c_c_alignment 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)].border = f_border
ws[col + str(current_row_number)] = \ ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" if reporting_period_data['values'][j][i] is not None else 0.00
col = chr(ord(col) + 1) col = chr(ord(col) + 1)
current_row_number += 1 current_row_number += 1
for i in range(0, len(time)): table_end_row_number = current_row_number - 1
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)] = time[i]
col = 'C' ws['B' + str(current_row_number)].font = title_font
for j in range(0, ca_len): ws['B' + str(current_row_number)].alignment = c_c_alignment
ws[col + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].border = f_border
ws[col + str(current_row_number)].alignment = c_c_alignment ws['B' + str(current_row_number)] = '小计'
ws[col + str(current_row_number)].border = f_border
ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
if reporting_period_data['values'][j][i] is not None else 0.00
col = chr(ord(col) + 1)
current_row_number += 1 col = 'C'
table_end_row_number = current_row_number - 1 for i in range(0, ca_len):
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)] = round(reporting_period_data['subtotals'][i], 2)
col = chr(ord(col) + 1)
ws['B' + str(current_row_number)].font = title_font current_row_number += 2
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)] = '小计'
col = 'C' format_time_width_number = 1.0
min_len_number = 1.0
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0
for i in range(0, ca_len): if period_type == 'hourly':
ws[col + str(current_row_number)].font = title_font format_time_width_number = 4.0
ws[col + str(current_row_number)].alignment = c_c_alignment min_len_number = 2
ws[col + str(current_row_number)].border = f_border min_width_number = 12.0
ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) elif period_type == 'daily':
col = chr(ord(col) + 1) format_time_width_number = 2.5
min_len_number = 4
min_width_number = 14.0
elif period_type == 'monthly':
format_time_width_number = 2.1
min_len_number = 4
min_width_number = 12.4
elif period_type == 'yearly':
format_time_width_number = 1.5
min_len_number = 5
min_width_number = 11.5
current_row_number += 2 for i in range(0, ca_len):
line = LineChart()
format_time_width_number = 1.0 line.title = '报告期消耗 - ' + \
min_len_number = 1.0 reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0 labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
if period_type == 'hourly': line.add_data(line_data, titles_from_data=True)
format_time_width_number = 4.0 line.set_categories(labels)
min_len_number = 2 line_data = line.series[0]
min_width_number = 12.0 line_data.marker.symbol = "circle"
elif period_type == 'daily': line_data.smooth = True
format_time_width_number = 2.5 line.x_axis.crosses = 'min'
min_len_number = 4 line.height = 8.25
min_width_number = 14.0 line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
elif period_type == 'monthly': if line.width > 24:
format_time_width_number = 2.1 line.width = 24
min_len_number = 4 line.dLbls = DataLabelList()
min_width_number = 12.4 line.dLbls.dLblPos = 't'
elif period_type == 'yearly': line.dLbls.showVal = True
format_time_width_number = 1.5 line.dLbls.showPercent = False
min_len_number = 5 chart_col = 'B'
min_width_number = 11.5 chart_cell = chart_col + str(chart_start_row_number)
chart_start_row_number += 6
for i in range(0, ca_len): ws.add_chart(line, chart_cell)
line = LineChart()
line.title = '报告期消耗 - ' + \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
line.add_data(line_data, titles_from_data=True)
line.set_categories(labels)
line_data = line.series[0]
line_data.marker.symbol = "circle"
line_data.smooth = True
line.x_axis.crosses = 'min'
line.height = 8.25
line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
if line.width > 24:
line.width = 24
line.dLbls = DataLabelList()
line.dLbls.dLblPos = 't'
line.dLbls.showVal = True
line.dLbls.showPercent = False
chart_col = 'B'
chart_cell = chart_col + str(chart_start_row_number)
chart_start_row_number += 6
ws.add_chart(line, chart_cell)
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)

View File

@ -318,136 +318,136 @@ def generate_excel(report,
current_row_number += 1 current_row_number += 1
####################### #######################
has_values_data = True has_values_data = True
has_timestamps_data = True has_timestamps_data = True
if 'values' not in reporting_period_data.keys() or \ if 'values' not in reporting_period_data.keys() or \
reporting_period_data['values'] is None or \ reporting_period_data['values'] is None or \
len(reporting_period_data['values']) == 0: len(reporting_period_data['values']) == 0:
has_values_data = False has_values_data = False
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 or \ len(reporting_period_data['timestamps']) == 0 or \
len(reporting_period_data['timestamps'][0]) == 0: len(reporting_period_data['timestamps'][0]) == 0:
has_timestamps_data = False has_timestamps_data = False
if has_values_data and has_timestamps_data: if has_values_data and has_timestamps_data:
ca_len = len(reporting_period_data['names']) ca_len = len(reporting_period_data['names'])
time = reporting_period_data['timestamps'][0] time = reporting_period_data['timestamps'][0]
ws['B' + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].font = title_font
ws['B' + str(current_row_number)] = name + ' 详细数据' ws['B' + str(current_row_number)] = name + ' 详细数据'
current_row_number += 1 current_row_number += 1
chart_start_row_number = current_row_number chart_start_row_number = current_row_number
current_row_number += ca_len * 6 current_row_number += ca_len * 6
table_start_row_number = current_row_number table_start_row_number = current_row_number
ws.row_dimensions[current_row_number].height = 60 ws.row_dimensions[current_row_number].height = 60
ws['B' + str(current_row_number)].fill = table_fill ws['B' + str(current_row_number)].fill = table_fill
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)] = '日期时间'
col = 'C'
for i in range(0, ca_len):
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)] = \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
col = chr(ord(col) + 1)
current_row_number += 1
for i in range(0, len(time)):
ws['B' + str(current_row_number)].font = title_font 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)].alignment = c_c_alignment
ws['B' + str(current_row_number)].border = f_border ws['B' + str(current_row_number)].border = f_border
ws['B' + str(current_row_number)] = '日期时间' ws['B' + str(current_row_number)] = time[i]
col = 'C' col = 'C'
for j in range(0, ca_len):
for i in range(0, ca_len):
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 = title_font
ws[col + str(current_row_number)].alignment = c_c_alignment 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)].border = f_border
ws[col + str(current_row_number)] = \ ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" if reporting_period_data['values'][j][i] is not None else 0.00
col = chr(ord(col) + 1) col = chr(ord(col) + 1)
current_row_number += 1 current_row_number += 1
for i in range(0, len(time)): table_end_row_number = current_row_number - 1
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)] = time[i]
col = 'C' ws['B' + str(current_row_number)].font = title_font
for j in range(0, ca_len): ws['B' + str(current_row_number)].alignment = c_c_alignment
ws[col + str(current_row_number)].font = title_font ws['B' + str(current_row_number)].border = f_border
ws[col + str(current_row_number)].alignment = c_c_alignment ws['B' + str(current_row_number)] = '小计'
ws[col + str(current_row_number)].border = f_border
ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
if reporting_period_data['values'][j][i] is not None else 0.00
col = chr(ord(col) + 1)
current_row_number += 1 col = 'C'
table_end_row_number = current_row_number - 1 for i in range(0, ca_len):
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)] = round(reporting_period_data['subtotals'][i], 2)
col = chr(ord(col) + 1)
ws['B' + str(current_row_number)].font = title_font current_row_number += 2
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)] = '小计'
col = 'C' format_time_width_number = 1.0
min_len_number = 1.0
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0
for i in range(0, ca_len): if period_type == 'hourly':
ws[col + str(current_row_number)].font = title_font format_time_width_number = 4.0
ws[col + str(current_row_number)].alignment = c_c_alignment min_len_number = 2
ws[col + str(current_row_number)].border = f_border min_width_number = 12.0
ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) elif period_type == 'daily':
col = chr(ord(col) + 1) format_time_width_number = 2.5
min_len_number = 4
min_width_number = 14.0
elif period_type == 'monthly':
format_time_width_number = 2.1
min_len_number = 4
min_width_number = 12.4
elif period_type == 'yearly':
format_time_width_number = 1.5
min_len_number = 5
min_width_number = 11.5
current_row_number += 2 for i in range(0, ca_len):
line = LineChart()
format_time_width_number = 1.0 line.title = '报告期消耗 - ' + \
min_len_number = 1.0 reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0 labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
if period_type == 'hourly': line.add_data(line_data, titles_from_data=True)
format_time_width_number = 4.0 line.set_categories(labels)
min_len_number = 2 line_data = line.series[0]
min_width_number = 12.0 line_data.marker.symbol = "circle"
elif period_type == 'daily': line_data.smooth = True
format_time_width_number = 2.5 line.x_axis.crosses = 'min'
min_len_number = 4 line.height = 8.25
min_width_number = 14.0 line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
elif period_type == 'monthly': if line.width > 24:
format_time_width_number = 2.1 line.width = 24
min_len_number = 4 line.dLbls = DataLabelList()
min_width_number = 12.4 line.dLbls.dLblPos = 't'
elif period_type == 'yearly': line.dLbls.showVal = True
format_time_width_number = 1.5 line.dLbls.showPercent = False
min_len_number = 5 chart_col = 'B'
min_width_number = 11.5 chart_cell = chart_col + str(chart_start_row_number)
chart_start_row_number += 6
for i in range(0, ca_len): ws.add_chart(line, chart_cell)
line = LineChart()
line.title = '报告期消耗 - ' + \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
line.add_data(line_data, titles_from_data=True)
line.set_categories(labels)
line_data = line.series[0]
line_data.marker.symbol = "circle"
line_data.smooth = True
line.x_axis.crosses = 'min'
line.height = 8.25
line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number
if line.width > 24:
line.width = 24
line.dLbls = DataLabelList()
line.dLbls.dLblPos = 't'
line.dLbls.showVal = True
line.dLbls.showPercent = False
chart_col = 'B'
chart_cell = chart_col + str(chart_start_row_number)
chart_start_row_number += 6
ws.add_chart(line, chart_cell)
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)