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)