python自动化办公(3)_ppt自动化处理

一、ppt基本操作

ppt自动化流程如下:
在这里插入图片描述

使用的库:pptx

二、 实例演示

1、新建幻灯片,添加第一页

import pptx
from pptx.util import Inches,Cm,Pt
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE

##ppt的文档结构是pptx(ppt文件)-->slide(幻灯片页)-->shape(幻灯片中的元素:文本框/图片/形状/表格等)-->paragraph-->run
##1、新建幻灯片,添加第一页
prs =  Presentation() #初始化一个空的PPT文档
title_slide_layout = prs.slide_layouts[0]  #使用PPT自带的模板,常用48中模板,ppt布局0是主标题和副标题
slide=prs.slides.add_slide(title_slide_layout) #使用上述模板添加一张slide幻灯片
title=slide.shapes.title  #获取本业幻灯片的title元素
subtitle=slide.placeholders[1]  #placeholders占位符索引,获取一页幻灯片中的元素

#设置主标题和副标题
title.text='Hello World'
subtitle.text='python-pptx using test'

prs.save('python-pptx_test1.pptx')

以上代码运行完后,就生成一个最简单的PPT,见下图
在这里插入图片描述

2、添加幻灯片页,设置字体

##2、新增幻灯片页,编辑增加幻灯片中的元素
slide=prs.slides.add_slide(prs.slide_layouts[1])
body_shape=slide.shapes.placeholders #获取幻灯片中的元素

#body_shape[0].text='this is placeholders[0]'
body_shape[1].text='this is placeholders[1]'
title_shape=slide.shapes.title 
title_shape.text='this is title'

#设置文本框字体
new_paragraph=body_shape[1].text_frame.add_paragraph() #在第二个占位符添加新的段落
new_paragraph.text='add_paragraph'
new_paragraph.font.bold=True #文字加粗
new_paragraph.font.italic=True #文字斜体
new_paragraph.font.size=Pt(15) #文字大小
new_paragraph.font.underline=True #文字下划线

prs.save('python-pptx_test2.pptx')

在这里插入图片描述

3、添加新的文本框

#3、添加元素:文本框
left=top=width=height=Inches(5) #预设位置大小
textbox=slide.shapes.add_textbox(left,top,width,height)
textbox.text='this is a new textbox'
new_para=textbox.text_frame.add_paragraph() #在文本框中添加段落
new_para.text='this is second para in textbox'

prs.save('python-pptx_test3.pptx')

在这里插入图片描述

4、添加图片

#4、添加图片
img_path=r'C:\Users\lmy\Pictures\Saved Pictures\blog_qr.png' #图片路径
left,top,width,height=Inches(1),Inches(4.5),Inches(1.7),Inches(1.7) #预设位置大小
pic=slide.shapes.add_picture(img_path,left,top,width,height)

prs.save('python-pptx_test4.pptx')

在这里插入图片描述

5、添加形状

#5、添加形状
left,top,width,height=Inches(1),Inches(3),Inches(1.8),Inches(1) #预设位置大小
shape=slide.shapes.add_shape(MSO_SHAPE.PENTAGON,left,top,width,height)
shape.text='Step1'
for n in range(2,6):
    left=left+width-Inches(0.3)
    shape=slide.shapes.add_shape(MSO_SHAPE.CHEVRON,left,top,width,height)
    shape.text='Step{}'.format(n)
    
prs.save('python-pptx_test5.pptx')  

在这里插入图片描述

6、添加表格

slide=prs.slides.add_slide(prs.slide_layouts[6]) #新建一张幻灯片
rows,cols,left,top,width,height=2,4,Inches(2),Inches(1),Inches(6),Inches(0.8) #预设行列数、位置大小
table=slide.shapes.add_table(rows,cols,left,top,width,height).table #添加表格,并获取表格类
table.columns[0].width=Inches(1.5) #列宽
table.columns[1].width=Inches(1.5) #列宽
table.columns[2].width=Inches(1.5) #列宽
table.columns[3].width=Inches(1.5) #列宽
table.cell(0,0).text='张三收入'
table.cell(0,1).text='李四收入'
table.cell(0,2).text='王五收入'
table.cell(0,3).text='赵六收入'
table.cell(1,0).text='10000'
table.cell(1,1).text='20000'
table.cell(1,2).text='15000'
table.cell(1,3).text='30000'
#以上是单个单元格写入,也可以通过循环、读表等方式

prs.save('python-pptx_test6.pptx')  

在这里插入图片描述

7、添加图表

chart_data=ChartData()
chart_data.categories=['张三收入','李四收入','王五收入','赵六收入']
chart_data.add_series('金额',(10000,20000,15000,30000))

x,y,cx,cy=Inches(2),Inches(2),Inches(6),Inches(4.5)
chart=slide.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED,x,y,cx,cy,chart_data)

在这里插入图片描述

以上只是简单案例演示,实际工作会更复杂,大家加油ヾ(◍°∇°◍)ノ゙

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值