# encoding = utf-8
from time import sleep
import win32com
def powerpoint(name):
# 生成了一个COM对象
ppt = win32com.client.Dispatch('PowerPoint.Application')
pres = ppt.Presentations.Add()
ppt.Visible = True
sleep(1)
# 对Powerpoint内容的操作
s1 = pres.Slides.Add(1, 1)
s1_0 = s1.Shapes[0].TextFrame.TextRange
s1_0.Text = u'尊敬的%s \n' % name
s1_1 = s1.Shapes[1].TextFrame.TextRange
s1_1.InsertAfter(u' 诚邀您参加于下周五晚六点在公司举行的晚会。')
# 保存文件
filename = name+'.pptx'
pres.SaveAs(filename)
# 关闭程序
pres.Close()
ppt.Application.Quit()
if __name__ == '__main__':
names = ['Alice', 'Bob', 'Eve']
for name in names:
powerpoint(name)
在python 3.8.6中出错
错误信息:
module 'win32com' has no attribute 'client'
在头部加入:
from win32com import client
错误信息:
line 168, in powerpoint ppt.Application.Quit()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\win32com\client\dynamic.py", line 638, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: PowerPoint.Application.Application
注释掉Quit()
#ppt.Application.Quit()
运行结果: