我需要使用python更改excel中图表的公式,以了解如何做到这一点我记录了一个宏,这就是我得到的:
ActiveSheet.ChartObjects("Graphique 1").Activate
ActiveChart.Axes(xlCategory).Select
ActiveSheet.ChartObjects("Graphique 1").Activate
ActiveChart.SeriesCollection(1).Values = "='Données'!$ET$68:$IJ$68"
ActiveChart.SeriesCollection(1).XValues = "='Données'!$ET$1:$IJ$1"
因此,我的图表显然被称为 Graphique 1,并且根据 Excel 文档 ChartObjects 在工作表对象上被调用。容易吧?
但是似乎没有任何效果:
from win32com import client
xl = client.Dispatch("Excel.Application")
xl.Visible = 1
workbook = xl.Workbooks.Open(r"D:\some\path\file.xls")
ws = workbook.Sheets("the sheet")
#tried but did not work :
ws.ChartObjects("Graphique 1").Activate = True #Exception occured : no element with this name
ws.ChartObjects("Graphique 1").Activate = 1 #Exception occured : no element with this name
ws.ChartObjects(1).Activate = True #Exception occured : no message
ws.ChartObjects(1).Activate = 1 #Exception occured : no message
#in case it's 0ed indexed
ws.ChartObjects(0).Activate = True #Exception occured : no message
ws.ChartObjects(0).Activate = 1 #Exception occured : no message
print ws.ChartObjects("Graphique 1").SeriesCollection(1).Values #Exception occured : no element with this name
print ws.ChartObjects(1).SeriesCollection(1).Values #Exception occured : no message
有任何想法吗?谢谢。
Ps:我现在不太了解excel,我有10张纸,我假设我的chartObject在这张纸上,如果它正在绘制
编辑
奇怪的是,每张纸的 ChartObjects 计数返回 0,怎么可能?我可以亲眼看到
>>> for i in range(1,10):
ws = workbook.Sheets(i)
co = ws.ChartObjects()
count = co.Count
print count
0
0
0
0
0
0
0
0
0