2017-06-08 58 views
0

用Python win32com如何獲得對圖表數據表的引用?用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的:「」對象沒有屬性「ChartData」

那麼,如何獲得PowerPoint創建的表的引用?或者,如何定義要用於我的數據的表格?

回答

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' 
相關問題