Change the location of the judgment code logic

pull/6/head
YangZhang-GitHub 2021-03-05 16:10:40 +08:00
parent 9c603094ac
commit 7344e7745f
7 changed files with 714 additions and 765 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

@ -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)