Change the code variable name and add the LineChart comment

pull/16/head
Caozhenhui 2021-03-10 19:27:30 +08:00
parent cdfe37dd2a
commit 2d966aa46a
9 changed files with 609 additions and 620 deletions

View File

@ -113,12 +113,6 @@ def generate_excel(report,
wrap_text=False, wrap_text=False,
shrink_to_fit=False, shrink_to_fit=False,
indent=0) indent=0)
# c_r_alignment = Alignment(vertical='bottom',
# horizontal='center',
# text_rotation=0,
# wrap_text=False,
# shrink_to_fit=False,
# indent=0)
# Img # Img
img = Image("excelexporters/myems.png") img = Image("excelexporters/myems.png")
@ -262,9 +256,9 @@ def generate_excel(report,
######################################################## ########################################################
# Third: 详细数据 # Third: 详细数据
# row_sat~ row_sat + 6*cal_len: line # analysis_end_row_number~ analysis_end_row_number + 6*cal_len: line
# row_da: table title # detailed_start_row_number: table title
# row_da + 1~: table_data # detailed_start_row_number + 1~: table_data
######################################################## ########################################################
has_timestamps_flag = True has_timestamps_flag = True
if "timestamps" not in reporting_period_data.keys() or \ if "timestamps" not in reporting_period_data.keys() or \
@ -277,40 +271,37 @@ def generate_excel(report,
names = reporting_period_data['names'] names = reporting_period_data['names']
ca_len = len(names) ca_len = len(names)
time_len = len(timestamps) time_len = len(timestamps)
# row_lines == the number of rows of lines line_charts_row_number = 6 * ca_len
row_lines = 6 * ca_len analysis_end_row_number = 9 + 2 * ca_len
# row_sat == the number of rows of statistical analysis table detailed_start_row_number = analysis_end_row_number + line_charts_row_number + 1
row_sat = 9 + 2 * ca_len
# row_da == the number of rows of Detailed data
row_da = row_sat + row_lines + 1
ws['B' + str(row_da)].font = title_font ws['B' + str(detailed_start_row_number)].font = title_font
ws['B' + str(row_da)] = name + ' 详细数据' ws['B' + str(detailed_start_row_number)] = name + ' 详细数据'
# table_title # table_title
ws['B' + str(row_da + 1)].fill = table_fill ws['B' + str(detailed_start_row_number + 1)].fill = table_fill
ws['B' + str(row_da + 1)].font = name_font ws['B' + str(detailed_start_row_number + 1)].font = name_font
ws['B' + str(row_da + 1)].alignment = c_c_alignment ws['B' + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws['B' + str(row_da + 1)] = "日期时间" ws['B' + str(detailed_start_row_number + 1)] = "日期时间"
ws['B' + str(row_da + 1)].border = f_border ws['B' + str(detailed_start_row_number + 1)].border = f_border
for i in range(0, ca_len): for i in range(0, ca_len):
col_average = chr(ord('C') + 2 * i) col_average = chr(ord('C') + 2 * i)
col_maximum = chr(ord('D') + 2 * i) col_maximum = chr(ord('D') + 2 * i)
ws[col_average + str(row_da + 1)].font = name_font ws[col_average + str(detailed_start_row_number + 1)].font = name_font
ws[col_average + str(row_da + 1)].alignment = c_c_alignment ws[col_average + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws[col_average + str(row_da + 1)] = names[i] + " 平均负荷(" + reporting_period_data['units'][ ws[col_average + str(detailed_start_row_number + 1)] = names[i] + " 平均负荷(" + reporting_period_data['units'][
i] + "/H)" i] + "/H)"
ws[col_average + str(row_da + 1)].border = f_border ws[col_average + str(detailed_start_row_number + 1)].border = f_border
ws[col_maximum + str(row_da + 1)].font = name_font ws[col_maximum + str(detailed_start_row_number + 1)].font = name_font
ws[col_maximum + str(row_da + 1)].alignment = c_c_alignment ws[col_maximum + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws[col_maximum + str(row_da + 1)] = names[i] + " 最大负荷(" + reporting_period_data['units'][ ws[col_maximum + str(detailed_start_row_number + 1)] = names[i] + " 最大负荷(" + reporting_period_data['units'][
i] + "/H)" i] + "/H)"
ws[col_maximum + str(row_da + 1)].border = f_border ws[col_maximum + str(detailed_start_row_number + 1)].border = f_border
# table_date # table_date
for i in range(0, time_len): for i in range(0, time_len):
rows = i + row_da + 2 rows = i + detailed_start_row_number + 2
ws['B' + str(rows)].font = name_font ws['B' + str(rows)].font = name_font
ws['B' + str(rows)].alignment = c_c_alignment ws['B' + str(rows)].alignment = c_c_alignment
@ -335,32 +326,36 @@ def generate_excel(report,
ws[col_maximum + str(rows)].number_format = '0.00' ws[col_maximum + str(rows)].number_format = '0.00'
ws[col_maximum + str(rows)].border = f_border ws[col_maximum + str(rows)].border = f_border
# LineChart ########################################################
# third: LineChart
# LineChart requires data from the detailed data table in the Excel file
# so print the detailed data table first and then print LineChart
########################################################
for i in range(0, ca_len): for i in range(0, ca_len):
lc = LineChart() line = LineChart()
lc.title = "报告期 最大负荷" line.title = "报告期 最大负荷 - " + names[i] + "(" + reporting_period_data['units'][i] + ")"
lc.style = 10 line.style = 10
lc.x_axis.majorTickMark = 'in' line.x_axis.majorTickMark = 'in'
lc.y_axis.majorTickMark = 'in' line.y_axis.majorTickMark = 'in'
lc.smooth = True line.smooth = True
lc.x_axis.crosses = 'min' line.x_axis.crosses = 'min'
lc.height = 8.25 line.height = 8.25
lc.width = 24 line.width = 24
lc.dLbls = DataLabelList() line.dLbls = DataLabelList()
lc.dLbls.dLblPos = 't' line.dLbls.dLblPos = 't'
lc.dLbls.showVal = True line.dLbls.showVal = True
times = Reference(ws, min_col=2, min_row=row_da + 2, times = Reference(ws, min_col=2, min_row=detailed_start_row_number + 2,
max_row=row_da + 2 + time_len) max_row=detailed_start_row_number + 2 + time_len)
lc_data = Reference(ws, min_col=2 + 2 * (i+1), min_row=row_da + 1, line_data = Reference(ws, min_col=2 + 2 * (i + 1), min_row=detailed_start_row_number + 1,
max_row=row_da + 1 + time_len) max_row=detailed_start_row_number + 1 + time_len)
lc.add_data(lc_data, titles_from_data=True) line.add_data(line_data, titles_from_data=True)
lc.set_categories(times) line.set_categories(times)
ser = lc.series[0] ser = line.series[0]
ser.marker.symbol = "diamond" ser.marker.symbol = "diamond"
ser.marker.size = 5 ser.marker.size = 5
chart_col = 'B' chart_col = 'B'
chart_cell = str(row_sat + 6 * i) chart_cell = str(analysis_end_row_number + 6 * i)
ws.add_chart(lc, chart_col + chart_cell) ws.add_chart(line, chart_col + chart_cell)
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)

View File

@ -114,12 +114,6 @@ def generate_excel(report,
wrap_text=False, wrap_text=False,
shrink_to_fit=False, shrink_to_fit=False,
indent=0) indent=0)
# c_r_alignment = Alignment(vertical='bottom',
# horizontal='center',
# text_rotation=0,
# wrap_text=False,
# shrink_to_fit=False,
# indent=0)
# Img # Img
img = Image("excelexporters/myems.png") img = Image("excelexporters/myems.png")
@ -317,11 +311,8 @@ def generate_excel(report,
######################################################## ########################################################
# Second: 详细数据 # Second: 详细数据
# row_sat+1~ row_sat+1+row_lines+: line # analysis_end_row_number+1~ analysis_end_row_number+1+line_charts_row_number+: line
# row_ddt~ : the detailed data table # detailed_start_row_number~ : the detailed data table
# row_sat : the number of rows of the analysis table
# row_lines : the number of rows of all line charts
# row_ddt : The number of rows in which the detailed data table header resides
######################################################## ########################################################
has_timestamps_flag = True has_timestamps_flag = True
if "timestamps" not in reporting_period_data.keys() or \ if "timestamps" not in reporting_period_data.keys() or \
@ -336,29 +327,29 @@ def generate_excel(report,
ca_len = len(names) ca_len = len(names)
time_len = len(timestamps) time_len = len(timestamps)
# the detailed title # the detailed title
row_lines = 6 * ca_len + 1 line_charts_row_number = 6 * ca_len + 1
row_sat = 9 + 2 * ca_len analysis_end_row_number = 9 + 2 * ca_len
row_ddt = row_sat + row_lines detailed_start_row_number = analysis_end_row_number + line_charts_row_number
ws['B' + str(row_ddt)].font = title_font ws['B' + str(detailed_start_row_number)].font = title_font
ws['B' + str(row_ddt)] = name + ' 详细数据' ws['B' + str(detailed_start_row_number)] = name + ' 详细数据'
# the detailed table_title # the detailed table_title
ws['B' + str(row_ddt+1)].fill = table_fill ws['B' + str(detailed_start_row_number + 1)].fill = table_fill
ws['B' + str(row_ddt+1)].font = name_font ws['B' + str(detailed_start_row_number + 1)].font = name_font
ws['B' + str(row_ddt+1)].alignment = c_c_alignment ws['B' + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws['B' + str(row_ddt+1)] = "时间" ws['B' + str(detailed_start_row_number + 1)] = "时间"
ws['B' + str(row_ddt+1)].border = f_border ws['B' + str(detailed_start_row_number + 1)].border = f_border
for i in range(0, ca_len): for i in range(0, ca_len):
col = chr(ord('C') + i) col = chr(ord('C') + i)
ws[col + str(row_ddt+1)].font = name_font ws[col + str(detailed_start_row_number + 1)].font = name_font
ws[col + str(row_ddt+1)].alignment = c_c_alignment ws[col + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws[col + str(row_ddt+1)] = names[i] + " - (" + reporting_period_data['units'][i] + ")" ws[col + str(detailed_start_row_number + 1)] = names[i] + " - (" + reporting_period_data['units'][i] + ")"
ws[col + str(row_ddt+1)].border = f_border ws[col + str(detailed_start_row_number + 1)].border = f_border
# the detailed table_date # the detailed table_date
for i in range(0, time_len): for i in range(0, time_len):
rows = i + row_ddt + 2 rows = i + detailed_start_row_number + 2
ws['B' + str(rows)].font = name_font ws['B' + str(rows)].font = name_font
ws['B' + str(rows)].alignment = c_c_alignment ws['B' + str(rows)].alignment = c_c_alignment
@ -375,7 +366,7 @@ def generate_excel(report,
ws[col + str(rows)].border = f_border ws[col + str(rows)].border = f_border
# 小计 # 小计
row_subtotals = row_ddt + 2 + time_len row_subtotals = detailed_start_row_number + 2 + time_len
ws['B' + str(row_subtotals)].font = name_font ws['B' + str(row_subtotals)].font = name_font
ws['B' + str(row_subtotals)].alignment = c_c_alignment ws['B' + str(row_subtotals)].alignment = c_c_alignment
ws['B' + str(row_subtotals)] = "小计" ws['B' + str(row_subtotals)] = "小计"
@ -390,29 +381,32 @@ def generate_excel(report,
ws[col + str(row_subtotals)].border = f_border ws[col + str(row_subtotals)].border = f_border
ws[col + str(row_subtotals)].number_format = '0.00' ws[col + str(row_subtotals)].number_format = '0.00'
# LineChart ########################################################
# third: LineChart
# LineChart requires data from the detailed data table in the Excel file
# so print the detailed data table first and then print LineChart
########################################################
for i in range(0, ca_len): for i in range(0, ca_len):
line = LineChart()
lc = LineChart() line.title = "报告期消耗" + " - " + names[i] + "(" + reporting_period_data['units'][i] + ")"
lc.title = "报告期消耗" + " - " + names[i] + "(" + reporting_period_data['units'][i] + ")" line.style = 10
lc.style = 10 line.height = 8.40 # cm 1.05*8 1.05cm = 30 pt
lc.height = 8.40 # cm 1.05*8 1.05cm = 30 pt line.width = 24
lc.width = 24 line.x_axis.majorTickMark = 'in'
lc.x_axis.majorTickMark = 'in' line.y_axis.majorTickMark = 'in'
lc.y_axis.majorTickMark = 'in' line.dLbls = DataLabelList()
lc.dLbls = DataLabelList() line.dLbls.dLblPos = 't'
lc.dLbls.dLblPos = 't' line.dLbls.showVal = True
lc.dLbls.showVal = True times = Reference(ws, min_col=2, min_row=detailed_start_row_number + 2,
times = Reference(ws, min_col=2, min_row=row_ddt + 2, max_row=detailed_start_row_number + 2 + time_len)
max_row=row_ddt + 2 + time_len) line_data = Reference(ws, min_col=3 + i, min_row=detailed_start_row_number + 1,
lc_data = Reference(ws, min_col=3 + i, min_row=row_ddt + 1, max_row=detailed_start_row_number + 1 + time_len)
max_row=row_ddt + 1 + time_len) line.add_data(line_data, titles_from_data=True)
lc.add_data(lc_data, titles_from_data=True) line.set_categories(times)
lc.set_categories(times) ser = line.series[0]
ser = lc.series[0]
ser.marker.symbol = "diamond" ser.marker.symbol = "diamond"
ser.marker.size = 5 ser.marker.size = 5
ws.add_chart(lc, 'B' + str(row_sat + 6 * i)) ws.add_chart(line, 'B' + str(analysis_end_row_number + 6 * i))
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)

View File

@ -5,6 +5,7 @@ from openpyxl.chart import (
LineChart, LineChart,
Reference, Reference,
) )
from openpyxl.chart.label import DataLabelList
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font from openpyxl.styles import PatternFill, Border, Side, Alignment, Font
from openpyxl.drawing.image import Image from openpyxl.drawing.image import Image
from openpyxl import Workbook from openpyxl import Workbook
@ -68,19 +69,16 @@ def generate_excel(report,
ws = wb.active ws = wb.active
# Row height # Row height
ws.row_dimensions[1].height = 121 ws.row_dimensions[1].height = 102
for i in range(2, 37 + 1): for i in range(2, 2000 + 1):
ws.row_dimensions[i].height = 30 ws.row_dimensions[i].height = 42
for i in range(38, 90 + 1):
ws.row_dimensions[i].height = 30
# Col width # Col width
ws.column_dimensions['A'].width = 1.5 ws.column_dimensions['A'].width = 1.5
ws.column_dimensions['B'].width = 20.0 ws.column_dimensions['B'].width = 25.0
for i in range(ord('C'), ord('I')): for i in range(ord('C'), ord('L')):
ws.column_dimensions[chr(i)].width = 15.0 ws.column_dimensions[chr(i)].width = 15.0
# Font # Font
@ -116,12 +114,6 @@ def generate_excel(report,
wrap_text=False, wrap_text=False,
shrink_to_fit=False, shrink_to_fit=False,
indent=0) indent=0)
# c_r_alignment = Alignment(vertical='bottom',
# horizontal='center',
# text_rotation=0,
# wrap_text=False,
# shrink_to_fit=False,
# indent=0)
# Img # Img
img = Image("excelexporters/myems.png") img = Image("excelexporters/myems.png")
@ -317,11 +309,8 @@ def generate_excel(report,
######################################################## ########################################################
# Second: 详细数据 # Second: 详细数据
# row_sat+1~ row_sat+1+row_lines+: line # a+1~ analysis_end_row_number+1+line_charts_row_number+: line
# row_ddt~ : the detailed data table # detailed_start_row_number~ : the detailed data table
# row_sat : the number of rows of the analysis table
# row_lines : the number of rows of all line charts
# row_ddt : The number of rows in which the detailed data table header resides
######################################################## ########################################################
has_timestamps_flag = True has_timestamps_flag = True
if "timestamps" not in reporting_period_data.keys() or \ if "timestamps" not in reporting_period_data.keys() or \
@ -336,29 +325,29 @@ def generate_excel(report,
ca_len = len(names) ca_len = len(names)
time_len = len(timestamps) time_len = len(timestamps)
# the detailed title # the detailed title
row_lines = 9 * ca_len line_charts_row_number = 6 * ca_len + 1
row_sat = 7 + 2 * ca_len analysis_end_row_number = 7 + 2 * ca_len
row_ddt = row_sat + row_lines + 2 detailed_start_row_number = analysis_end_row_number + line_charts_row_number + 2
ws['B' + str(row_ddt)].font = title_font ws['B' + str(detailed_start_row_number)].font = title_font
ws['B' + str(row_ddt)] = name + ' 详细数据' ws['B' + str(detailed_start_row_number)] = name + ' 详细数据'
# the detailed table_title # the detailed table_title
ws['B' + str(row_ddt+1)].fill = table_fill ws['B' + str(detailed_start_row_number + 1)].fill = table_fill
ws['B' + str(row_ddt+1)].font = name_font ws['B' + str(detailed_start_row_number + 1)].font = name_font
ws['B' + str(row_ddt+1)].alignment = c_c_alignment ws['B' + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws['B' + str(row_ddt+1)] = "时间" ws['B' + str(detailed_start_row_number + 1)] = "时间"
ws['B' + str(row_ddt+1)].border = f_border ws['B' + str(detailed_start_row_number + 1)].border = f_border
for i in range(0, ca_len): for i in range(0, ca_len):
col = chr(ord('C') + i) col = chr(ord('C') + i)
ws[col + str(row_ddt+1)].font = name_font ws[col + str(detailed_start_row_number + 1)].font = name_font
ws[col + str(row_ddt+1)].alignment = c_c_alignment ws[col + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws[col + str(row_ddt+1)] = names[i] + " - (" + reporting_period_data['units'][i] + ")" ws[col + str(detailed_start_row_number + 1)] = names[i] + " - (" + reporting_period_data['units'][i] + ")"
ws[col + str(row_ddt+1)].border = f_border ws[col + str(detailed_start_row_number + 1)].border = f_border
# the detailed table_date # the detailed table_date
for i in range(0, time_len): for i in range(0, time_len):
rows = i + row_ddt + 2 rows = i + detailed_start_row_number + 2
ws['B' + str(rows)].font = name_font ws['B' + str(rows)].font = name_font
ws['B' + str(rows)].alignment = c_c_alignment ws['B' + str(rows)].alignment = c_c_alignment
@ -375,7 +364,7 @@ def generate_excel(report,
ws[col + str(rows)].border = f_border ws[col + str(rows)].border = f_border
# 小计 # 小计
row_subtotals = row_ddt + 2 + time_len row_subtotals = detailed_start_row_number + 2 + time_len
ws['B' + str(row_subtotals)].font = name_font ws['B' + str(row_subtotals)].font = name_font
ws['B' + str(row_subtotals)].alignment = c_c_alignment ws['B' + str(row_subtotals)].alignment = c_c_alignment
ws['B' + str(row_subtotals)] = "小计" ws['B' + str(row_subtotals)] = "小计"
@ -390,26 +379,32 @@ def generate_excel(report,
ws[col + str(row_subtotals)].border = f_border ws[col + str(row_subtotals)].border = f_border
ws[col + str(row_subtotals)].number_format = '0.00' ws[col + str(row_subtotals)].number_format = '0.00'
# LineChart ########################################################
# third: LineChart
# LineChart requires data from the detailed data table in the Excel file
# so print the detailed data table first and then print LineChart
########################################################
for i in range(0, ca_len): for i in range(0, ca_len):
line = LineChart()
lc = LineChart() line.title = "报告期消耗" + " - " + names[i] + "(" + reporting_period_data['units'][i] + ")"
lc.title = "报告期消耗" + " - " + names[i] + "(" + reporting_period_data['units'][i] + ")" line.style = 10
lc.style = 10 line.height = 8.40 # cm 1.05*8 1.05cm = 30 pt
lc.height = 8.40 # cm 1.05*8 1.05cm = 30 pt line.width = 24
lc.width = 23.28 line.x_axis.majorTickMark = 'in'
lc.x_axis.majorTickMark = 'in' line.y_axis.majorTickMark = 'in'
lc.y_axis.majorTickMark = 'in' line.dLbls = DataLabelList()
times = Reference(ws, min_col=2, min_row=row_ddt + 2, line.dLbls.dLblPos = 't'
max_row=row_ddt + 2 + time_len) line.dLbls.showVal = True
lc_data = Reference(ws, min_col=3 + i, min_row=row_ddt + 1, times = Reference(ws, min_col=2, min_row=detailed_start_row_number + 2,
max_row=row_ddt + 1 + time_len) max_row=detailed_start_row_number + 2 + time_len)
lc.add_data(lc_data, titles_from_data=True) line_data = Reference(ws, min_col=3 + i, min_row=detailed_start_row_number + 1,
lc.set_categories(times) max_row=detailed_start_row_number + 1 + time_len)
ser = lc.series[0] line.add_data(line_data, titles_from_data=True)
line.set_categories(times)
ser = line.series[0]
ser.marker.symbol = "diamond" ser.marker.symbol = "diamond"
ser.marker.size = 5 ser.marker.size = 5
ws.add_chart(lc, 'B' + str(row_sat + 2 + 9 * i)) ws.add_chart(line, 'B' + str(analysis_end_row_number + 2 + 6 * i))
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)

View File

@ -113,12 +113,6 @@ def generate_excel(report,
wrap_text=False, wrap_text=False,
shrink_to_fit=False, shrink_to_fit=False,
indent=0) indent=0)
# c_r_alignment = Alignment(vertical='bottom',
# horizontal='center',
# text_rotation=0,
# wrap_text=False,
# shrink_to_fit=False,
# indent=0)
# Img # Img
img = Image("excelexporters/myems.png") img = Image("excelexporters/myems.png")
@ -264,40 +258,40 @@ def generate_excel(report,
# Second: 报告期单位面积消耗 # Second: 报告期单位面积消耗
# 9 + ca_len * 2: title # 9 + ca_len * 2: title
# 10 + ca_len * 2: table title # 10 + ca_len * 2: table title
# row_title + 2 ~ row_title + 2 + ca_len : table_data # per_unit_area_start_row_number + 2 ~ per_unit_area_start_row_number + 2 + ca_len : table_data
################################################# #################################################
if has_energy_data_flag: if has_energy_data_flag:
names = reporting_period_data['names'] names = reporting_period_data['names']
ca_len = len(names) ca_len = len(names)
row_title = 9 + ca_len * 2 per_unit_area_start_row_number = 9 + ca_len * 2
ws['B' + str(row_title)].font = title_font ws['B' + str(per_unit_area_start_row_number)].font = title_font
ws['B' + str(row_title)] = name + ' 单位面积值' + str(report['shopfloor']['area']) + '' ws['B' + str(per_unit_area_start_row_number)] = name + ' 单位面积值' + str(report['shopfloor']['area']) + ''
category = reporting_period_data['names'] category = reporting_period_data['names']
# table_title # table_title
ws['B' + str(row_title + 1)].fill = table_fill ws['B' + str(per_unit_area_start_row_number + 1)].fill = table_fill
ws['B' + str(row_title + 1)].font = title_font ws['B' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['B' + str(row_title + 1)].alignment = c_c_alignment ws['B' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['B' + str(row_title + 1)] = '报告期' ws['B' + str(per_unit_area_start_row_number + 1)] = '报告期'
ws['B' + str(row_title + 1)].border = f_border ws['B' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['C' + str(row_title + 1)].font = title_font ws['C' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['C' + str(row_title + 1)].alignment = c_c_alignment ws['C' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['C' + str(row_title + 1)] = '平均负荷' ws['C' + str(per_unit_area_start_row_number + 1)] = '平均负荷'
ws['C' + str(row_title + 1)].border = f_border ws['C' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['D' + str(row_title + 1)].font = title_font ws['D' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['D' + str(row_title + 1)].alignment = c_c_alignment ws['D' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['D' + str(row_title + 1)] = '最大负荷' ws['D' + str(per_unit_area_start_row_number + 1)] = '最大负荷'
ws['D' + str(row_title + 1)].border = f_border ws['D' + str(per_unit_area_start_row_number + 1)].border = f_border
# table_data # table_data
for i, value in enumerate(category): for i, value in enumerate(category):
row_data = row_title + 2 + i row_data = per_unit_area_start_row_number + 2 + i
ws['B' + str(row_data)].font = name_font ws['B' + str(row_data)].font = name_font
ws['B' + str(row_data)].alignment = c_c_alignment ws['B' + str(row_data)].alignment = c_c_alignment
ws['B' + str(row_data)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][ ws['B' + str(row_data)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][
@ -322,9 +316,9 @@ def generate_excel(report,
######################################################## ########################################################
# Third: 详细数据 # Third: 详细数据
# row_sat~ row_sat + 6*cal_len: line # analysis_end_row_number~ analysis_end_row_number + 6*cal_len: line
# row_da: table title # detailed_start_row_number: table title
# row_da + 1~: table_data # detailed_start_row_number + 1~: table_data
######################################################## ########################################################
has_timestamps_flag = True has_timestamps_flag = True
if "timestamps" not in reporting_period_data.keys() or \ if "timestamps" not in reporting_period_data.keys() or \
@ -337,40 +331,37 @@ def generate_excel(report,
names = reporting_period_data['names'] names = reporting_period_data['names']
ca_len = len(names) ca_len = len(names)
time_len = len(timestamps) time_len = len(timestamps)
# row_lines == the number of rows of lines line_charts_row_number = 6 * ca_len
row_lines = 6 * ca_len analysis_end_row_number = 12 + 3 * ca_len
# row_sat == the number of rows of statistical analysis table detailed_start_row_number = analysis_end_row_number + line_charts_row_number + 1
row_sat = 12 + 3 * ca_len
# row_da == the number of rows of Detailed data
row_da = row_sat + row_lines + 1
ws['B' + str(row_da)].font = title_font ws['B' + str(detailed_start_row_number)].font = title_font
ws['B' + str(row_da)] = name + ' 详细数据' ws['B' + str(detailed_start_row_number)] = name + ' 详细数据'
# table_title # table_title
ws['B' + str(row_da + 1)].fill = table_fill ws['B' + str(detailed_start_row_number + 1)].fill = table_fill
ws['B' + str(row_da + 1)].font = name_font ws['B' + str(detailed_start_row_number + 1)].font = name_font
ws['B' + str(row_da + 1)].alignment = c_c_alignment ws['B' + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws['B' + str(row_da + 1)] = "日期时间" ws['B' + str(detailed_start_row_number + 1)] = "日期时间"
ws['B' + str(row_da + 1)].border = f_border ws['B' + str(detailed_start_row_number + 1)].border = f_border
for i in range(0, ca_len): for i in range(0, ca_len):
col_average = chr(ord('C') + 2 * i) col_average = chr(ord('C') + 2 * i)
col_maximum = chr(ord('D') + 2 * i) col_maximum = chr(ord('D') + 2 * i)
ws[col_average + str(row_da + 1)].font = name_font ws[col_average + str(detailed_start_row_number + 1)].font = name_font
ws[col_average + str(row_da + 1)].alignment = c_c_alignment ws[col_average + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws[col_average + str(row_da + 1)] = names[i] + " 平均负荷(" + reporting_period_data['units'][ ws[col_average + str(detailed_start_row_number + 1)] = names[i] + " 平均负荷(" + reporting_period_data['units'][
i] + "/H)" i] + "/H)"
ws[col_average + str(row_da + 1)].border = f_border ws[col_average + str(detailed_start_row_number + 1)].border = f_border
ws[col_maximum + str(row_da + 1)].font = name_font ws[col_maximum + str(detailed_start_row_number + 1)].font = name_font
ws[col_maximum + str(row_da + 1)].alignment = c_c_alignment ws[col_maximum + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws[col_maximum + str(row_da + 1)] = names[i] + " 最大负荷(" + reporting_period_data['units'][ ws[col_maximum + str(detailed_start_row_number + 1)] = names[i] + " 最大负荷(" + reporting_period_data['units'][
i] + "/H)" i] + "/H)"
ws[col_maximum + str(row_da + 1)].border = f_border ws[col_maximum + str(detailed_start_row_number + 1)].border = f_border
# table_date # table_date
for i in range(0, time_len): for i in range(0, time_len):
rows = i + row_da + 2 rows = i + detailed_start_row_number + 2
ws['B' + str(rows)].font = name_font ws['B' + str(rows)].font = name_font
ws['B' + str(rows)].alignment = c_c_alignment ws['B' + str(rows)].alignment = c_c_alignment
@ -395,32 +386,36 @@ def generate_excel(report,
ws[col_maximum + str(rows)].number_format = '0.00' ws[col_maximum + str(rows)].number_format = '0.00'
ws[col_maximum + str(rows)].border = f_border ws[col_maximum + str(rows)].border = f_border
# LineChart ########################################################
# third: LineChart
# LineChart requires data from the detailed data table in the Excel file
# so print the detailed data table first and then print LineChart
########################################################
for i in range(0, ca_len): for i in range(0, ca_len):
lc = LineChart() line = LineChart()
lc.title = "报告期 最大负荷" line.title = "报告期 最大负荷 - " + names[i] + "(" + reporting_period_data['units'][i] + ")"
lc.style = 10 line.style = 10
lc.x_axis.majorTickMark = 'in' line.x_axis.majorTickMark = 'in'
lc.y_axis.majorTickMark = 'in' line.y_axis.majorTickMark = 'in'
lc.smooth = True line.smooth = True
lc.x_axis.crosses = 'min' line.x_axis.crosses = 'min'
lc.height = 8.25 line.height = 8.25
lc.width = 24 line.width = 24
lc.dLbls = DataLabelList() line.dLbls = DataLabelList()
lc.dLbls.dLblPos = 't' line.dLbls.dLblPos = 't'
lc.dLbls.showVal = True line.dLbls.showVal = True
times = Reference(ws, min_col=2, min_row=row_da + 2, times = Reference(ws, min_col=2, min_row=detailed_start_row_number + 2,
max_row=row_da + 2 + time_len) max_row=detailed_start_row_number + 2 + time_len)
lc_data = Reference(ws, min_col=2 + 2 * (i+1), min_row=row_da + 1, line_data = Reference(ws, min_col=2 + 2 * (i + 1), min_row=detailed_start_row_number + 1,
max_row=row_da + 1 + time_len) max_row=detailed_start_row_number + 1 + time_len)
lc.add_data(lc_data, titles_from_data=True) line.add_data(line_data, titles_from_data=True)
lc.set_categories(times) line.set_categories(times)
ser = lc.series[0] ser = line.series[0]
ser.marker.symbol = "diamond" ser.marker.symbol = "diamond"
ser.marker.size = 5 ser.marker.size = 5
chart_col = 'B' chart_col = 'B'
chart_cell = str(row_sat + 6 * i) chart_cell = str(analysis_end_row_number + 6 * i)
ws.add_chart(lc, chart_col + chart_cell) ws.add_chart(line, chart_col + chart_cell)
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)

View File

@ -5,6 +5,7 @@ from openpyxl.chart import (
LineChart, LineChart,
Reference, Reference,
) )
from openpyxl.chart.label import DataLabelList
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font from openpyxl.styles import PatternFill, Border, Side, Alignment, Font
from openpyxl.drawing.image import Image from openpyxl.drawing.image import Image
from openpyxl import Workbook from openpyxl import Workbook
@ -68,19 +69,16 @@ def generate_excel(report,
ws = wb.active ws = wb.active
# Row height # Row height
ws.row_dimensions[1].height = 121 ws.row_dimensions[1].height = 102
for i in range(2, 37 + 1): for i in range(2, 2000 + 1):
ws.row_dimensions[i].height = 30 ws.row_dimensions[i].height = 42
for i in range(38, 90 + 1):
ws.row_dimensions[i].height = 30
# Col width # Col width
ws.column_dimensions['A'].width = 1.5 ws.column_dimensions['A'].width = 1.5
ws.column_dimensions['B'].width = 20.0 ws.column_dimensions['B'].width = 25.0
for i in range(ord('C'), ord('I')): for i in range(ord('C'), ord('L')):
ws.column_dimensions[chr(i)].width = 15.0 ws.column_dimensions[chr(i)].width = 15.0
# Font # Font
@ -116,12 +114,6 @@ def generate_excel(report,
wrap_text=False, wrap_text=False,
shrink_to_fit=False, shrink_to_fit=False,
indent=0) indent=0)
# c_r_alignment = Alignment(vertical='bottom',
# horizontal='center',
# text_rotation=0,
# wrap_text=False,
# shrink_to_fit=False,
# indent=0)
# Img # Img
img = Image("excelexporters/myems.png") img = Image("excelexporters/myems.png")
@ -318,63 +310,61 @@ def generate_excel(report,
# Second: 报告期消耗 # Second: 报告期消耗
# 9 + ca_len * 2: title # 9 + ca_len * 2: title
# 10 + ca_len * 2: table title # 10 + ca_len * 2: table title
# row_title + 2 ~ row_title + 2 + ca_len : table_data # per_unit_area_start_row_number + 2 ~ per_unit_area_start_row_number + 2 + ca_len : table_data
################################################# #################################################
if has_energy_data_flag: if has_energy_data_flag:
names = reporting_period_data['names'] names = reporting_period_data['names']
ca_len = len(names) ca_len = len(names)
row_title = 9 + ca_len * 2 per_unit_area_start_row_number = 9 + ca_len * 2
ws['B' + str(row_title)].font = title_font ws['B' + str(per_unit_area_start_row_number)].font = title_font
ws['B' + str(row_title)] = name + ' 单位面积值' ws['B' + str(per_unit_area_start_row_number)] = name + ' 单位面积值' + str(report['shopfloor']['area']) + ''
ws['D' + str(row_title)].font = title_font
ws['D' + str(row_title)] = str(report['shopfloor']['area']) + ''
category = reporting_period_data['names'] category = reporting_period_data['names']
# table_title # table_title
ws['B' + str(row_title + 1)].fill = table_fill ws['B' + str(per_unit_area_start_row_number + 1)].fill = table_fill
ws['B' + str(row_title + 1)].font = title_font ws['B' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['B' + str(row_title + 1)].alignment = c_c_alignment ws['B' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['B' + str(row_title + 1)] = '报告期' ws['B' + str(per_unit_area_start_row_number + 1)] = '报告期'
ws['B' + str(row_title + 1)].border = f_border ws['B' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['C' + str(row_title + 1)].font = title_font ws['C' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['C' + str(row_title + 1)].alignment = c_c_alignment ws['C' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['C' + str(row_title + 1)] = '算术平均数' ws['C' + str(per_unit_area_start_row_number + 1)] = '算术平均数'
ws['C' + str(row_title + 1)].border = f_border ws['C' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['D' + str(row_title + 1)].font = title_font ws['D' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['D' + str(row_title + 1)].alignment = c_c_alignment ws['D' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['D' + str(row_title + 1)] = '中位数' ws['D' + str(per_unit_area_start_row_number + 1)] = '中位数'
ws['D' + str(row_title + 1)].border = f_border ws['D' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['E' + str(row_title + 1)].font = title_font ws['E' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['E' + str(row_title + 1)].alignment = c_c_alignment ws['E' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['E' + str(row_title + 1)] = '最小值' ws['E' + str(per_unit_area_start_row_number + 1)] = '最小值'
ws['E' + str(row_title + 1)].border = f_border ws['E' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['F' + str(row_title + 1)].font = title_font ws['F' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['F' + str(row_title + 1)].alignment = c_c_alignment ws['F' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['F' + str(row_title + 1)] = '最大值' ws['F' + str(per_unit_area_start_row_number + 1)] = '最大值'
ws['F' + str(row_title + 1)].border = f_border ws['F' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['G' + str(row_title + 1)].font = title_font ws['G' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['G' + str(row_title + 1)].alignment = c_c_alignment ws['G' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['G' + str(row_title + 1)] = '样本标准差' ws['G' + str(per_unit_area_start_row_number + 1)] = '样本标准差'
ws['G' + str(row_title + 1)].border = f_border ws['G' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['H' + str(row_title + 1)].font = title_font ws['H' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['H' + str(row_title + 1)].alignment = c_c_alignment ws['H' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['H' + str(row_title + 1)] = '样本方差' ws['H' + str(per_unit_area_start_row_number + 1)] = '样本方差'
ws['H' + str(row_title + 1)].border = f_border ws['H' + str(per_unit_area_start_row_number + 1)].border = f_border
# table_data # table_data
for i, value in enumerate(category): for i, value in enumerate(category):
row_data = row_title + 2 + i row_data = per_unit_area_start_row_number + 2 + i
ws['B' + str(row_data)].font = name_font ws['B' + str(row_data)].font = name_font
ws['B' + str(row_data)].alignment = c_c_alignment ws['B' + str(row_data)].alignment = c_c_alignment
ws['B' + str(row_data)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][ ws['B' + str(row_data)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][
@ -431,9 +421,9 @@ def generate_excel(report,
######################################################## ########################################################
# Third: 详细数据 # Third: 详细数据
# row_sat+row_title~ row_sat+row_title+time_len: line # detailed_start_row_number~ detailed_start_row_number+time_len: line
# row_sat+1+row_title: table title # detailed_start_row_number+1: table title
# i + row_sat + 2 + 9 * ca_len~: table_data # i + analysis_end_row_number + 2 + 6 * ca_len~: table_data
######################################################## ########################################################
has_timestamps_flag = True has_timestamps_flag = True
if "timestamps" not in reporting_period_data.keys() or \ if "timestamps" not in reporting_period_data.keys() or \
@ -448,29 +438,29 @@ def generate_excel(report,
ca_len = len(names) ca_len = len(names)
time_len = len(timestamps) time_len = len(timestamps)
# title # title
row_title = 9 * ca_len line_charts_row_number = 6 * ca_len
# row_st == row_statistical analysis table analysis_end_row_number = 12 + 3 * ca_len
row_sat = 12 + 3 * ca_len detailed_start_row_number = analysis_end_row_number + line_charts_row_number + 1
ws['B' + str(row_sat+row_title)].font = title_font ws['B' + str(detailed_start_row_number)].font = title_font
ws['B' + str(row_sat+row_title)] = name + ' 详细数据' ws['B' + str(detailed_start_row_number)] = name + ' 详细数据'
# table_title # table_title
ws['B' + str(row_sat+1+row_title)].fill = table_fill ws['B' + str(detailed_start_row_number+1)].fill = table_fill
ws['B' + str(row_sat+1+row_title)].font = name_font ws['B' + str(detailed_start_row_number+1)].font = name_font
ws['B' + str(row_sat+1+row_title)].alignment = c_c_alignment ws['B' + str(detailed_start_row_number+1)].alignment = c_c_alignment
ws['B' + str(row_sat+1+row_title)] = "时间" ws['B' + str(detailed_start_row_number+1)] = "时间"
ws['B' + str(row_sat+1+row_title)].border = f_border ws['B' + str(detailed_start_row_number+1)].border = f_border
for i in range(0, ca_len): for i in range(0, ca_len):
col = chr(ord('C') + i) col = chr(ord('C') + i)
ws[col + str(row_sat+1+row_title)].font = name_font ws[col + str(detailed_start_row_number+1)].font = name_font
ws[col + str(row_sat+1+row_title)].alignment = c_c_alignment ws[col + str(detailed_start_row_number+1)].alignment = c_c_alignment
ws[col + str(row_sat+1+row_title)] = names[i] + " - (" + reporting_period_data['units'][i] + ")" ws[col + str(detailed_start_row_number+1)] = names[i] + " - (" + reporting_period_data['units'][i] + ")"
ws[col + str(row_sat+1+row_title)].border = f_border ws[col + str(detailed_start_row_number+1)].border = f_border
# table_date # table_date
for i in range(0, time_len): for i in range(0, time_len):
rows = i + row_sat + 2 + 9 * ca_len rows = i + detailed_start_row_number + 2
ws['B' + str(rows)].font = name_font ws['B' + str(rows)].font = name_font
ws['B' + str(rows)].alignment = c_c_alignment ws['B' + str(rows)].alignment = c_c_alignment
@ -487,7 +477,7 @@ def generate_excel(report,
ws[col + str(rows)].border = f_border ws[col + str(rows)].border = f_border
# 小计 # 小计
row_subtotals = row_sat + 2 + time_len + 9 * ca_len row_subtotals = detailed_start_row_number + 2 + time_len
ws['B' + str(row_subtotals)].font = name_font ws['B' + str(row_subtotals)].font = name_font
ws['B' + str(row_subtotals)].alignment = c_c_alignment ws['B' + str(row_subtotals)].alignment = c_c_alignment
ws['B' + str(row_subtotals)] = "小计" ws['B' + str(row_subtotals)] = "小计"
@ -502,26 +492,36 @@ def generate_excel(report,
ws[col + str(row_subtotals)].border = f_border ws[col + str(row_subtotals)].border = f_border
ws[col + str(row_subtotals)].number_format = '0.00' ws[col + str(row_subtotals)].number_format = '0.00'
# LineChart ########################################################
# third: LineChart
# LineChart requires data from the detailed data table in the Excel file
# so print the detailed data table first and then print LineChart
########################################################
for i in range(0, ca_len): for i in range(0, ca_len):
lc = LineChart() line = LineChart()
lc.title = "报告期消耗" + " - " + names[i] + "(" + reporting_period_data['units'][i] + ")" line.title = "报告期消耗" + " - " + names[i] + "(" + reporting_period_data['units'][i] + ")"
lc.style = 10 line.style = 10
lc.height = 8.40 # cm 1.05*8 1.05cm = 30 pt line.x_axis.majorTickMark = 'in'
lc.width = 31 line.y_axis.majorTickMark = 'in'
lc.x_axis.majorTickMark = 'in' line.smooth = True
lc.y_axis.majorTickMark = 'in' line.x_axis.crosses = 'min'
times = Reference(ws, min_col=2, min_row=row_sat + 2 + row_title, line.height = 8.25
max_row=row_sat + 2 + row_title + time_len) line.width = 24
lc_data = Reference(ws, min_col=3 + i, min_row=row_sat + 1 + row_title, line.dLbls = DataLabelList()
max_row=row_sat + 1 + row_title + time_len) line.dLbls.dLblPos = 't'
lc.add_data(lc_data, titles_from_data=True) line.dLbls.showVal = True
lc.set_categories(times) times = Reference(ws, min_col=2, min_row=detailed_start_row_number + 2,
ser = lc.series[0] max_row=detailed_start_row_number + 2 + time_len)
line_data = Reference(ws, min_col=3 + i, min_row=detailed_start_row_number + 1,
max_row=detailed_start_row_number + 1 + time_len)
line.add_data(line_data, titles_from_data=True)
line.set_categories(times)
ser = line.series[0]
ser.marker.symbol = "diamond" ser.marker.symbol = "diamond"
ser.marker.size = 5 chart_col = 'B'
ws.add_chart(lc, 'B' + str(row_sat + 9 * i)) chart_cell = str(analysis_end_row_number + 6 * i)
ws.add_chart(line, chart_col + chart_cell)
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)

View File

@ -5,6 +5,7 @@ from openpyxl.chart import (
LineChart, LineChart,
Reference, Reference,
) )
from openpyxl.chart.label import DataLabelList
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font from openpyxl.styles import PatternFill, Border, Side, Alignment, Font
from openpyxl.drawing.image import Image from openpyxl.drawing.image import Image
from openpyxl import Workbook from openpyxl import Workbook
@ -68,19 +69,16 @@ def generate_excel(report,
ws = wb.active ws = wb.active
# Row height # Row height
ws.row_dimensions[1].height = 121 ws.row_dimensions[1].height = 102
for i in range(2, 37 + 1): for i in range(2, 2000 + 1):
ws.row_dimensions[i].height = 30 ws.row_dimensions[i].height = 42
for i in range(38, 90 + 1):
ws.row_dimensions[i].height = 30
# Col width # Col width
ws.column_dimensions['A'].width = 1.5 ws.column_dimensions['A'].width = 1.5
ws.column_dimensions['B'].width = 20.0 ws.column_dimensions['B'].width = 25.0
for i in range(ord('C'), ord('I')): for i in range(ord('C'), ord('L')):
ws.column_dimensions[chr(i)].width = 15.0 ws.column_dimensions[chr(i)].width = 15.0
# Font # Font
@ -116,12 +114,6 @@ def generate_excel(report,
wrap_text=False, wrap_text=False,
shrink_to_fit=False, shrink_to_fit=False,
indent=0) indent=0)
# c_r_alignment = Alignment(vertical='bottom',
# horizontal='center',
# text_rotation=0,
# wrap_text=False,
# shrink_to_fit=False,
# indent=0)
# Img # Img
img = Image("excelexporters/myems.png") img = Image("excelexporters/myems.png")
@ -320,63 +312,63 @@ def generate_excel(report,
# Second: 报告期消耗 # Second: 报告期消耗
# 9 + ca_len * 2: title # 9 + ca_len * 2: title
# 10 + ca_len * 2: table title # 10 + ca_len * 2: table title
# row_title + 2 ~ row_title + 2 + ca_len : table_data # per_unit_area_start_row_number + 2 ~ per_unit_area_start_row_number + 2 + ca_len : table_data
################################################# #################################################
if has_energy_data_flag: if has_energy_data_flag:
names = reporting_period_data['names'] names = reporting_period_data['names']
ca_len = len(names) ca_len = len(names)
row_title = 9 + ca_len * 2 per_unit_area_start_row_number = 9 + ca_len * 2
ws['B' + str(row_title)].font = title_font ws['B' + str(per_unit_area_start_row_number)].font = title_font
ws['B' + str(row_title)] = name + ' 单位面积值' ws['B' + str(per_unit_area_start_row_number)] = name + ' 单位面积值'
ws['D' + str(row_title)].font = title_font ws['D' + str(per_unit_area_start_row_number)].font = title_font
ws['D' + str(row_title)] = str(report['space']['area']) + '' ws['D' + str(per_unit_area_start_row_number)] = str(report['space']['area']) + ''
category = reporting_period_data['names'] category = reporting_period_data['names']
# table_title # table_title
ws['B' + str(row_title + 1)].fill = table_fill ws['B' + str(per_unit_area_start_row_number + 1)].fill = table_fill
ws['B' + str(row_title + 1)].font = title_font ws['B' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['B' + str(row_title + 1)].alignment = c_c_alignment ws['B' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['B' + str(row_title + 1)] = '报告期' ws['B' + str(per_unit_area_start_row_number + 1)] = '报告期'
ws['B' + str(row_title + 1)].border = f_border ws['B' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['C' + str(row_title + 1)].font = title_font ws['C' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['C' + str(row_title + 1)].alignment = c_c_alignment ws['C' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['C' + str(row_title + 1)] = '算术平均数' ws['C' + str(per_unit_area_start_row_number + 1)] = '算术平均数'
ws['C' + str(row_title + 1)].border = f_border ws['C' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['D' + str(row_title + 1)].font = title_font ws['D' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['D' + str(row_title + 1)].alignment = c_c_alignment ws['D' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['D' + str(row_title + 1)] = '中位数' ws['D' + str(per_unit_area_start_row_number + 1)] = '中位数'
ws['D' + str(row_title + 1)].border = f_border ws['D' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['E' + str(row_title + 1)].font = title_font ws['E' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['E' + str(row_title + 1)].alignment = c_c_alignment ws['E' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['E' + str(row_title + 1)] = '最小值' ws['E' + str(per_unit_area_start_row_number + 1)] = '最小值'
ws['E' + str(row_title + 1)].border = f_border ws['E' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['F' + str(row_title + 1)].font = title_font ws['F' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['F' + str(row_title + 1)].alignment = c_c_alignment ws['F' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['F' + str(row_title + 1)] = '最大值' ws['F' + str(per_unit_area_start_row_number + 1)] = '最大值'
ws['F' + str(row_title + 1)].border = f_border ws['F' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['G' + str(row_title + 1)].font = title_font ws['G' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['G' + str(row_title + 1)].alignment = c_c_alignment ws['G' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['G' + str(row_title + 1)] = '样本标准差' ws['G' + str(per_unit_area_start_row_number + 1)] = '样本标准差'
ws['G' + str(row_title + 1)].border = f_border ws['G' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['H' + str(row_title + 1)].font = title_font ws['H' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['H' + str(row_title + 1)].alignment = c_c_alignment ws['H' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['H' + str(row_title + 1)] = '样本方差' ws['H' + str(per_unit_area_start_row_number + 1)] = '样本方差'
ws['H' + str(row_title + 1)].border = f_border ws['H' + str(per_unit_area_start_row_number + 1)].border = f_border
# table_data # table_data
for i, value in enumerate(category): for i, value in enumerate(category):
row_data = row_title + 2 + i row_data = per_unit_area_start_row_number + 2 + i
ws['B' + str(row_data)].font = name_font ws['B' + str(row_data)].font = name_font
ws['B' + str(row_data)].alignment = c_c_alignment ws['B' + str(row_data)].alignment = c_c_alignment
ws['B' + str(row_data)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][ ws['B' + str(row_data)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][
@ -433,9 +425,9 @@ def generate_excel(report,
######################################################## ########################################################
# Third: 详细数据 # Third: 详细数据
# row_sat+row_title~ row_sat+row_title+time_len: line # analysis_end_row_number~ analysis_end_row_number+time_len: line
# row_sat+1+row_title: table title # analysis_end_row_number+1+line_charts_row_number: table title
# i + row_sat + 2 + 10 * ca_len~: table_data # i + analysis_end_row_number + 2 + 10 * ca_len~: table_data
######################################################## ########################################################
has_timestamps_flag = True has_timestamps_flag = True
if "timestamps" not in reporting_period_data.keys() or \ if "timestamps" not in reporting_period_data.keys() or \
@ -450,29 +442,29 @@ def generate_excel(report,
ca_len = len(names) ca_len = len(names)
time_len = len(timestamps) time_len = len(timestamps)
# title # title
row_title = 10 * ca_len line_charts_row_number = 6 * ca_len
# row_st == row_statistical analysis table analysis_end_row_number = 12 + 3 * ca_len
row_sat = 12 + 3 * ca_len detailed_start_row_number = analysis_end_row_number + line_charts_row_number + 1
ws['B' + str(row_sat+row_title)].font = title_font ws['B' + str(detailed_start_row_number)].font = title_font
ws['B' + str(row_sat+row_title)] = name + ' 详细数据' ws['B' + str(detailed_start_row_number)] = name + ' 详细数据'
# table_title # table_title
ws['B' + str(row_sat+1+row_title)].fill = table_fill ws['B' + str(detailed_start_row_number + 1)].fill = table_fill
ws['B' + str(row_sat+1+row_title)].font = name_font ws['B' + str(detailed_start_row_number + 1)].font = name_font
ws['B' + str(row_sat+1+row_title)].alignment = c_c_alignment ws['B' + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws['B' + str(row_sat+1+row_title)] = "时间" ws['B' + str(detailed_start_row_number + 1)] = "时间"
ws['B' + str(row_sat+1+row_title)].border = f_border ws['B' + str(detailed_start_row_number + 1)].border = f_border
for i in range(0, ca_len): for i in range(0, ca_len):
col = chr(ord('C') + i) col = chr(ord('C') + i)
ws[col + str(row_sat+1+row_title)].font = name_font ws[col + str(detailed_start_row_number + 1)].font = name_font
ws[col + str(row_sat+1+row_title)].alignment = c_c_alignment ws[col + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws[col + str(row_sat+1+row_title)] = names[i] + " - (" + reporting_period_data['units'][i] + ")" ws[col + str(detailed_start_row_number + 1)] = names[i] + " - (" + reporting_period_data['units'][i] + ")"
ws[col + str(row_sat+1+row_title)].border = f_border ws[col + str(detailed_start_row_number + 1)].border = f_border
# table_date # table_date
for i in range(0, time_len): for i in range(0, time_len):
rows = i + row_sat + 2 + 10 * ca_len rows = i + detailed_start_row_number + 2
ws['B' + str(rows)].font = name_font ws['B' + str(rows)].font = name_font
ws['B' + str(rows)].alignment = c_c_alignment ws['B' + str(rows)].alignment = c_c_alignment
@ -489,7 +481,7 @@ def generate_excel(report,
ws[col + str(rows)].border = f_border ws[col + str(rows)].border = f_border
# 小计 # 小计
row_subtotals = row_sat + 2 + time_len + 10 * ca_len row_subtotals = detailed_start_row_number + 2 + time_len
ws['B' + str(row_subtotals)].font = name_font ws['B' + str(row_subtotals)].font = name_font
ws['B' + str(row_subtotals)].alignment = c_c_alignment ws['B' + str(row_subtotals)].alignment = c_c_alignment
ws['B' + str(row_subtotals)] = "小计" ws['B' + str(row_subtotals)] = "小计"
@ -504,26 +496,35 @@ def generate_excel(report,
ws[col + str(row_subtotals)].border = f_border ws[col + str(row_subtotals)].border = f_border
ws[col + str(row_subtotals)].number_format = '0.00' ws[col + str(row_subtotals)].number_format = '0.00'
# LineChart ########################################################
# third: LineChart
# LineChart requires data from the detailed data table in the Excel file
# so print the detailed data table first and then print LineChart
########################################################
for i in range(0, ca_len): for i in range(0, ca_len):
lc = LineChart() line = LineChart()
lc.title = "报告期消耗" + " - " + names[i] + "(" + reporting_period_data['units'][i] + ")" line.title = "报告期消耗" + " - " + names[i] + "(" + reporting_period_data['units'][i] + ")"
lc.style = 10 line.style = 10
lc.height = 8.40 # cm 1.05*8 1.05cm = 30 pt line.x_axis.majorTickMark = 'in'
lc.width = 31 line.y_axis.majorTickMark = 'in'
lc.x_axis.majorTickMark = 'in' line.smooth = True
lc.y_axis.majorTickMark = 'in' line.x_axis.crosses = 'min'
times = Reference(ws, min_col=2, min_row=row_sat + 2 + row_title, line.height = 8.25
max_row=row_sat + 2 + row_title + time_len) line.width = 24
lc_data = Reference(ws, min_col=3 + i, min_row=row_sat + 1 + row_title, line.dLbls = DataLabelList()
max_row=row_sat + 1 + row_title + time_len) line.dLbls.dLblPos = 't'
lc.add_data(lc_data, titles_from_data=True) line.dLbls.showVal = True
lc.set_categories(times) times = Reference(ws, min_col=2, min_row=detailed_start_row_number + 2,
ser = lc.series[0] max_row=detailed_start_row_number + 2 + time_len)
line_data = Reference(ws, min_col=3 + i, min_row=detailed_start_row_number + 1,
max_row=detailed_start_row_number + 1 + time_len)
line.add_data(line_data, titles_from_data=True)
line.set_categories(times)
ser = line.series[0]
ser.marker.symbol = "diamond" ser.marker.symbol = "diamond"
ser.marker.size = 5 ser.marker.size = 5
ws.add_chart(lc, 'B' + str(row_sat + 10 * i)) ws.add_chart(line, 'B' + str(analysis_end_row_number + 6 * i))
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)

View File

@ -264,40 +264,40 @@ def generate_excel(report,
# Second: 报告期单位面积消耗 # Second: 报告期单位面积消耗
# 9 + ca_len * 2: title # 9 + ca_len * 2: title
# 10 + ca_len * 2: table title # 10 + ca_len * 2: table title
# row_title + 2 ~ row_title + 2 + ca_len : table_data # per_unit_area_start_row_number + 2 ~ per_unit_area_start_row_number + 2 + ca_len : table_data
################################################# #################################################
if has_energy_data_flag: if has_energy_data_flag:
names = reporting_period_data['names'] names = reporting_period_data['names']
ca_len = len(names) ca_len = len(names)
row_title = 9 + ca_len * 2 per_unit_area_start_row_number = 9 + ca_len * 2
ws['B' + str(row_title)].font = title_font ws['B' + str(per_unit_area_start_row_number)].font = title_font
ws['B' + str(row_title)] = name + ' 单位面积值' + str(report['store']['area']) + '' ws['B' + str(per_unit_area_start_row_number)] = name + ' 单位面积值' + str(report['store']['area']) + ''
category = reporting_period_data['names'] category = reporting_period_data['names']
# table_title # table_title
ws['B' + str(row_title + 1)].fill = table_fill ws['B' + str(per_unit_area_start_row_number + 1)].fill = table_fill
ws['B' + str(row_title + 1)].font = title_font ws['B' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['B' + str(row_title + 1)].alignment = c_c_alignment ws['B' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['B' + str(row_title + 1)] = '报告期' ws['B' + str(per_unit_area_start_row_number + 1)] = '报告期'
ws['B' + str(row_title + 1)].border = f_border ws['B' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['C' + str(row_title + 1)].font = title_font ws['C' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['C' + str(row_title + 1)].alignment = c_c_alignment ws['C' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['C' + str(row_title + 1)] = '平均负荷' ws['C' + str(per_unit_area_start_row_number + 1)] = '平均负荷'
ws['C' + str(row_title + 1)].border = f_border ws['C' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['D' + str(row_title + 1)].font = title_font ws['D' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['D' + str(row_title + 1)].alignment = c_c_alignment ws['D' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['D' + str(row_title + 1)] = '最大负荷' ws['D' + str(per_unit_area_start_row_number + 1)] = '最大负荷'
ws['D' + str(row_title + 1)].border = f_border ws['D' + str(per_unit_area_start_row_number + 1)].border = f_border
# table_data # table_data
for i, value in enumerate(category): for i, value in enumerate(category):
row_data = row_title + 2 + i row_data = per_unit_area_start_row_number + 2 + i
ws['B' + str(row_data)].font = name_font ws['B' + str(row_data)].font = name_font
ws['B' + str(row_data)].alignment = c_c_alignment ws['B' + str(row_data)].alignment = c_c_alignment
ws['B' + str(row_data)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][ ws['B' + str(row_data)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][
@ -322,9 +322,9 @@ def generate_excel(report,
######################################################## ########################################################
# Third: 详细数据 # Third: 详细数据
# row_sat~ row_sat + 6*cal_len: line # analysis_end_row_number~ analysis_end_row_number + 6*cal_len: line
# row_da: table title # detailed_start_row_number: table title
# row_da + 1~: table_data # detailed_start_row_number + 1~: table_data
######################################################## ########################################################
has_timestamps_flag = True has_timestamps_flag = True
if "timestamps" not in reporting_period_data.keys() or \ if "timestamps" not in reporting_period_data.keys() or \
@ -337,40 +337,37 @@ def generate_excel(report,
names = reporting_period_data['names'] names = reporting_period_data['names']
ca_len = len(names) ca_len = len(names)
time_len = len(timestamps) time_len = len(timestamps)
# row_lines == the number of rows of lines line_charts_row_number = 6 * ca_len
row_lines = 6 * ca_len analysis_end_row_number = 12 + 3 * ca_len
# row_sat == the number of rows of statistical analysis table detailed_start_row_number = analysis_end_row_number + line_charts_row_number + 1
row_sat = 12 + 3 * ca_len
# row_da == the number of rows of Detailed data
row_da = row_sat + row_lines + 1
ws['B' + str(row_da)].font = title_font ws['B' + str(detailed_start_row_number)].font = title_font
ws['B' + str(row_da)] = name + ' 详细数据' ws['B' + str(detailed_start_row_number)] = name + ' 详细数据'
# table_title # table_title
ws['B' + str(row_da + 1)].fill = table_fill ws['B' + str(detailed_start_row_number + 1)].fill = table_fill
ws['B' + str(row_da + 1)].font = name_font ws['B' + str(detailed_start_row_number + 1)].font = name_font
ws['B' + str(row_da + 1)].alignment = c_c_alignment ws['B' + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws['B' + str(row_da + 1)] = "日期时间" ws['B' + str(detailed_start_row_number + 1)] = "日期时间"
ws['B' + str(row_da + 1)].border = f_border ws['B' + str(detailed_start_row_number + 1)].border = f_border
for i in range(0, ca_len): for i in range(0, ca_len):
col_average = chr(ord('C') + 2 * i) col_average = chr(ord('C') + 2 * i)
col_maximum = chr(ord('D') + 2 * i) col_maximum = chr(ord('D') + 2 * i)
ws[col_average + str(row_da + 1)].font = name_font ws[col_average + str(detailed_start_row_number + 1)].font = name_font
ws[col_average + str(row_da + 1)].alignment = c_c_alignment ws[col_average + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws[col_average + str(row_da + 1)] = names[i] + " 平均负荷(" + reporting_period_data['units'][ ws[col_average + str(detailed_start_row_number + 1)] = names[i] + " 平均负荷(" + reporting_period_data['units'][
i] + "/H)" i] + "/H)"
ws[col_average + str(row_da + 1)].border = f_border ws[col_average + str(detailed_start_row_number + 1)].border = f_border
ws[col_maximum + str(row_da + 1)].font = name_font ws[col_maximum + str(detailed_start_row_number + 1)].font = name_font
ws[col_maximum + str(row_da + 1)].alignment = c_c_alignment ws[col_maximum + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws[col_maximum + str(row_da + 1)] = names[i] + " 最大负荷(" + reporting_period_data['units'][ ws[col_maximum + str(detailed_start_row_number + 1)] = names[i] + " 最大负荷(" + reporting_period_data['units'][
i] + "/H)" i] + "/H)"
ws[col_maximum + str(row_da + 1)].border = f_border ws[col_maximum + str(detailed_start_row_number + 1)].border = f_border
# table_date # table_date
for i in range(0, time_len): for i in range(0, time_len):
rows = i + row_da + 2 rows = i + detailed_start_row_number + 2
ws['B' + str(rows)].font = name_font ws['B' + str(rows)].font = name_font
ws['B' + str(rows)].alignment = c_c_alignment ws['B' + str(rows)].alignment = c_c_alignment
@ -395,32 +392,36 @@ def generate_excel(report,
ws[col_maximum + str(rows)].number_format = '0.00' ws[col_maximum + str(rows)].number_format = '0.00'
ws[col_maximum + str(rows)].border = f_border ws[col_maximum + str(rows)].border = f_border
# LineChart ########################################################
# third: LineChart
# LineChart requires data from the detailed data table in the Excel file
# so print the detailed data table first and then print LineChart
########################################################
for i in range(0, ca_len): for i in range(0, ca_len):
lc = LineChart() line = LineChart()
lc.title = "报告期 最大负荷" line.title = "报告期 最大负荷 - " + names[i] + "(" + reporting_period_data['units'][i] + ")"
lc.style = 10 line.style = 10
lc.x_axis.majorTickMark = 'in' line.x_axis.majorTickMark = 'in'
lc.y_axis.majorTickMark = 'in' line.y_axis.majorTickMark = 'in'
lc.smooth = True line.smooth = True
lc.x_axis.crosses = 'min' line.x_axis.crosses = 'min'
lc.height = 8.25 line.height = 8.25
lc.width = 24 line.width = 24
lc.dLbls = DataLabelList() line.dLbls = DataLabelList()
lc.dLbls.dLblPos = 't' line.dLbls.dLblPos = 't'
lc.dLbls.showVal = True line.dLbls.showVal = True
times = Reference(ws, min_col=2, min_row=row_da + 2, times = Reference(ws, min_col=2, min_row=detailed_start_row_number + 2,
max_row=row_da + 2 + time_len) max_row=detailed_start_row_number + 2 + time_len)
lc_data = Reference(ws, min_col=2 + 2 * (i+1), min_row=row_da + 1, line_data = Reference(ws, min_col=2 + 2 * (i+1), min_row=detailed_start_row_number + 1,
max_row=row_da + 1 + time_len) max_row=detailed_start_row_number + 1 + time_len)
lc.add_data(lc_data, titles_from_data=True) line.add_data(line_data, titles_from_data=True)
lc.set_categories(times) line.set_categories(times)
ser = lc.series[0] ser = line.series[0]
ser.marker.symbol = "diamond" ser.marker.symbol = "diamond"
ser.marker.size = 5 ser.marker.size = 5
chart_col = 'B' chart_col = 'B'
chart_cell = str(row_sat + 6 * i) chart_cell = str(analysis_end_row_number + 6 * i)
ws.add_chart(lc, chart_col + chart_cell) ws.add_chart(line, chart_col + chart_cell)
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)

View File

@ -7,6 +7,7 @@ from openpyxl.chart import (
LineChart, LineChart,
Reference, Reference,
) )
from openpyxl.chart.label import DataLabelList
from openpyxl.drawing.image import Image from openpyxl.drawing.image import Image
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font from openpyxl.styles import PatternFill, Border, Side, Alignment, Font
@ -69,19 +70,16 @@ def generate_excel(report,
ws = wb.active ws = wb.active
# Row height # Row height
ws.row_dimensions[1].height = 121 ws.row_dimensions[1].height = 102
for i in range(2, 37 + 1): for i in range(2, 2000 + 1):
ws.row_dimensions[i].height = 30 ws.row_dimensions[i].height = 42
for i in range(38, 90 + 1):
ws.row_dimensions[i].height = 30
# Col width # Col width
ws.column_dimensions['A'].width = 1.5 ws.column_dimensions['A'].width = 1.5
ws.column_dimensions['B'].width = 20.0 ws.column_dimensions['B'].width = 25.0
for i in range(ord('C'), ord('I')): for i in range(ord('C'), ord('L')):
ws.column_dimensions[chr(i)].width = 15.0 ws.column_dimensions[chr(i)].width = 15.0
# Font # Font
@ -319,63 +317,61 @@ def generate_excel(report,
# Second: 报告期消耗 # Second: 报告期消耗
# 9 + ca_len * 2: title # 9 + ca_len * 2: title
# 10 + ca_len * 2: table title # 10 + ca_len * 2: table title
# row_title + 2 ~ row_title + 2 + ca_len : table_data # per_unit_area_start_row_number + 2 ~ per_unit_area_start_row_number + 2 + ca_len : table_data
################################################# #################################################
if has_energy_data_flag: if has_energy_data_flag:
names = reporting_period_data['names'] names = reporting_period_data['names']
ca_len = len(names) ca_len = len(names)
row_title = 9 + ca_len * 2 per_unit_area_start_row_number = 9 + ca_len * 2
ws['B' + str(row_title)].font = title_font ws['B' + str(per_unit_area_start_row_number)].font = title_font
ws['B' + str(row_title)] = name + ' 单位面积值' ws['B' + str(per_unit_area_start_row_number)] = name + ' 单位面积值' + str(report['store']['area']) + ''
ws['D' + str(row_title)].font = title_font
ws['D' + str(row_title)] = str(report['store']['area']) + ''
category = reporting_period_data['names'] category = reporting_period_data['names']
# table_title # table_title
ws['B' + str(row_title + 1)].fill = table_fill ws['B' + str(per_unit_area_start_row_number + 1)].fill = table_fill
ws['B' + str(row_title + 1)].font = title_font ws['B' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['B' + str(row_title + 1)].alignment = c_c_alignment ws['B' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['B' + str(row_title + 1)] = '报告期' ws['B' + str(per_unit_area_start_row_number + 1)] = '报告期'
ws['B' + str(row_title + 1)].border = f_border ws['B' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['C' + str(row_title + 1)].font = title_font ws['C' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['C' + str(row_title + 1)].alignment = c_c_alignment ws['C' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['C' + str(row_title + 1)] = '算术平均数' ws['C' + str(per_unit_area_start_row_number + 1)] = '算术平均数'
ws['C' + str(row_title + 1)].border = f_border ws['C' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['D' + str(row_title + 1)].font = title_font ws['D' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['D' + str(row_title + 1)].alignment = c_c_alignment ws['D' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['D' + str(row_title + 1)] = '中位数' ws['D' + str(per_unit_area_start_row_number + 1)] = '中位数'
ws['D' + str(row_title + 1)].border = f_border ws['D' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['E' + str(row_title + 1)].font = title_font ws['E' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['E' + str(row_title + 1)].alignment = c_c_alignment ws['E' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['E' + str(row_title + 1)] = '最小值' ws['E' + str(per_unit_area_start_row_number + 1)] = '最小值'
ws['E' + str(row_title + 1)].border = f_border ws['E' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['F' + str(row_title + 1)].font = title_font ws['F' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['F' + str(row_title + 1)].alignment = c_c_alignment ws['F' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['F' + str(row_title + 1)] = '最大值' ws['F' + str(per_unit_area_start_row_number + 1)] = '最大值'
ws['F' + str(row_title + 1)].border = f_border ws['F' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['G' + str(row_title + 1)].font = title_font ws['G' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['G' + str(row_title + 1)].alignment = c_c_alignment ws['G' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['G' + str(row_title + 1)] = '样本标准差' ws['G' + str(per_unit_area_start_row_number + 1)] = '样本标准差'
ws['G' + str(row_title + 1)].border = f_border ws['G' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['H' + str(row_title + 1)].font = title_font ws['H' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['H' + str(row_title + 1)].alignment = c_c_alignment ws['H' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['H' + str(row_title + 1)] = '样本方差' ws['H' + str(per_unit_area_start_row_number + 1)] = '样本方差'
ws['H' + str(row_title + 1)].border = f_border ws['H' + str(per_unit_area_start_row_number + 1)].border = f_border
# table_data # table_data
for i, value in enumerate(category): for i, value in enumerate(category):
row_data = row_title + 2 + i row_data = per_unit_area_start_row_number + 2 + i
ws['B' + str(row_data)].font = name_font ws['B' + str(row_data)].font = name_font
ws['B' + str(row_data)].alignment = c_c_alignment ws['B' + str(row_data)].alignment = c_c_alignment
ws['B' + str(row_data)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][ ws['B' + str(row_data)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][
@ -432,9 +428,9 @@ def generate_excel(report,
######################################################## ########################################################
# Third: 详细数据 # Third: 详细数据
# row_sat+row_title~ row_sat+row_title+time_len: line # detailed_start_row_number~ detailed_start_row_number+time_len: line
# row_sat+1+row_title: table title # detailed_start_row_number+1: table title
# i + row_sat + 2 + 10 * ca_len~: table_data # i + analysis_end_row_number + 2 + 6 * ca_len~: table_data
######################################################## ########################################################
has_timestamps_flag = True has_timestamps_flag = True
if "timestamps" not in reporting_period_data.keys() or \ if "timestamps" not in reporting_period_data.keys() or \
@ -449,29 +445,29 @@ def generate_excel(report,
ca_len = len(names) ca_len = len(names)
time_len = len(timestamps) time_len = len(timestamps)
# title # title
row_title = 10 * ca_len line_charts_row_number = 6 * ca_len
# row_st == row_statistical analysis table analysis_end_row_number = 12 + 3 * ca_len
row_sat = 12 + 3 * ca_len detailed_start_row_number = analysis_end_row_number + line_charts_row_number + 1
ws['B' + str(row_sat + row_title)].font = title_font ws['B' + str(detailed_start_row_number)].font = title_font
ws['B' + str(row_sat + row_title)] = name + ' 详细数据' ws['B' + str(detailed_start_row_number)] = name + ' 详细数据'
# table_title # table_title
ws['B' + str(row_sat + 1 + row_title)].fill = table_fill ws['B' + str(detailed_start_row_number + 1)].fill = table_fill
ws['B' + str(row_sat + 1 + row_title)].font = name_font ws['B' + str(detailed_start_row_number + 1)].font = name_font
ws['B' + str(row_sat + 1 + row_title)].alignment = c_c_alignment ws['B' + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws['B' + str(row_sat + 1 + row_title)] = "时间" ws['B' + str(detailed_start_row_number + 1)] = "时间"
ws['B' + str(row_sat + 1 + row_title)].border = f_border ws['B' + str(detailed_start_row_number + 1)].border = f_border
for i in range(0, ca_len): for i in range(0, ca_len):
col = chr(ord('C') + i) col = chr(ord('C') + i)
ws[col + str(row_sat + 1 + row_title)].font = name_font ws[col + str(detailed_start_row_number + 1)].font = name_font
ws[col + str(row_sat + 1 + row_title)].alignment = c_c_alignment ws[col + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws[col + str(row_sat + 1 + row_title)] = names[i] + " - (" + reporting_period_data['units'][i] + ")" ws[col + str(detailed_start_row_number + 1)] = names[i] + " - (" + reporting_period_data['units'][i] + ")"
ws[col + str(row_sat + 1 + row_title)].border = f_border ws[col + str(detailed_start_row_number + 1)].border = f_border
# table_date # table_date
for i in range(0, time_len): for i in range(0, time_len):
rows = i + row_sat + 2 + 10 * ca_len rows = i + detailed_start_row_number + 2
ws['B' + str(rows)].font = name_font ws['B' + str(rows)].font = name_font
ws['B' + str(rows)].alignment = c_c_alignment ws['B' + str(rows)].alignment = c_c_alignment
@ -488,7 +484,7 @@ def generate_excel(report,
ws[col + str(rows)].border = f_border ws[col + str(rows)].border = f_border
# 小计 # 小计
row_subtotals = row_sat + 2 + time_len + 10 * ca_len row_subtotals = detailed_start_row_number + 2 + time_len
ws['B' + str(row_subtotals)].font = name_font ws['B' + str(row_subtotals)].font = name_font
ws['B' + str(row_subtotals)].alignment = c_c_alignment ws['B' + str(row_subtotals)].alignment = c_c_alignment
ws['B' + str(row_subtotals)] = "小计" ws['B' + str(row_subtotals)] = "小计"
@ -503,25 +499,34 @@ def generate_excel(report,
ws[col + str(row_subtotals)].border = f_border ws[col + str(row_subtotals)].border = f_border
ws[col + str(row_subtotals)].number_format = '0.00' ws[col + str(row_subtotals)].number_format = '0.00'
# LineChart ########################################################
# third: LineChart
# LineChart requires data from the detailed data table in the Excel file
# so print the detailed data table first and then print LineChart
########################################################
for i in range(0, ca_len): for i in range(0, ca_len):
lc = LineChart() line = LineChart()
lc.title = "报告期消耗" + " - " + names[i] + "(" + reporting_period_data['units'][i] + ")" line.title = "报告期消耗" + " - " + names[i] + "(" + reporting_period_data['units'][i] + ")"
lc.style = 10 line.style = 10
lc.height = 8.40 # cm 1.05*8 1.05cm = 30 pt line.x_axis.majorTickMark = 'in'
lc.width = 31 line.y_axis.majorTickMark = 'in'
lc.x_axis.majorTickMark = 'in' line.smooth = True
lc.y_axis.majorTickMark = 'in' line.x_axis.crosses = 'min'
times = Reference(ws, min_col=2, min_row=row_sat + 2 + row_title, line.height = 8.25
max_row=row_sat + 2 + row_title + time_len) line.width = 24
lc_data = Reference(ws, min_col=3 + i, min_row=row_sat + 1 + row_title, line.dLbls = DataLabelList()
max_row=row_sat + 1 + row_title + time_len) line.dLbls.dLblPos = 't'
lc.add_data(lc_data, titles_from_data=True) line.dLbls.showVal = True
lc.set_categories(times) times = Reference(ws, min_col=2, min_row=detailed_start_row_number + 2,
ser = lc.series[0] max_row=detailed_start_row_number + 2 + time_len)
line_data = Reference(ws, min_col=3 + i, min_row=detailed_start_row_number + 1,
max_row=detailed_start_row_number + 1 + time_len)
line.add_data(line_data, titles_from_data=True)
line.set_categories(times)
ser = line.series[0]
ser.marker.symbol = "diamond" ser.marker.symbol = "diamond"
ser.marker.size = 5 ser.marker.size = 5
ws.add_chart(lc, 'B' + str(row_sat + 10 * i)) ws.add_chart(line, 'B' + str(analysis_end_row_number + 10 * i))
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)

View File

@ -5,6 +5,7 @@ from openpyxl.chart import (
LineChart, LineChart,
Reference, Reference,
) )
from openpyxl.chart.label import DataLabelList
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font from openpyxl.styles import PatternFill, Border, Side, Alignment, Font
from openpyxl.drawing.image import Image from openpyxl.drawing.image import Image
from openpyxl import Workbook from openpyxl import Workbook
@ -68,19 +69,16 @@ def generate_excel(report,
ws = wb.active ws = wb.active
# Row height # Row height
ws.row_dimensions[1].height = 121 ws.row_dimensions[1].height = 102
for i in range(2, 37 + 1): for i in range(2, 2000 + 1):
ws.row_dimensions[i].height = 30 ws.row_dimensions[i].height = 42
for i in range(38, 90 + 1):
ws.row_dimensions[i].height = 30
# Col width # Col width
ws.column_dimensions['A'].width = 1.5 ws.column_dimensions['A'].width = 1.5
ws.column_dimensions['B'].width = 20.0 ws.column_dimensions['B'].width = 25.0
for i in range(ord('C'), ord('I')): for i in range(ord('C'), ord('L')):
ws.column_dimensions[chr(i)].width = 15.0 ws.column_dimensions[chr(i)].width = 15.0
# Font # Font
@ -183,8 +181,6 @@ def generate_excel(report,
if has_energy_data_flag: if has_energy_data_flag:
ws['B6'].font = title_font ws['B6'].font = title_font
ws['B6'] = name + ' 统计分析' ws['B6'] = name + ' 统计分析'
# ws['D6'].font = title_font
# ws['D6'] = '面积' +report['space']['area']
category = reporting_period_data['names'] category = reporting_period_data['names']
@ -320,63 +316,61 @@ def generate_excel(report,
# Second: 报告期消耗 # Second: 报告期消耗
# 9 + ca_len * 2: title # 9 + ca_len * 2: title
# 10 + ca_len * 2: table title # 10 + ca_len * 2: table title
# row_title + 2 ~ row_title + 2 + ca_len : table_data # per_unit_area_start_row_number + 2 ~ per_unit_area_start_row_number + 2 + ca_len : table_data
################################################# #################################################
if has_energy_data_flag: if has_energy_data_flag:
names = reporting_period_data['names'] names = reporting_period_data['names']
ca_len = len(names) ca_len = len(names)
row_title = 9 + ca_len * 2 per_unit_area_start_row_number = 9 + ca_len * 2
ws['B' + str(row_title)].font = title_font ws['B' + str(per_unit_area_start_row_number)].font = title_font
ws['B' + str(row_title)] = name + ' 单位面积值' ws['B' + str(per_unit_area_start_row_number)] = name + ' 单位面积值' + str(report['tenant']['area']) + ''
ws['D' + str(row_title)].font = title_font
ws['D' + str(row_title)] = str(report['tenant']['area']) + ''
category = reporting_period_data['names'] category = reporting_period_data['names']
# table_title # table_title
ws['B' + str(row_title + 1)].fill = table_fill ws['B' + str(per_unit_area_start_row_number + 1)].fill = table_fill
ws['B' + str(row_title + 1)].font = title_font ws['B' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['B' + str(row_title + 1)].alignment = c_c_alignment ws['B' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['B' + str(row_title + 1)] = '报告期' ws['B' + str(per_unit_area_start_row_number + 1)] = '报告期'
ws['B' + str(row_title + 1)].border = f_border ws['B' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['C' + str(row_title + 1)].font = title_font ws['C' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['C' + str(row_title + 1)].alignment = c_c_alignment ws['C' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['C' + str(row_title + 1)] = '算术平均数' ws['C' + str(per_unit_area_start_row_number + 1)] = '算术平均数'
ws['C' + str(row_title + 1)].border = f_border ws['C' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['D' + str(row_title + 1)].font = title_font ws['D' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['D' + str(row_title + 1)].alignment = c_c_alignment ws['D' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['D' + str(row_title + 1)] = '中位数' ws['D' + str(per_unit_area_start_row_number + 1)] = '中位数'
ws['D' + str(row_title + 1)].border = f_border ws['D' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['E' + str(row_title + 1)].font = title_font ws['E' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['E' + str(row_title + 1)].alignment = c_c_alignment ws['E' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['E' + str(row_title + 1)] = '最小值' ws['E' + str(per_unit_area_start_row_number + 1)] = '最小值'
ws['E' + str(row_title + 1)].border = f_border ws['E' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['F' + str(row_title + 1)].font = title_font ws['F' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['F' + str(row_title + 1)].alignment = c_c_alignment ws['F' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['F' + str(row_title + 1)] = '最大值' ws['F' + str(per_unit_area_start_row_number + 1)] = '最大值'
ws['F' + str(row_title + 1)].border = f_border ws['F' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['G' + str(row_title + 1)].font = title_font ws['G' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['G' + str(row_title + 1)].alignment = c_c_alignment ws['G' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['G' + str(row_title + 1)] = '样本标准差' ws['G' + str(per_unit_area_start_row_number + 1)] = '样本标准差'
ws['G' + str(row_title + 1)].border = f_border ws['G' + str(per_unit_area_start_row_number + 1)].border = f_border
ws['H' + str(row_title + 1)].font = title_font ws['H' + str(per_unit_area_start_row_number + 1)].font = title_font
ws['H' + str(row_title + 1)].alignment = c_c_alignment ws['H' + str(per_unit_area_start_row_number + 1)].alignment = c_c_alignment
ws['H' + str(row_title + 1)] = '样本方差' ws['H' + str(per_unit_area_start_row_number + 1)] = '样本方差'
ws['H' + str(row_title + 1)].border = f_border ws['H' + str(per_unit_area_start_row_number + 1)].border = f_border
# table_data # table_data
for i, value in enumerate(category): for i, value in enumerate(category):
row_data = row_title + 2 + i row_data = per_unit_area_start_row_number + 2 + i
ws['B' + str(row_data)].font = name_font ws['B' + str(row_data)].font = name_font
ws['B' + str(row_data)].alignment = c_c_alignment ws['B' + str(row_data)].alignment = c_c_alignment
ws['B' + str(row_data)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][ ws['B' + str(row_data)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][
@ -433,9 +427,9 @@ def generate_excel(report,
######################################################## ########################################################
# Third: 详细数据 # Third: 详细数据
# row_sat+row_title~ row_sat+row_title+time_len: line # detailed_start_row_number~ detailed_start_row_number+time_len: line
# row_sat+1+row_title: table title # detailed_start_row_number + 1: table title
# i + row_sat + 2 + 10 * ca_len~: table_data # i + analysis_end_row_number + 2 + 6 * ca_len~: table_data
######################################################## ########################################################
has_timestamps_flag = True has_timestamps_flag = True
if "timestamps" not in reporting_period_data.keys() or \ if "timestamps" not in reporting_period_data.keys() or \
@ -450,29 +444,29 @@ def generate_excel(report,
ca_len = len(names) ca_len = len(names)
time_len = len(timestamps) time_len = len(timestamps)
# title # title
row_title = 10 * ca_len line_charts_row_number = 6 * ca_len
# row_st == row_statistical analysis table analysis_end_row_number = 12 + 3 * ca_len
row_sat = 12 + 3 * ca_len detailed_start_row_number = analysis_end_row_number + line_charts_row_number + 1
ws['B' + str(row_sat+row_title)].font = title_font ws['B' + str(detailed_start_row_number)].font = title_font
ws['B' + str(row_sat+row_title)] = name + ' 详细数据' ws['B' + str(detailed_start_row_number)] = name + ' 详细数据'
# table_title # table_title
ws['B' + str(row_sat+1+row_title)].fill = table_fill ws['B' + str(detailed_start_row_number + 1)].fill = table_fill
ws['B' + str(row_sat+1+row_title)].font = name_font ws['B' + str(detailed_start_row_number + 1)].font = name_font
ws['B' + str(row_sat+1+row_title)].alignment = c_c_alignment ws['B' + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws['B' + str(row_sat+1+row_title)] = "时间" ws['B' + str(detailed_start_row_number + 1)] = "时间"
ws['B' + str(row_sat+1+row_title)].border = f_border ws['B' + str(detailed_start_row_number + 1)].border = f_border
for i in range(0, ca_len): for i in range(0, ca_len):
col = chr(ord('C') + i) col = chr(ord('C') + i)
ws[col + str(row_sat+1+row_title)].font = name_font ws[col + str(detailed_start_row_number + 1)].font = name_font
ws[col + str(row_sat+1+row_title)].alignment = c_c_alignment ws[col + str(detailed_start_row_number + 1)].alignment = c_c_alignment
ws[col + str(row_sat+1+row_title)] = names[i] + " - (" + reporting_period_data['units'][i] + ")" ws[col + str(detailed_start_row_number + 1)] = names[i] + " - (" + reporting_period_data['units'][i] + ")"
ws[col + str(row_sat+1+row_title)].border = f_border ws[col + str(detailed_start_row_number + 1)].border = f_border
# table_date # table_date
for i in range(0, time_len): for i in range(0, time_len):
rows = i + row_sat + 2 + 10 * ca_len rows = i + detailed_start_row_number + 2
ws['B' + str(rows)].font = name_font ws['B' + str(rows)].font = name_font
ws['B' + str(rows)].alignment = c_c_alignment ws['B' + str(rows)].alignment = c_c_alignment
@ -489,7 +483,7 @@ def generate_excel(report,
ws[col + str(rows)].border = f_border ws[col + str(rows)].border = f_border
# 小计 # 小计
row_subtotals = row_sat + 2 + time_len + 10 * ca_len row_subtotals = detailed_start_row_number + 2 + time_len
ws['B' + str(row_subtotals)].font = name_font ws['B' + str(row_subtotals)].font = name_font
ws['B' + str(row_subtotals)].alignment = c_c_alignment ws['B' + str(row_subtotals)].alignment = c_c_alignment
ws['B' + str(row_subtotals)] = "小计" ws['B' + str(row_subtotals)] = "小计"
@ -504,26 +498,35 @@ def generate_excel(report,
ws[col + str(row_subtotals)].border = f_border ws[col + str(row_subtotals)].border = f_border
ws[col + str(row_subtotals)].number_format = '0.00' ws[col + str(row_subtotals)].number_format = '0.00'
# LineChart ########################################################
# third: LineChart
# LineChart requires data from the detailed data table in the Excel file
# so print the detailed data table first and then print LineChart
########################################################
for i in range(0, ca_len): for i in range(0, ca_len):
lc = LineChart() line = LineChart()
lc.title = "报告期消耗" + " - " + names[i] + "(" + reporting_period_data['units'][i] + ")" line.title = "报告期消耗" + " - " + names[i] + "(" + reporting_period_data['units'][i] + ")"
lc.style = 10 line.style = 10
lc.height = 8.40 # cm 1.05*8 1.05cm = 30 pt line.x_axis.majorTickMark = 'in'
lc.width = 31 line.y_axis.majorTickMark = 'in'
lc.x_axis.majorTickMark = 'in' line.smooth = True
lc.y_axis.majorTickMark = 'in' line.x_axis.crosses = 'min'
times = Reference(ws, min_col=2, min_row=row_sat + 2 + row_title, line.height = 8.25
max_row=row_sat + 2 + row_title + time_len) line.width = 24
lc_data = Reference(ws, min_col=3 + i, min_row=row_sat + 1 + row_title, line.dLbls = DataLabelList()
max_row=row_sat + 1 + row_title + time_len) line.dLbls.dLblPos = 't'
lc.add_data(lc_data, titles_from_data=True) line.dLbls.showVal = True
lc.set_categories(times) times = Reference(ws, min_col=2, min_row=detailed_start_row_number + 2,
ser = lc.series[0] max_row=detailed_start_row_number + 2 + time_len)
line_data = Reference(ws, min_col=3 + i, min_row=detailed_start_row_number + 1,
max_row=detailed_start_row_number + 1 + time_len)
line.add_data(line_data, titles_from_data=True)
line.set_categories(times)
ser = line.series[0]
ser.marker.symbol = "diamond" ser.marker.symbol = "diamond"
ser.marker.size = 5 ser.marker.size = 5
ws.add_chart(lc, 'B' + str(row_sat + 10 * i)) ws.add_chart(line, 'B' + str(analysis_end_row_number + 6 * i))
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)