python-pptx处理替换文本

 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')

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值