现在,我必须在PowerPoint中与它们工作所在的区域相对应的名称旁边添加各种图像/图标。我找到code来查找和替换文本,但是我想在整个PowerPoint中查找文本(例如“健康”,“水”等),并替换为相应的图像。有直接的方法吗?下面的代码用ReplaceList字符串替换'Health':
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)
也许这样的东西是您想要的,尽管我不确定您要相对于text(box)放置图像的位置。您也可以使用width
控制height
和add_picture
。
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')