xlwings中的图表对象

2024-10-02 00:38:10 发布

您现在位置:Python中文网/ 问答频道 /正文

代码片段来自xlwingshere的官方文档,它是我问题的设置。在

import xlwings as xw
sht = xw.Book().sheets[0]
sht.range('A1').value = [['Foo1', 'Foo2'], [1, 2]]
chart = sht.charts.add()
chart.set_source_data(sht.range('A1').expand())
chart.chart_type = 'line'
chart.name

运行print(chart.api)输出下面的元组。在

^{pr2}$

如果我想使用api属性来执行一些基本的图表操作,如删除图例和添加标题,则只有在对chart.api[1]执行此操作时才有效。例如,下面的代码可以正常工作。它删除图表图例并添加标题。在

chart.api[1].HasLegend = 0
chart.api[1].SetElement(2)
chart.api[1].ChartTitle.Text = 'A title'

但是,我对chart.api[0]所做的任何操作都会产生错误(例如,print(chart.api[0].HasLegend)会产生错误)。我不明白这是什么东西,也不知道它有多有用。我在官方文件中找不到任何与此相关的东西。在

最后我的问题是:上面索引0处的对象是什么?请帮我摸摸是什么。在


Tags: 代码api标题官方a1错误chart图表
1条回答
网友
1楼 · 发布于 2024-10-02 00:38:10

还有另一篇文章是关于索引0处对象的问题。在

set chart name in Xlwings

The expression chart.api returns a tuple with two COM wrappers. I'm not really sure why there are two COM wrappers, but it seems that you need the second one to access the chart. Hence the use of chart.api[1] here.

相关问题 更多 >

    热门问题