From 12c3233beae9923359c6d0d7c9f3098185b37217 Mon Sep 17 00:00:00 2001 From: lichaojun Date: Thu, 3 Oct 2024 22:39:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20python=20=E5=AE=9E=E7=8E=B0=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E5=88=A0=E9=99=A4=E6=9F=90=E4=B8=80=E4=B8=AA=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9=E4=B8=8B=E6=89=80=E6=9C=89ppt=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E6=9C=80=E5=90=8E=E4=B8=80=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- py-src/del_ppt.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 py-src/del_ppt.py diff --git a/py-src/del_ppt.py b/py-src/del_ppt.py new file mode 100644 index 0000000..a650cb7 --- /dev/null +++ b/py-src/del_ppt.py @@ -0,0 +1,49 @@ +import os +import shutil +from pptx import Presentation +from pptx.util import Inches +from PIL import Image + +# 指定包含PPT文件的文件夹 +folder_path = 'C:/Users/Administrator/Desktop/测试' + + +def del_slide(presentation, slide_index): + """ + 删除某一张幻灯片并返回该幻灯片对象 + :param presentation: Presentation 对象 + :param slide_index: 索引 + :return: 被删除的幻灯片对象 + """ + + slides = list(presentation.slides._sldIdLst) + slide_to_delete = slides[slide_index] + presentation.slides._sldIdLst.remove(slide_to_delete) + return slide_to_delete # 返回被删除的幻灯片对象 + +def delete_last_slide_from_ppt(ppt_path): + """ + 从PPT中删除最后一张幻灯片 + :param ppt_path: PPT文件路径 + """ + prs = Presentation(ppt_path) + last_slide_index = len(prs.slides) - 1 + if last_slide_index >= 0: + del_slide(prs, last_slide_index) + prs.save(ppt_path) + print(f"已删除最后一张幻灯片: {ppt_path}") + else: + print(f"pptx文件中没有幻灯片: {ppt_path}") + +def batch_delete_last_slides(directory): + """ + 批量删除每个PPT的最后一张幻灯片 + :param directory: 文件夹路径 + """ + for filename in os.listdir(directory): + if filename.endswith('.pptx') or filename.endswith('.ppt'): + ppt_path = os.path.join(directory, filename) + delete_last_slide_from_ppt(ppt_path) + +# 调用批量删除函数 +batch_delete_last_slides(folder_path) \ No newline at end of file