我要做的就是:利用python自动控制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啦qqqqqq'
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('D:\自动化\ss.pptx')
ppt_catch_format_text(r"D:\自动化\课件.pptx")