0

用python win32com如何获取图表数据表的引用?

我可以创建一个带有数据表的图表(PowerPoint 在单独的窗口中弹出它),例如:

    import win32com
    from MSO import constants as msoconst

    Application = win32com.client.Dispatch("PowerPoint.Application")
    Application.Visible = True
    Presentation = Application.Presentations.Add()

    FirstSlide = Presentation.Slides.Add(1, 12)

... no problem adding slides, shapes and text and setting font size and color ....

    InventoryChart = FirstSlide.Shapes.AddChart2(201,msoconst.xlColumnClustered ,10,80,470,220,False) # 0 = Clustered Column, 1 = Combo Area, 2 = Clustered Column
    InventoryChartData = InventoryChart.ChartData

ChartData 不起作用:AttributeError: '' object has no attribute 'ChartData'

那么,如何获得对 PowerPoint 创建的表格的引用?或者我如何定义用于我的数据的表?

4

1 回答 1

1

不久前我有同样的问题,并花了很多时间试图找到答案。

ChartData 是Chart对象的一个​​属性。因此,要访问 ChartData 对象,您需要告诉 PowerPoint 您刚刚添加的形状是一个图表。

这里有两种方法可以做到这一点。

# Option 1 - Add .Chart to end of AddChart2 method
InventoryChart = FirstSlide.Shapes.AddChart2(201,msoconst.xlColumnClustered,10,80,470,220,False).Chart

# Option 2 - Define Chart object separate from AddChart2
InventoryChart = FirstSlide.Shapes(1).Chart

# You can now access the chart's data worksheet
InventoryChartData = InventoryChart.ChartData.Workbook.Worksheets(1)

# Write a value to worksheet
InventoryChartData.Range('F1').Value = 150

# Apply numeric formatting to Series 1 values
InventoryChartData.Range('B2:B5').NumberFormat = '0.00'
于 2017-07-07T13:29:36.097 回答