python_指定的幻灯片中删除包含特定文本的文本框

python_指定的幻灯片中删除包含特定文本的文本框

from pptx import Presentation

def delete_textbox_with_content_in_slide(ppt_path, slide_index, content_list):
    
    # 打开一个现有的PPT文件
    presentation = Presentation(ppt_path)

    # 检查幻灯片索引是否有效
    if slide_index < 0 or slide_index >= len(presentation.slides):
        print("指定的幻灯片索引超出范围")
        return
    
    # 获取指定索引的幻灯片
    slide = presentation.slides[slide_index]
    
    # 获取幻灯片中的所有形状
    shapes = list(slide.shapes)
    
    # 遍历所有形状
    for shape in shapes:
        # 检查该形状是否是文本框
        if shape.has_text_frame:
            print("检测到文本框")
            # 获取文本框中的文本
            text = shape.text.strip()
            for content in content_list:
                # 如果文本匹配,删除该形状
                if content in text :
                    sp = shape._element
                    sp.getparent().remove(sp)
                    print(f"已删除包含内容 '{content}' 的文本框")

    # 保存修改后的PPT文件
    presentation.save(ppt_path)

ppt_path='D:\desktop\测试论文\PPT模板_结果_修改后1.pptx'
# 指定幻灯片索引和要删除的文本内容
slide_index = 1  # 假设我们要操作第一张幻灯片
content_list = ["标题占位","内容占位"]

# 调用函数删除指定幻灯片中包含特定内容的文本框
delete_textbox_with_content_in_slide(ppt_path, slide_index, content_list)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值