【问题标题】:Use python to find text in PowerPoint and replace with image?使用python在PowerPoint中查找文本并替换为图像?
【发布时间】:2019-09-24 21:40:46
【问题描述】:

现在我必须在 PowerPoint 中的名称旁边添加与它们工作的部门相对应的各种图像/图标。我找到code 来查找和替换文本,但我想在整个 powerpoint 中查找文本(例如“健康”、“水”等)并替换为相应的图像。有没有直接的方法可以做到这一点?下面的代码将“Health”替换为 ReplaceList 字符串:

from pptx import Presentation    
ppt = Presentation('Powerpoint.pptx')    
TextList = 'Health'
ReplaceList = r'C:\Users\E\Desktop\Icons\Health.png'

for slide in ppt.slides:
    for shape in slide.shapes:
        if shape.has_text_frame:
            shape.text = shape.text.replace(TextList,ReplaceList)

ppt.save('New_Powerpoint.pptx')

如果没有,有没有办法找到文本位置(左、上),然后使用下面的代码插入图像?

pic = slide.shapes.add_picture(image, left, top)

【问题讨论】:

    标签: python powerpoint python-pptx


    【解决方案1】:

    也许你正在寻找这样的东西,尽管我不确定你希望图像相对于文本(框)放置在哪里。您还可以使用add_picture 控制widthheight

    from pptx import Presentation    
    ppt = Presentation('Powerpoint.pptx')    
    search_str = 'Health'
    image_link = r'C:\Users\E\Desktop\Icons\Health.png'
    
    for slide in ppt.slides:
        for shape in slide.shapes:
            if shape.has_text_frame:
                if(shape.text.find(search_str))!=-1:
                    pic = slide.shapes.add_picture(image_link, shape.left, shape.top)
    
    ppt.save('NewPresentation.pptx')
    

    【讨论】:

      猜你喜欢
      • 2019-09-20
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 2011-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多