python 批量将PPT导出成图片集的案例
导读
需要使用python做一个将很多个不规则PPT导出成用文件夹归纳好的图片集,所以就需要使用comtypes调用本机电脑上的ppt软件,批量打开另存为多张图片
采坑
公司电脑使用comtypes完美导出图片,系统win10
回家后使用自己的电脑就报错,系统也是win10,最后没办法放弃comtypes采用win32com,最终成功
源代码
"""
	该工具函数的功能:批量将PPT导出成图片
"""
importcomtypes.client
importos
importwin32com
importwin32com.client
#basepath
BASH_PATH='C:\\web\\python\\tool\\formatPPTtaobao\\ppts\\'
#renderjpg
RENDER_BASE_PATH='C:\\web\\python\\tool\\formatPPTtaobao\\render\\'
#初始化PPT
definit_powerpoint():
powerpoint=win32com.client.Dispatch('PowerPoint.Application')#comtypes.client.CreateObject("Powerpoint.Application")
powerpoint.Visible=1
returnpowerpoint
#PPTTOPDF
defppt_to_pdf_or_jpg(powerpoint,inputFileName,outputFileName,formatType=32):
ifoutputFileName[-3:]!='pdf':
outputFileName=outputFileName[0:-4]+".pdf"
deck=powerpoint.Presentations.Open(inputFileName)
#deck.SaveAs(outputFileName,formatType)#formatType=32forppttopdf
deck.SaveAs(inputFileName.rsplit('.')[0]+'.jpg',17)
deck.Close()
#savefileasafolder
defconvert_files_in_folder(powerpoint,folder):
files=os.listdir(folder)
pptfiles=[fforfinfilesiff.endswith((".ppt",".pptx"))]
print(pptfiles)
forpptfileinpptfiles:
fullpath=os.path.join(cwd,pptfile)
ppt_to_pdf_or_jpg(powerpoint,fullpath,fullpath)
#deletemorepptandselect20
defdelete_more_ppt(folder):
	files=os.listdir(folder)
	allNum=len(files)
	endPoint=0
	if(allNum<=20):
		return
	else:
		endPoint=allNum-20
	forfinfiles[0:endPoint]:
		if(os.path.isfile(folder+f)):
			os.unlink(folder+f)
#execpicturecomposite
defexecPictureComposite():
	os.system('pythoncomponsite_jpg_taobao.py')
	pass
#deleterenderjpgs
defdeleteRenderJpg():
jpgs=os.listdir(RENDER_BASE_PATH)
forjpginjpgs:
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中的文字
我就废话不多说了,大家还是直接看代码吧~
frompptximportPresentation
frompptx.utilimportInches,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');
以上为个人经验,希望能给大家一个参考,也希望大家多多支持毛票票。如有错误或未考虑完全的地方,望不吝赐教。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。