在Xlwings中设置图表名称 [英] set chart name in Xlwings

查看:104
本文介绍了在Xlwings中设置图表名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过xlwings绘制图表时,无法更改图表名称.图表名称和图例名称仍为系列1",但左上角显示的是我想要的2月销售量"

When I plot a chart by xlwings, I can't change chart name. The chart name and legend name are still 'Series 1', but the upper left corner shows 'Feb sales' which is I want

import xlwings as xw

sht = xw.Book().sheets[0]
sht.range('A1').value = list(zip([1, 2, 3, 4]))
chart = sht.charts.add()
chart.set_source_data(sht.range('A1').expand())
chart.chart_type = 'line_markers'
chart.name='Feb sales'
#chart.api.ChartTitle.Text = 'Feb sales'
#chart.delete()

创建图表

这是一个已知问题吗?我该如何解决?

Is this a known issue? How can I fix this?

推荐答案

这应该有效:

import xlwings as xw

sht = xw.Book().sheets[0]
sht.range('A1').value = list(zip([1, 2, 3, 4]))
chart = sht.charts.add()
chart.set_source_data(sht.range('A1').expand())
chart.chart_type = 'line_markers'

chart.api[1].SetElement(2)  # Place chart title at the top
chart.api[1].ChartTitle.Text = 'Feb sales'  # Change text of the chart title

表达式 chart.api 返回一个带有两个COM包装器的元组.我不太确定为什么有两个COM包装器,但是您似乎需要第二个包装器才能访问图表.因此,此处使用 chart.api [1] .

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.

具有属性 xlwings.Chart.name ,您可以设置Excel图表的 Name 属性(就像您在问题代码中所做的一样),但这不是用于显示的属性.要在图表中显示文本,您需要设置excel图表的 ChartTitle 属性(在此答案的代码中已完成).

With the attribute xlwings.Chart.name you can set an excel chart's Name property (as you did in your question's code), but this is not a property that is used for display. To achieve display of text in the chart you need to set the excel chart's ChartTitle property (as done in this answer's code).

这篇关于在Xlwings中设置图表名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆