栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python遍历子目录,利用python-pptx删除ppt文件最后一页

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python遍历子目录,利用python-pptx删除ppt文件最后一页

需求背景

美女同事给了一堆PPT文件,问我能否把快速最后一页删除?先告诉她,当然没问题,在仔细看任务内容,确实所有PPT课件最后一页是广告内容,人工删除还挺费事的,幸好有利器

安装必需组件

pip install python-pptx

顺利完成代码安装,如果你安装上有问题,请自行BD解决

实现逻辑源代码
'''
程序功能:Python遍历目录删除PPT最后一页

'''
from pptx import Presentation
import os


def list_dir_more(CurPath=os.getcwd(), file_list=[]):
    FileList = os.listdir(CurPath)
    # print FileList
    for File in FileList:
        SubPath = CurPath + '\' + File
        if os.path.isdir(SubPath):
            list_dir_more(SubPath, file_list)
        else:
            file_list.append(SubPath)
    
    return file_list
 
 
def del_ppt_lastpage(file):
    prs = Presentation(file)
    # 查看一共几页
    slides = prs.slides
    number_pages = len(slides)
    print('file={},总页数={}'.format(file,number_pages))
    rId = prs.slides._sldIdLst[-1].rId
    prs.part.drop_rel(rId)
    del prs.slides._sldIdLst[-1]
    # 保存ppt文件
    prs.save(file)
    
if __name__ =="__main__":
    file_list=list_dir_more('.PPT')
    count=1
    for file in file_list:
        try:
            if '.ppt' in file:
                print('正在处理第 {}/{} 个文件:{}'.format(count,len(file_list),file))
                del_ppt_lastpage(file)
            
        except BaseException as del_err:
            print('第 {}/{} 个文件:{} ,处理失败,提示信息:{}'.format(count, len(file_list), file,del_err))
 
        count += 1
        
        
执行结果 

 顺利完成任务,得到美女同事赞赏的目光!

Python真是自动化的利器啊,大量的成熟组件,助大家早日脱单啊,哈哈~~!

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/994869.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号