利用Python整理PPT

思路

在这里插入图片描述

1.编号

在这里插入图片描述
首先复制粘贴ppt所在位置,然后修改path,运行以下代码

import os

path='F:\example'  #ppt文件所在位置
count = 1
for file in os.listdir(path):
    if file.endswith('.pptx') or file.endswith('.ppt'):
        os.rename(os.path.join(path,file),os.path.join(path,str(count)+'.'+file))
        count+=1

运行完毕之后,PPT文件都自动前缀上序号了
在这里插入图片描述

2.导出图片

import comtypes.client
import time

def init_powerpoint():
    powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
    powerpoint.Visible = 1
    return powerpoint

def ppt_to_picture(powerpoint, inputFileName, formatType = 32):
    deck = powerpoint.Presentations.Open(inputFileName)
    deck.SaveAs(inputFileName.rsplit('.')[0] + '.jpg', 17)
    deck.Close()

def convert_files_in_folder(powerpoint, folder):
    files = os.listdir(folder)
    pptfiles = [f for f in files if f.endswith((".ppt", ".pptx"))]
    for pptfile in pptfiles:
        fullpath = os.path.join(path, pptfile)
        ppt_to_picture(powerpoint, fullpath, fullpath)

if __name__ == "__main__":
    powerpoint = init_powerpoint()
    convert_files_in_folder(powerpoint, path)
    powerpoint.Quit()

运行完毕之后,你就会发现该目录下多了几个文件夹,里面装着由ppt文件导出的图片,当然你也可以修改一下以上代码,就能实现自动导出pdf或其他类型文件
在这里插入图片描述
在这里插入图片描述

3.图片拼接

封面页放最顶端,其他ppt每4页为一行,最后拼接起来,非常直观,方便使用时快速浏览
在这里插入图片描述

from os import listdir
from PIL import Image

files = listdir(path)
for file in files:
    if file.endswith('.pptx')==0 and file.endswith('.ppt')==0:
        Path=path+'\\'+file
        ims=[Image.open(r'%s\%s'%(Path,fn)) for fn in listdir(r'%s'%Path) if fn.endswith('.JPG')]
        width,height=ims[0].size
        body=Image.new(ims[0].mode,(width*4,height*((len(ims)+3)//4)))
        for i in range(len(ims)+1):
            if i!=1:
                for fn in listdir(r'%s'%Path): 
                    if fn.endswith('幻灯片%d.JPG'%i):
                        im=Image.open(r'%s\%s'%(Path,fn))
                        body.paste(im,box=(((i-2)%4)*width,((i+2)//4-1)*height))          
        
        w,h=body.size
        header = ims[0].resize((width*4, height*4))
        result=Image.new(body.mode,(w,height*4+h))
        result.paste(header,box=(0,0))
        result.paste(body,box=(0,height*4))
        result.save('%s/%s.jpg'%(path,file))
        time.sleep(1)

4.汇总

完成以上工作之后,该目录下变得无比杂乱,各种类型文件都有,所以我们需要稍作整理一下,把文件夹放入一个文件夹——5(我放在倒数第一个文件夹里,都是些ppt导出的图片,基本上没什么用处了,最后我一般会手动删除掉);把图片放入另一个文件夹——“目录”
在这里插入图片描述

#收尾
import shutil

os.mkdir('%s/目录'%path)
for file in os.listdir(path) :
    if file.endswith('.jpg'):
        shutil.move('%s/%s'%(path,file),'%s/目录'%path)
        
    elif file.endswith('ppt')==0 and file.endswith('pptx')==0 and file.endswith('目录')==0 and file.endswith('草稿')==0:
        try:
            shutil.move('%s/%s'%(path,file),'%s/%s'%(path,str(count-1)))
        except:
            pass

最终效果图

打开文件夹目录就是我们刚刚处理完毕的图片了
在这里插入图片描述

  • 9
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值