继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

【九月打卡】第11天 Python办公自动化

ninjaes
关注TA
已关注
手记 23
粉丝 0
获赞 4

课程名称Python办公自动化

课程章节:第6章 玩转PPT自动化

课程讲师: 离岛


课程内容

python操作PPT的主要场景如下

http://img4.sycdn.imooc.com/63248ca60001d62009570549.jpg


需要安装python-pptx 第三方模块, 用于自动化生成和更新PowerPoint文件


安装 pin install python-pptx


导入 import pptx


http://img1.sycdn.imooc.com/63248ecc0001ba7904260431.jpg

import pptx

prs = pptx.Presentation() #这里也可以入参,表示打开的ppt文件
prs.slides.add_slide(prs.slide_layouts[0])
prs.slides.add_slide(prs.slide_layouts[1])
prs.slides.add_slide(prs.slide_layouts[2])
prs.save('test.pptx') #这里也可以是另存为其他名字的ppt文件

#删除幻灯片
print(len(prs.slides))
del prs.slides._sldIdLst[1]
print(len(prs.slides))

#添加文本框
text1=slide.shapes.add_textbox(Inches(5),Inches(5),Inches(5),Inches(5))
text1.text="我是文本框"
p1=text1.text_frame.add_paragraph()
p1.text='我是段落1'
p1.add_run().text='end'
title_shape=slide.shapes.title
title_shape.text='标题1'
slide.shapes.placeholders[1].text='标题2'


添加图形到PPT

#添加自选图形
shape=slide.shapes.add_shape(MSO_SHAPE.HEXAGON,Inches(2),Inches(2),Inches(5),Inches(3))
#填充、边框
fill=shape.fill
fill.solid()
fill.fore_color.rgb=RGBColor(255,0,0)
line=shape.line
line.color.rgb=RGBColor(55,3,5)
line.width=Pt(2)


实现的结果如下

http://img1.sycdn.imooc.com/632493c500019a2309830500.jpg

课程收获

 从python的角度去操作PPT, 这样不仅效率更高而且更方便操作


打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP