python图表excel数据改变_使用python win32com在excel 2007图表中更改轴标签

I have an excel sheet with some data in first two columns. I created a simple chart with this data. I am facing problem in adding axis labels to the chart.

here is my script

from win32com.client import Dispatch, constants

excel = win32com.client.Dispatch('Excel.Application')

wb = excel.Workbooks.Open( 'output_data.xls', False, True)

excel.Visible = False

ws1 = wb.Worksheets('sheet_1)

ch = ws1.Shapes.AddChart( 73, 200, 50, 800, 500).Select()

excel.ActiveChart.ApplyLayout(1)

excel.ActiveChart.SetSourceData(Source=ws1.Range("$A:$B"))

excel.ActiveChart.ChartTitle.Text = "Integral"

excel.ActiveChart.Legend.Delete()

-------up to this everything fine.

excel.ActiveChart.axes(constants.xlCategory).AxisTitle.Caption = "Z_coordinate"

but when I add the axis labels, it returns with an attribute error xlCategory.

how can I add the axis labels and change the font size.

Thanks in advance.

解决方案

You probably used the wrong enum axis type. Each enum (as far as I can tell) only works for certain types of charts. According to the built-in macro recorder (very useful even for python-based scripts, btw), scatter plots use xlValue, not xlCategory. Try one of the other enums until your code works.

I haven't fully figured out Excel in win32com yet, but I managed to get axis titles to appear after a bit of trial and error. Here's a short snippet from some code I wrote for an XY scatter plot with titles for X axis, Y axis, and Y2 axis:

Foo = chart.SeriesCollection(1)

Bar = chart.SeriesCollection(2)

Bar.AxisGroup = 2

Primary_Axis = chart.Axes(AxisGroup=xlPrimary)

Foo_xAxis = Primary_Axis(1)

Foo_yAxis = Primary_Axis(2)

Foo_xAxis .HasTitle = True

Foo_yAxis .HasTitle = True

Bar_yAxis = chart.Axes(xlValue, AxisGroup=xlSecondary)

Bar_yAxis.HasTitle = True

Foo_xAxis .AxisTitle.Text = "Primary X axis string"

Foo_yAxis .AxisTitle.Text = "Primary Y axis string"

Bar_yAxis.AxisTitle.Text = "Secondary Y axis string(Y2)"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值