使用python win32com在excel 2007图表中更改轴标签 [英] changing axis labels in excel 2007 charts using python win32com
问题描述
我有一个Excel表,前两列中有一些数据。我用这个数据创建了一个简单的图表。
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.
这里是我的脚本
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"
但是当我添加轴标签时,它返回一个属性错误xlCategory。
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.
提前感谢。
推荐答案
您可能使用了错误的枚举轴类型。每个枚举(据我所知)只适用于某些类型的图表。根据内置的宏记录器(非常有用,甚至对于基于python的脚本,btw),散点图使用xlValue,而不是xlCategory。尝试其他列举之一,直到您的代码工作。
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.
我还没有完全搞清楚Excel在win32com,但我设法得到轴标题后出现一点试验和错误。这里有一段代码,我写了一个XY散点图,标题为X轴,Y轴和Y2轴:
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)"
这篇关于使用python win32com在excel 2007图表中更改轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!