0

I am trying to change the PPT bar chart series data and for that I am changing the xml of PPT using Python.

I am iterating over chart series and I am able to change these data Something like below.

ser.xpath('./c:cat//c:pt/c:v')[1].text = "Some Category"
ser.xpath('./c:cat//c:pt/c:v')[2].text = "Cat 3"
ser.xpath('./c:cat//c:pt/c:v')[3].text = "cat 4"

But problem is when I click on +(Edit) button in PPT files it again comes to it default chart and all data overwritten by python program get vanished.

Can anyone tell how to save a xml of PPT using python in this case.

Does xpath has save method too ?

How can we reproduce this problem ?

Open a ppt and select a bar chart by default and save it.

Then read the slide using Python-pptx install using pip install python-pptx

    from pptx import Presentation
    prs =  Presentation('file.pptx')

    fifteen = prs.slides[0]
    for shape15 in fifteen.shapes:
        try:
            print shape15.placeholder_format
        except:
            if shape15.has_chart:
                chart = shape15.chart
                for cnt, ser in enumerate(chart._chartSpace.xpath('//c:ser')): 
                        ser.xpath('./c:cat//c:pt/c:v')[1].text = "Some other 889cat"
                        ser.xpath('./c:cat//c:pt/c:v')[2].text = "Cat 3"
                        ser.xpath('./c:cat//c:pt/c:v')[3].text = "cat 4"

When I open the file

enter image description here

When I click on the filter

enter image description here

David Zemens
  • 53,033
  • 11
  • 81
  • 130
J Doe
  • 112
  • 7
  • How can we reproduce this problem? Please provide a [mcve]. – mzjn Jul 09 '18 at 12:04
  • Check now if you want to reproduce it – J Doe Jul 09 '18 at 12:15
  • `Presentation` has a `save` method: https://python-pptx.readthedocs.io/en/latest/user/presentations.html. Aren't you using that? – mzjn Jul 09 '18 at 13:12
  • Yeah I am trying that too – J Doe Jul 09 '18 at 13:44
  • Why are you using `xpath` instead of the more exposed methods to manipulate chart objects? Have you tried simply creating a new `cd = ChartCategoryData()` object, modifying that with the appropriate series & category data, etc., and then use the [`chart.replace_data`](http://python-pptx.readthedocs.io/en/latest/api/chart.html#pptx.chart.chart.Chart.replace_data) method? – David Zemens Jul 12 '18 at 15:56
  • 1
    @DavidZemens because python-pptx does not support combination chart – J Doe Jul 13 '18 at 05:59
  • This is just a guess, but perhaps the labels/etc are still "linked to source" (the underlying Excel worksheet). It seems likely to me that, changing series names without also changing the underlying Excel (or breaking the "link") might be overwritten intentionally in this scenario. – David Zemens Jul 13 '18 at 16:49

0 Answers0