python 批量將PPT導出成圖片集的案例

導讀

需要使用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'); 

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀: