简体   繁体   English

PPTX 模块 - 遍历段落以查找/替换文本但仅替换第一个实例

[英]PPTX module - looping through paragraphs to find/replace text but only replaces 1st instance

I've been stuck on this for a while.我已经坚持了一段时间。 I'm using a find and replace function to loop through shape objects in Powerpoint using pptx module.我正在使用查找和替换 function 来使用 pptx 模块循环遍历 Powerpoint 中的形状对象。 When I call this function, it only replaces the 1st occurrence of the text (which happens to be the title).当我调用这个 function 时,它只替换了第一次出现的文本(恰好是标题)。 I suspect the issue is this current code ONLY replaces the first occurrence of the text or won't parse through the entire shape object and is looking for shapes that contain ONLY the full searchable text.我怀疑问题是当前代码仅替换了第一次出现的文本,或者不会解析整个形状 object 并且正在寻找仅包含完整可搜索文本的形状。

Code is found here: `代码可在此处找到:`

def search_and_replace(search_str, repl_str, input):
    """"search and replace text in PowerPoint while preserving formatting"""
    prs = Presentation(input)
    for slide in prs.slides:
        for shape in slide.placeholders:
            print('%d %s' % (shape.placeholder_format.idx, shape.name))
            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(input)

` `

I tried printing the shape objects and it looks like they are showing up properly.我尝试打印形状对象,看起来它们显示正确。

Don't forget text frames can have multiple paragraphs and runs不要忘记文本框可以有多个段落和运行

for paragraph in shape.text_frame.paragraphs:
    for run in paragraph.runs:

I made my own module to replace text and images using pythonpptx.我制作了自己的模块来使用 pythonpptx 替换文本和图像。 It will do the hard part of replacing text runs for you: https://pypi.org/project/templatepptx/它将为您完成替换文本运行的困难部分: https://pypi.org/project/templatepptx/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python for 循环没有遍历所有元素? 它只取第一个元素 - Python for loop not looping through all the elements? It is only taking the 1st element 仅替换替换第一个参数 - Replace only replacing the 1st argument 如何仅用逗号前第一个出现的单词替换 dataframe 文本列 - How to replace dataframe text column with only the 1st occuring word / words before a comma 需要用 open & for 循环替换特定的行(文本中的第一行) - Need to replace specific line (1st line in the text) in with open & for loop Python pptx (Power Point) 查找和替换文本 (ctrl + H) - Python pptx (Power Point) Find and replace text (ctrl + H) Python-pptx,查找特殊文本并替换为图片 - Python-pptx, find special text and replace with image 仅保留每行中的第一个非空值(并用 NaN 替换其他值) - Keep only the 1st non-null value in each row (and replace others with NaN) Pandas替换DataFrame中的第一个结果 - Pandas Replace 1st Result in a DataFrame 如何根据字符串的 id 从给定的文本文件中提取字符串的第一个、第二个和最后一个实例? - How to extract 1st, 2nd and last instance of a string from a given text file based on their ids? Python模块CiscoConfParse仅返回接口上的第一个IPv6地址 - Python Module CiscoConfParse returns only the 1st IPv6 address on an interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM