如何在python中编辑Xml文件后将其保存到pptx文件?
我想更改 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会那样做。我将加载输入 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.