python-pptx替换ppt模板的文本

我要做的就是:利用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")
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虚坏叔叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值