chart插入到excel里
试过好多种方法,都无法把chart插入到excel,求指点wb=openpyxl.load_workbook(filename)
ws = wb.sheetnames
chart=LineChart()
chart.title='Result'
chart.x_axis.title='times'
chart.y_axis.title='values'
xvalues=Reference(ws,min_col=1,min_row=2,max_row=40)
yvalues=Reference(ws,min_col=6,min_row=2,max_row=40)
chart.add_data(yvalues,from_rows=True,titles_from_data=False)
chart.set_categories(xvalues)
ws.insert_chart('A60',chart)
ws = wb.sheetnames会生成一个列表,这里是错的
改成ws = wb #sheetname是你的工作薄名称
ws.insert_chart('A60',chart)
改为ws.add_chart(chart, "A60")
例子:
import openpyxl
from openpyxl.chart import (
LineChart,
Reference,
)
filename = 'test.xlsx'
wb=openpyxl.load_workbook(filename)
sheetname = 'Sheet1'
ws = wb
chart=LineChart()
chart.title='Result'
chart.x_axis.title='times'
chart.y_axis.title='values'
xvalues=Reference(ws,min_col=1,min_row=2,max_row=40)
yvalues=Reference(ws,min_col=6,min_row=2,max_row=40)
chart.add_data(yvalues,from_rows=True,titles_from_data=False)
chart.set_categories(xvalues)
ws.add_chart(chart, "A1")
wb.save('test.xlsx')
wb = openpyxl.load_workbook(path + "\\Result.xlsx")
ws=wb.create_sheet(‘sheet’)
chart=LineChart()
chart.title='Result'
chart.x_axis.title='times'
chart.y_axis.title='Values'
xvalues=Reference(ws,min_col=1,min_row=2,max_row=ws.max_row)
yvalues=Reference(ws,min_col=2,min_row=2,max_row=ws.max_row)
chart.add_data(yvalues,from_rows=True,titles_from_data=False)
chart.set_categories(xvalues)
ws.add_chart(chart,'G2')
这样操作没有报错,但是图表没有加到excel ,求指导
amywong 发表于 2021-2-1 19:21
wb = openpyxl.load_workbook(path + "\\Result.xlsx")
ws=wb.create_sheet(‘sheet’)
chart=LineChart( ...
操作完以后保存文件了吗
wd.save(path + "\\Result.xlsx")
页:
[1]