2017-06-08 3 views
0

Avec Python win32com comment obtenir une référence à une table de données de graphique?Avec Python win32com comment obtenir une référence à une table de données de graphique?

Je peux créer un tableau avec une table de données (PowerPoint apparaît il dans une fenêtre séparée) comme:

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 ne fonctionne pas: AttributeError: « » objet n'a pas d'attribut « ChartData » Alors, comment obtenez-vous une référence à la table que crée PowerPoint? Ou comment définir la table à utiliser pour mes données?

Répondre

1

J'ai eu la même question tout à l'heure et j'ai passé beaucoup de temps à essayer de trouver une réponse.

ChartData est une propriété de l'objet Chart. Donc, pour accéder à l'objet ChartData, vous devez indiquer à PowerPoint que la forme que vous venez d'ajouter est un graphique.

Voici deux façons de le faire.

# 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'