距離運營人員提出PPT,word自動轉圖片沒多久後。有天運營跑過來說道,能不能自動生成封面圖,以及視頻。因為每次手動一個一個打開PPT再用islide導出封面圖工作很繁瑣,效率也不高。有了上次寫PPT,word自動轉圖片的慄子,這次寫起來也十分順手了。
本文python版本3.9.5需windows 平臺,需安裝Microsoft Office
腳本思路python讀取目錄下的PPT->打開PPT->導出圖片->拿到圖片拼接製作封面圖。先來看看運營要求的封面圖,讀取PPT的前7張,第一張寬度鋪滿,第二張開始等比分配
拿到PPT集合def getPpt():
files = os.listdir("./"+pptFilePath)
return files
刪除文件夾def removeFileInFirstDir(targetDir):
for file in os.listdir(targetDir):
targetFile = os.path.join(targetDir, file)
if os.path.isfile(targetFile):
os.remove(targetFile)
PPT轉圖片順便把視頻也轉出來def ppt2png(ppt_path,powerpoint,fileName):
try:
ppt = powerpoint.Presentations.Open(ppt_path)
#保存為圖片
img_path = os.path.abspath(imgFilePath + '.png')
ppt.SaveAs(img_path, 18) # 17保存為jpg格式
# quality:0-100. The level of quality of the slide. The higher the number, the higher the quality.
quality = 60
# resolution:The resolution of the slide. 480,720,1080...
resolution = 720
# frames: The number of frames per second.
frames = 24
mp4_target = os.path.abspath('./ppt/'+fileName+'.mp4')
print(mp4_target)
print(fileName,"----正在轉成視頻")
ppt.CreateVideo(mp4_target,-1,1,resolution,frames,quality)
while True:
try:
time.sleep(0.1)
if os.path.exists(mp4_target) and os.path.getsize(mp4_target) == 0:
# The filesize is 0 bytes when convert do not complete.
continue
break
except Exception as e:
print (e)
break
# 關閉打開的ppt文件
ppt.Close()
except IOError:
print('PPT轉png失敗',ppt_path)
else:
print('\n')
print('\n')
print("PPT轉png成功",ppt_path)
圖片拼接成封面圖def getCover(fileName):
# 封面圖 w 1012 h 1431 空白6
w = 1012
h = 1431
padding = 6
toImage = Image.new('RGB',(w,h),"#E6F5FF") # #E6F5FF填充背景
files = os.listdir("./"+imgFilePath)
files.sort(key = lambda x : int(x.split('.')[0][3:])) #使用sort進行按順序讀取
for index,value in enumerate(files):
if(index <7 ):
_path = os.path.abspath('./img/'+value)
pic_fole_head = Image.open(_path)
# 獲取圖片的尺寸
if(index==0):
#第一張圖片寬度鋪滿 1012 - 6 -6 ,高度561
# 按照指定的尺寸,給圖片重新賦值,<PIL.Image.Image image mode=RGB size=200x200 at 0x127B7978>
tmppic = pic_fole_head.resize((w - padding * 2, 561))
# 計算每個圖片的左上角的坐標點(0, 0)
loc = (6,6)
toImage.paste(tmppic, loc)
else:
# 按照指定的尺寸,給圖片重新賦值,<PIL.Image.Image image mode=RGB size=200x200 at 0x127B7978>
# w = 1012
# h = 1431
# padding = 6
smallW = int((1012 - padding * 3 ) / 2 )
tmppic = pic_fole_head.resize((smallW, 280))
# 計算每個圖片的左上角的坐標點(0, 0),(200, 0),(200, 200)
line = math.ceil(index/2) #計算處於第幾行
x = 6
if(index % 2 ==0):
x = smallW + padding * 2
loc = (int(x ), int((line-1) * 280 + 561 + (line+1)* 6))
toImage.paste(tmppic, loc)
toImage.save("./ppt/"+fileName+'.png')
print(fileName+'封面生成成功')
初始化PPTdef init_powerpoint():
powerpoint = win32com.client.Dispatch('PowerPoint.Application') #comtypes.client.CreateObject("Powerpoint.Application")
powerpoint.Visible = 1
return powerpoint
最後調用if __name__=='__main__':
powerpoint = init_powerpoint()
pptArr = getPpt()
print('---pptArr--',pptArr)
for index,value in enumerate(pptArr):
if(('.ppt' in value) ==True):
removeFileInFirstDir('./'+imgFilePath)
_path = os.path.abspath('./ppt/'+value)
fileName = os.path.basename(_path).split('.')[0]
ppt2png(_path,powerpoint,fileName)
getCover(fileName)
time.sleep(2)
powerpoint.Quit()
# removeFileInFirstDir('./'+imgFilePath)
input("輸入任意鍵結束")運行後的封面圖及視頻
最後打包成exe文件給運營人員用就可以了pyinstaller -c -F -i a.ico index.py
結尾歡迎關注