我找不到在现有工作簿中修改或删除现有Excel图形的方法。
这是我能实现的唯一代码:
from openpyxl.chart import AreaChart, Reference, Series
import openpyxl as opyxl
def CreateGraphAC(wb, ws, rows, columns):
chart = AreaChart()
chart.title = "Area Chart"
chart.style = 13
chart.x_axis.title = 'Test'
chart.y_axis.title = 'Percentage'
cats = Reference(ws, min_col=1, min_row=2, max_row=rows)
data = Reference(ws, min_col=2, min_row=1, max_col=columns, max_row=rows)
chart.add_data(data, titles_from_data=True)
chart.set_categories(cats)
ws.add_chart(chart, "F1")
wb.save('test.xlsx')
wb = opyxl.load_workbook('test.xlsx')
allSheetNames = wb.sheetnames
ws = wb.active
CreateGraphAC(wb, ws, 2, 2)
我发现的唯一一件事是,你可以在工作表上获得图表列表:
ws._charts
有什么建议吗?提前感谢大家。
自己解决
使用del worksheet._charts[number]
和workbook.save('file.xlsx')
,并使用修改后的参数创建一个新图表。