Search code examples
python-3.xchartspython-pptx

Python-pptx: How to update chart data for the second chart, if two chart are present on same slide


I want to update chart data for my existing powerpoint presentation slide, having 2 charts, namely a Bar Chart and a Pie Chart. I am successfully able to update the bar chart as it was first element on my content slide placeholder. Now I am not sure how to update the second chart i.e Pie chart on same slide. My code is like as below:

prs = Presentation("Request_Slide.pptx")
slide = prs.slides[0]
for shape in slide.placeholders:
     print('%d %s' %(shape.placeholder_format.idx, shape.name))

with above I got below response:

0 Title 1
1 Content Placeholder 5

With help of this i managed to navigate to my first chart on Slide[0] and updated its chart data however i am struggling to navigate to second chart present in content placeholder. Any suggestions would be of great help and thanks in advance.


Solution

  • Managed to resolve using listing the shape name index and using shape index to update chart data by using below code:

     list_shape_names = [shape.name for shape in slide.shapes]
    
        if shape_name not in list_shape_names:
            shape_index = None
        else:
            shape_index = list_shape_names.index(shape_name)
    

    But do take care while passing shape_name as it will be case sensitive.