在 Xlwings 中设置图表名称
set chart name in Xlwings
当我用xlwings绘制图表时,我无法更改图表名称。图表名称和图例名称仍然是'Series 1',但左上角显示'Feb sales',这是我想要的
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()
created chart
这是一个已知问题吗?我该如何解决这个问题?
这应该有效:
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
returns 具有两个 COM 包装器的元组。我不太确定为什么有两个 COM 包装器,但似乎您需要第二个包装器才能访问图表。因此这里使用 chart.api[1]
。
具有 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
属性 属性(如本答案代码中所做的那样)。
当我用xlwings绘制图表时,我无法更改图表名称。图表名称和图例名称仍然是'Series 1',但左上角显示'Feb sales',这是我想要的
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()
created chart
这是一个已知问题吗?我该如何解决这个问题?
这应该有效:
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
returns 具有两个 COM 包装器的元组。我不太确定为什么有两个 COM 包装器,但似乎您需要第二个包装器才能访问图表。因此这里使用 chart.api[1]
。
具有 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
属性 属性(如本答案代码中所做的那样)。