Python pptx (Power Point) 查找和替换文本 (ctrl + H)

Mor*_*nis 2 python powerpoint python-2.7 python-pptx

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

示例代码:

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)
Run Code Online (Sandbox Code Playgroud)

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

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

Ric*_*nce 9

对于那些只想将一些代码复制并粘贴到程序中以查找并替换 PowerPoint 中的文本同时保持格式的人(就像我一样),请执行以下操作:

def search_and_replace(search_str, repl_str, input, output):
    """"search and replace text in PowerPoint while preserving formatting"""
    #Useful Links ;)
    #/sf/ask/2654736591/
    #/sf/ask/3167292971/
    from pptx import Presentation
    prs = Presentation(input)
    for slide in prs.slides:
        for shape in slide.shapes:
            if shape.has_text_frame:
                if(shape.text.find(search_str))!=-1:
                    text_frame = shape.text_frame
                    cur_text = text_frame.paragraphs[0].runs[0].text
                    new_text = cur_text.replace(str(search_str), str(repl_str))
                    text_frame.paragraphs[0].runs[0].text = new_text
    prs.save(output)
Run Code Online (Sandbox Code Playgroud)

先验是许多答案的组合,但它完成了工作。它只是在每次出现 时替换search_str为。repl_strsearch_str

在此答案的范围内,您将使用: search_and_replace('Week 20', 'Week 25', "Template.pptx", "NewPowerPoint.pptx")


Sam*_*way 6

从我自己的项目中发布代码,因为其他答案都没有成功地使用具有多个段落的复杂文本的字符串而不会丢失格式:

prs = Presentation('blah.pptx')

# To get shapes in your slides
slides = [slide for slide in prs.slides]
shapes = []
for slide in slides:
    for shape in slide.shapes:
        shapes.append(shape)

def replace_text(self, replacements: dict, shapes: List):
    """Takes dict of {match: replacement, ... } and replaces all matches.
    Currently not implemented for charts or graphics.
    """
    for shape in shapes:
        for match, replacement in replacements.items():
            if shape.has_text_frame:
                if (shape.text.find(match)) != -1:
                    text_frame = shape.text_frame
                    for paragraph in text_frame.paragraphs:
                        for run in paragraph.runs:
                            cur_text = run.text
                            new_text = cur_text.replace(str(match), str(replacement))
                            run.text = new_text
            if shape.has_table:
                for row in shape.table.rows:
                    for cell in row.cells:
                        if match in cell.text:
                            new_text = cell.text.replace(match, replacement)
                            cell.text = new_text

replace_text({'string to replace': 'replacement text'}, shapes) 
Run Code Online (Sandbox Code Playgroud)