思路
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
最终效果图
打开文件夹目录就是我们刚刚处理完毕的图片了