Python pptx (Power Point) 查找和替换文本 (ctrl + H) [英] Python pptx (Power Point) Find and replace text (ctrl + H)

查看:112
本文介绍了Python pptx (Power Point) 查找和替换文本 (ctrl + H)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之问题:如何使用 Python-pptx 模块使用查找和替换选项 (Ctrl+H)?

Question in Short: How can I use the find and replace option (Ctrl+H) using the Python-pptx module?

示例代码:

from pptx import Presentation

nameOfFile = "NewPowerPoint.pptx" #Replace this with: path name on your computer + name of the new file.

def open_PowerPoint_Presentation(oldFileName, newFileName):
    prs = Presentation(oldFileName)

    prs.save(newFileName)
open_PowerPoint_Presentation('Template.pptx', nameOfFile)

我有一个名为Template.pptx"的 Power Point 文档.在我的 Python 程序中,我添加了一些幻灯片并在其中放置了一些图片.将所有图片放入文档后,它会将其另存为另一个 Power Point 演示文稿.

I have a Power Point document named "Template.pptx". With my Python program I am adding some slides and putting some pictures in them. Once all the pictures are put into the document it saves it as another power point presentation.

问题是这个Template.pptx"包含所有旧的周数,比如第 20 周".我想让 Python 找到并将所有这些单词组合替换为第 25 周"(例如).

The problem is that this "Template.pptx" has all the old week numbers in it, Like "Week 20". I want to make Python find and replace all these word combinations to "Week 25" (for example).

推荐答案

您必须访问每个形状的每张幻灯片,并使用可用的文本功能查找匹配项.这可能不太好,因为 PowerPoint 有将运行分成看起来像奇怪的块的习惯.它这样做是为了支持拼写检查等功能,但它的行为是不可预测的.

You would have to visit each slide on each shape and look for a match using the available text features. It might not be pretty because PowerPoint has a habit of splitting runs up into what may seem like odd chunks. It does this to support features like spell checking and so forth, but its behavior there is unpredictable.

因此,使用 Shape.text 之类的内容查找事件可能很容易.根据您的具体情况,在不丢失任何字体格式的情况下替换它们可能会更困难.

So finding the occurrences with things like Shape.text will probably be the easy part. Replacing them without losing any font formatting they have might be more difficult, depending on the particulars of your situation.

这篇关于Python pptx (Power Point) 查找和替换文本 (ctrl + H)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆