如何一键合并文件夹中(含子文件夹)所有PPT

工作中经常会有多人共同更新一个文档的情况,例如部门日报,可能部门几十个人都要去同一份文档更新,由于PPT不能设置多人同时编辑,难免会遇到冲突的情况,这时需要排队等待,如果有人忘记关闭,那后面的人更是无法更新。

解决办法是,每个项目更新在一个单独的PPT文档(可以分布在不同的子文件夹中),最后再编写一个小程序一键合并这些文档到一个总的文档。

更新:已增加排序功能,默认按文件夹-文件名排序,可重新选择排序。

import os
import time
import win32com.client as win32
from datetime import datetime
starttime = datetime.now()
def join_ppt():
    path = input("请输入文件夹路径,直接按回车默认合并当前文件夹:") or os.getcwd()
    path_files_list = []
    for root, dirs, files in os.walk(path):
        for file in files:
            if file.endswith(('ppt','pptx')) and len(file)>4 and file[3]=='-' and file[:3].isdigit():
                path_files_list.append([os.path.join(root, file),file,root,time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.path.getmtime(os.path.join(root, file))))])
    if path_files_list == []:
        return 'N1',len(path_files_list)
    
    path_files_list.sort(key = lambda x: (x[2],x[1]))#默认按文件夹-文件名排序
    new_name = os.path.join(path, path_files_list[0][1][4:])#默认保存到根文件夹,第一个文件名去除序号
    path_files = ['{}  ({}更新)'.format(x[0],x[3]) for x in path_files_list]
    print('即将合并的文件如下:(默认按文件夹-文件名排序)')
    print('\n'.join(path_files))
    merge_choice = input('请输入数字选择:(直接回车默认选择1)\n1.后台合并,\n2.前台合并,\n3.退出不合并,\n4.重新排序后合并\n')
#   如果用户选择了4,则重新排序并展示,用户可反复选择和展示,直到选择其它选项
    while merge_choice == '4':
        sort_choice = input('请输入合并方式:(直接回车默认选择1)\n1.按文件夹-文件名排序,\n2.按文件名排序(不论文件在哪个文件夹),\n3.按更新时间正序,\n4.按更新时间倒序\n')
        if sort_choice == '2':
            path_files_list.sort(key = lambda x: x[1])
            sort_choice_text = '按文件名排序(不论文件在哪个文件夹)'
        elif sort_choice == '3':
            path_files_list.sort(key = lambda x: x[3])
            sort_choice_text = '按更新时间正序'
        elif sort_choice == '4':
            path_files_list.sort(key = lambda x: x[3],reverse = True)
            sort_choice_text = '按更新时间倒序'
        else:
            sort_choice_text = '按文件夹-文件名排序'
        path_files = ['{}  ({}更新)'.format(x[0],x[3]) for x in path_files_list]
        print('即将合并的文件如下:({})'.format(sort_choice_text))
        print('\n'.join(path_files))
        merge_choice = input('请输入数字选择:(直接回车默认选择1)\n1.后台合并,\n2.前台合并,\n3.退出不合并,\n4.重新排序后合并\n')
    if merge_choice == '3': 
        return 'N2',len(path_files)#用户选择3即终止合并,否则程序继续进行
    withwindow = 1 if merge_choice == '2' else 0#根据选择,执行前台合并or后台合并
    global starttime 
    starttime = datetime.now()
    Application = win32.gencache.EnsureDispatch("PowerPoint.Application")
#     Application.Visible = False
    new_ppt = Application.Presentations.Add(WithWindow=withwindow)
#   执行合并操作
    for file in path_files_list:
        file = file[0]
        exit_ppt = Application.Presentations.Open(file,ReadOnly=1,WithWindow=0)
        print('正在操作的文件:', file)
        page_num = exit_ppt.Slides.Count
        exit_ppt.Close()
        new_ppt.Slides.InsertFromFile(file, new_ppt.Slides.Count, 1, page_num)
    try:
        new_ppt.SaveAs(new_name)  
    except:
        new_name = new_name.replace('.ppt','-{}.ppt'.format(starttime.strftime('%Y%m%d%H%M%S')))
        new_ppt.SaveAs(new_name) 
    if withwindow == 0:
        new_ppt.Close()
#    Application.Quit()
    return new_name,len(path_files_list)
print("程序功能:合并文件夹中所有已编码(***-开头)的PPT文件(含ppt,pptx)")
print("作者:Jogarys")
iscontinue = 'Y'
while iscontinue == 'Y':
    try:
        file_name,qty = join_ppt()
        endtime = datetime.now()
        if file_name == 'N1':
            print('没有文件需要合并')
        elif file_name == 'N2':
            print('未执行合并操作')
        else:
            print('已完成{}个ppt文件合并,已生成新文件:{},用时{}秒'.format(qty,file_name,(endtime-starttime).seconds))
    except Exception as ex:
        print('发生错误,错误类型:{}'.format(ex))
    iscontinue = input('请按回车退出,或输入Y继续合并...').upper()
    

打包成exe文件后(打包方法参考https://blog.csdn.net/weixin_46156257/article/details/130885686?spm=1001.2014.3001.5501),将exe文件放到需要合并的文件夹中,如下

 运行merge_ppt.exe后效果如下(均选择默认操作:即直接回车):

  合并后生成了新文件:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值