python中使用python-ppt库操作ppt来替换文本内容,包括图片在前方的
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE
def ppt_catch_format_text(filename):
"""
抓取PPT的内容,按段落返回
其中 filename 是PPT文件的路径
"""
search_str = '我是'
repl_str = 'PPT啦'
prs = Presentation(filename)
for x in range(len(prs.slides)):
# ---Only on text-boxes outside group elements---
for shape in prs.slides[x].shapes:
if hasattr(shape, "text"):
if(shape.text.find(search_str))!=-1:
text_frame = shape.text_frame
cur_texts = text_frame.paragraphs[0].runs
for index in range(len(cur_texts)):
print(text_frame.paragraphs[0].runs[index].text)
if(cur_texts[index].text.find(search_str))!=-1:
#print(5566)
#print(cur_texts[index].text)
cur_text = text_frame.paragraphs[0].runs[index].text
new_text = cur_text.replace(str(search_str), str(repl_str))
text_frame.paragraphs[0].runs[index].text = new_text
# ---Only operate on group shapes---
group_shapes = [shp for shp in prs.slides[x].shapes
if shp.shape_type ==MSO_SHAPE_TYPE.GROUP]
#print(group_shapes)
for group_shape in group_shapes:
for shape in group_shape.shapes:
if shape.has_text_frame:
if(shape.text.find(search_str))!=-1:
text_frame = shape.text_frame
# cur_texts = text_frame.paragraphs[0].runs
for index in range(len(text_frame.paragraphs)):
cur_text = text_frame.paragraphs[index].text
#print(cur_texts[index].text.encode('utf-8').strip().decode())
if(cur_text.find(search_str))!=-1:
print(7788)
#print(cur_texts[index].text)
new_text = cur_text.replace(str(search_str), str(repl_str))
text_frame.paragraphs[index].text = new_text
#print(cur_text)
#prs.save('ss.pptx')
ppt_catch_format_text('s.pptx')