如何在python中编辑Xml文件后将其保存到pptx文件?

发布于 2025-01-13 20:09:02 字数 449 浏览 5 评论 0原文

我想更改 pptx 文件中的一些值。为此,我已将 pptx 文件转换为 xml 文件,并使用以下代码更改了值。但我不知道如何将这个新文件另存为 pptx 文件。我可以将其保存为 xml 文件,但不能保存为 pptx。我使用的是Python 3.10版本


with open('filename.xml','r') as f:
     a = f.read()
     f.closed
if 'value' in a:
     a = a.replace('value', 'newvalue')
print('success')
if 'newvalue' in a:
     print('done')
with open(filename.xml','w') as file:
     print('this works')
     file.write(a)
     print('this worked')

I want to change some values in pptx file. For that I have converted the pptx file into xml file and changed the value using below code. But I don't know how can I save this new file as an pptx file. I can save it as an xml file but not as pptx. I am using python version 3.10


with open('filename.xml','r') as f:
     a = f.read()
     f.closed
if 'value' in a:
     a = a.replace('value', 'newvalue')
print('success')
if 'newvalue' in a:
     print('done')
with open(filename.xml','w') as file:
     print('this works')
     file.write(a)
     print('this worked')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凤舞天涯 2025-01-20 20:09:02

我不会那样做。我将加载输入 pptx 并使用 python-pptx 对其应用转换。然后写出生成的pptx。

在我看来,你的例子是在进行文本替换。这可以通过使用 python-pptx 迭代幻灯片中的形状对象来完成。

I wouldn't do that. I would load the input pptx and apply transforms - using python-pptx - to that. Then write out the resulting pptx.

Your example seemed to me to be doing text replacement. That can be done by iterating over the shape objects within the slides - with python-pptx.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文