首页 > 解决方案 > xpath Python:系列数据不会永久保存在PPT中

问题描述

我正在尝试更改 PPT 条形图系列数据,为此我正在使用 Python 更改 PPT 的 xml。

我正在迭代图表系列,并且能够更改这些数据,如下所示。

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"

但问题是当我点击 PPT 文件中的 +(Edit) 按钮时,它又回到了默认图表,所有被 python 程序覆盖的数据都消失了。

在这种情况下,谁能告诉如何使用 python 保存 PPT 的 xml。

xpath 也有保存方法吗?

我们怎样才能重现这个问题?

打开一个ppt,默认选择一个条形图并保存。

Python-pptx 然后使用 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"

当我打开文件

在此处输入图像描述

当我点击过滤器时

在此处输入图像描述

标签: pythonxmlpowerpointelementtreepython-pptx

解决方案


推荐阅读