python - 使用 python win32com 更改 excel 2007 图表中的轴标签

标签 python charts excel-2007 python-2.6 win32com

我有一个 Excel 工作表,前两列中有一些数据。我用这些数据创建了一个简单的图表。我在向图表添加轴标签时遇到问题。

这是我的脚本

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()

------到目前为止一切都很好。

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

但是当我添加轴标签时,它返回属性错误 xlCategory。

如何添加轴标签并更改字体大小。

提前致谢。

最佳答案

您可能使用了错误的枚举轴类型。每个枚举(据我所知)仅适用于某些类型的图表。根据内置的宏记录器(顺便说一句,即使对于基于 python 的脚本也非常有用),散点图使用 xlValue,而不是 xlCategory。试试one of the other enums until your code works .

我还没有完全弄清楚 win32com 中的 Excel,但经过一番尝试和错误后,我设法让轴标题出现。以下是我为 XY 散点图编写的一些代码的简短片段,其中包含 X 轴、Y 轴和 Y2 轴的标题:

    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 - 使用 python win32com 更改 excel 2007 图表中的轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17166351/

相关文章:

ms-access - VBA 文件打开速度很慢

python - 使用 Scapy 解析数据包字节

python - TypeError : list indices must be integers, 不是 str 与 xmltodict:

python - 如何将数百万个文件移动到 Azure Blob 存储中同一容器中的另一个文件?

javascript - DateFormat 在 Google Annotation Chart 上显示季度数字

javascript - Highcharts - Bar - 设置X轴宽度和图表区域宽度恒定

javascript - 通过绘图 API 渲染 Highcharts 数据表以将容器分隔为图表?

python - Pygame在子目录中找不到图像文件夹

debugging - 当前范围内的重复声明

excel - EPPlus 条形图条在 Excel 2013 中不显示负值的颜色,但在 Excel 2007 中工作正常