python 批量将PPT导出成图片集的案例
作者:我的小英短 时间:2021-09-14 17:52:36
导读
需要使用python做一个将很多个不规则PPT导出成用文件夹归纳好的图片集,所以就需要使用comtypes调用本机电脑上的ppt软件,批量打开另存为多张图片
采坑
公司电脑使用comtypes完美导出图片,系统win10
回家后使用自己的电脑就报错,系统也是win10,最后没办法放弃comtypes采用win32com,最终成功
源代码
"""
该工具函数的功能:批量将PPT导出成图片
"""
import comtypes.client
import os
import win32com
import win32com.client
# base path
BASH_PATH = 'C:\\web\\python\\tool\\formatPPTtaobao\\ppts\\'
# render jpg
RENDER_BASE_PATH = 'C:\\web\\python\\tool\\formatPPTtaobao\\render\\'
# 初始化PPT
def init_powerpoint():
powerpoint = win32com.client.Dispatch('PowerPoint.Application') #comtypes.client.CreateObject("Powerpoint.Application")
powerpoint.Visible = 1
return powerpoint
# PPT TO PDF
def ppt_to_pdf_or_jpg(powerpoint, inputFileName, outputFileName, formatType = 32):
if outputFileName[-3:] != 'pdf':
outputFileName = outputFileName[0:-4] + ".pdf"
deck = powerpoint.Presentations.Open(inputFileName)
#deck.SaveAs(outputFileName, formatType) # formatType = 32 for ppt to pdf
deck.SaveAs(inputFileName.rsplit('.')[0] + '.jpg', 17)
deck.Close()
# save file as a folder
def convert_files_in_folder(powerpoint, folder):
files = os.listdir(folder)
pptfiles = [f for f in files if f.endswith((".ppt", ".pptx"))]
print(pptfiles)
for pptfile in pptfiles:
fullpath = os.path.join(cwd, pptfile)
ppt_to_pdf_or_jpg(powerpoint, fullpath, fullpath)
# delete more ppt and select 20
def delete_more_ppt(folder):
files = os.listdir(folder)
allNum = len(files)
endPoint = 0
if(allNum <= 20):
return
else:
endPoint = allNum - 20
for f in files[0:endPoint]:
if(os.path.isfile(folder+f)):
os.unlink(folder+f)
# exec picture composite
def execPictureComposite():
os.system('python componsite_jpg_taobao.py')
pass
# delete render jpgs
def deleteRenderJpg():
jpgs = os.listdir(RENDER_BASE_PATH)
for jpg in jpgs:
os.unlink(RENDER_BASE_PATH+jpg)
if __name__ == "__main__":
cwd = BASH_PATH
delete_more_ppt(cwd)
deleteRenderJpg()
powerpoint = init_powerpoint()
convert_files_in_folder(powerpoint, cwd)
powerpoint.Quit()
execPictureComposite()
补充:用python处理ppt中的文字
我就废话不多说了,大家还是直接看代码吧~
from pptx import Presentation
from pptx.util import Inches,Pt
ppt=Presentation()
#在ppt中加入一个幻灯片
slide=ppt.slides.add_slide(ppt.slide_layouts[1])
#第一种
body_shape=slide.shapes.placeholders
body_shape[0].text='这是占位符[0]'
body_shape[1].text='这是占位符[1]'
#第二种
title_shape=slide.shapes.title
title_shape.text='这里是标题'
#取出本页第二个文本框
subtitle=slide.shapes.placeholders[1]
#在第二个文本框里写入汉字
subtitle.text='这里是文本框'
#在文本框里添加一个段落
new_paragraph=body_shape[1].text_frame.add_paragraph()
new_paragraph.text='新段落'
new_paragraph.font.bold=True
new_paragraph.font.italic=True
new_paragraph.font.size=Pt(15)
new_paragraph.font.underline=True
#添加文本框
left=Inches(2)
top=Inches(2)
width=Inches(3)
height=Inches(3)
textbox=slide.shapes.add_textbox(left,top,width.height)
textbox.text="这是新文本框"
#在文本框里添加新段落
new_par=textbox.text_frame.add_paragraph()
new_par.text='这是文本框里的第二段'
ppt.save('test.pptx');
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://damiao.blog.csdn.net/article/details/88813632
标签:python,PPT,导出,图片集
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
透明度设置
2009-12-12 18:51:00
美化段落文本 1
2008-05-15 12:24:00
![](https://img.aspxhome.com/file/UploadPic/20085/15/2008515123035529s.jpg)
Python爬虫天气预报实例详解(小白入门)
2023-03-23 16:05:48
![](https://img.aspxhome.com/file/2023/5/77565_0s.jpg)
获取星期几的名称
2012-01-29 18:16:31
ASP用JAVASCRIPT脚本实现分页的办法
2007-10-30 13:18:00
PHP基于phpqrcode生成带LOGO图像的二维码实例
2023-11-23 23:47:18
技术性击倒与抬杠
2009-02-12 12:28:00
运用ASP调用数据库中视图及存储过程
2008-02-03 15:33:00
PHP实现向关联数组指定的Key之前插入元素的方法
2023-07-14 08:41:33
debug case: onsubmit=return false依然能提交
2009-01-18 13:07:00
Python中import机制详解
2023-08-25 10:30:29
基于JS实现html中placeholder属性提示文字效果示例
2023-09-07 22:50:58
![](https://img.aspxhome.com/file/2023/0/56130_0s.gif)
XPath 1.0 学习笔记
2009-02-25 12:19:00
Python3如何在服务器打印资产信息
2023-11-15 08:03:45
![](https://img.aspxhome.com/file/2023/5/63545_0s.png)
Python 迭代器Iterator详情
2021-10-12 07:01:42
简单的文本内容处理工具
2010-01-28 12:31:00
浅谈Python3实现两个矩形的交并比(IoU)
2021-09-06 19:31:56
![](https://img.aspxhome.com/file/2023/7/67247_0s.jpg)
MySQL列类型的选择与MySQL的查询效率
2009-01-14 12:53:00
如何实现My SQL中的用户的管理问题
2008-12-03 13:56:00
Python 学习笔记
2022-06-18 07:12:31