python-pptx的基本使用

python-pptx的使用首先需要了解几个基本概念:

1.引入python-pptx

frompptximportpresentation
# 实例化Presentation
prs= Presentation()

2.ppt模板的选择

a、使用ppt自带的模板

prs= Presentation()
prs.slide_layouts[index]

ppt自带了常用的1-48种模板通过index选择对应的模板

b、使用自定义ppt模板

prs= Presentation('template.pptx')

3.新建一页幻灯片

slide= prs.slides.add_slide(prs.slide_layouts[1])
# prs.slides.add_slide()增加一页幻灯片方法

4.编辑幻灯中的元素

a、根据placeholdes索引获取一页幻灯片中的元素

body_shape= slide.shapes.placeholders
# body_shape为本页ppt中所有shapes
body_shape[0].text= 'this is placeholders[0]'
body_shape[1].text= 'this is placeholders[1]'

在ppt中所有的元素均被当成一个shape,slide.shapes表示幻灯片类中的模型类,placeholders中为每个模型,采用slide_layouts[1]中包含两个文本框,所以printlen(slide.shapes.placeholders) 话为2

b、获取幻灯片中的title元素(本页幻灯片必须含有标题元素才能通过此方法获取)

title_shape= slide.shapes.title
# 获取本页ppt的title元素
title_shape.text= 'this is a title'

c、在本页幻灯片中新增元素

new_paragraph= body_shape[1].text_frame.add_paragraph()
# 在第二个shape中的文本框中添加新段落
new_paragraph.text= 'add_paragraph'#新段落中的文字
ew_paragraph.font.bold= True # 文字加粗
new_paragraph.font.italic= True # 文字斜体
frompptx.utilimportPt#设置文字大小必须引入pptx.util中的Pt
new_paragraph.font.size= Pt(15)  # 文字大小
new_paragraph.font.underline= True # 文字下划线new_paragraph.level = 1 # 新段落的级别

5.新增幻灯中的元素

a、添加新文本框

frompptx.utilimportInches
left= top= width= height= Inches(5)  
# 预设位置及大小
textbox= slide.shapes.add_textbox(left, top, width, height)  
# 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' 
# 段落文字

b、添加图片

 

img_path= 'img_path.jpg' 
# 文件路径
left, top, width, height= Inches(1), Inches(4.5), Inches(2), Inches(2)  
# 预设位置及大小
pic= slide.shapes.add_picture(img_path, left, top, width, height) 
# 在指定位置按预设值添加图片

c、添加形状

frompptx.enum.shapesimportMSO_SHAPE
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)  
# 在指定位置按预设值添加类型为PENTAGON的形状
shape.text= 'Step 1'
forninrange(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)

d、添加表格

rows, cols, left, top, width, height= 2, 2, Inches(3.5), Inches(4.5), Inches(6), Inches(0.8)
table= slide.shapes.add_table(rows, cols, left, top, width, height).table 
# 添加表格,并取表格类
table.columns[0].width= Inches(2.0)  
# 第一纵列宽度
table.columns[1].width= Inches(4.0)  
# 第二纵列宽度
table.cell(0, 0).text= 'text00' 
# 指定位置写入文本
table.cell(0, 1).text= 'text01'
table.cell(1, 0).text= 'text10'
table.cell(1, 1).text= 'text11'

 

 

### 使用 `python-pptx` 创建和操作 PPT 文件 #### 导入库与初始化 为了开始使用 `python-pptx` 进行 PowerPoint 文档的操作,首先需要确保已经成功安装此库。接着,在代码中导入必要的模块并准备打开或者新建一个 PPT 文件。 ```python from pptx import Presentation # 导入Presentation类来处理PPT文件[^1] ``` 对于新创建的演示文稿: ```python prs = Presentation() # 初始化一个新的Presentation对象表示空白文档 ``` 而对于已有文件,则可以通过指定路径加载现有 PPT: ```python path_to_existing_presentation = 'example.pptx' prs = Presentation(path_to_existing_presentation) # 加载现有的PPT文件 ``` #### 添加幻灯片 每一份演示文稿都由多个幻灯片组成,可以利用 `.slides.add_slide()` 方法向当前演示文稿添加新的幻灯片。通常情况下会先定义好所需的布局再进行添加。 ```python slide_layout = prs.slide_layouts[0] # 获取第一个可用的幻灯片布局样式 new_slide = prs.slides.add_slide(slide_layout) # 基于选定布局新增一张幻灯片 ``` #### 插入内容到幻灯片 一旦有了幻灯片之后就可以往里面填充各种各样的元素了,比如文字框、图形、图像甚至是图表等复杂组件。下面展示了一个简单的例子说明怎样给幻灯片加入一段文本。 ```python title_text_frame = new_slide.shapes.title.text_frame title_text_frame.text = "这是标题" body_shape = new_slide.placeholders[1].text_frame paragraph = body_shape.paragraphs[0] run = paragraph.add_run() run.text = "这里是正文内容" ``` 除了基本的文字之外还可以做更多事情,例如调整字体大小颜色等等属性;也可以插入图片或者其他多媒体资源。具体实现方式取决于实际需求和个人偏好。 #### 保存修改后的PPT文件 完成所有的编辑工作以后记得调用 `.save()` 函数把更改持久化下来。 ```python output_file_path = './modified_example.pptx' prs.save(output_file_path) # 将最终版本保存至指定位置[^2] ``` 通过上述步骤即可轻松上手 `python-pptx` 来构建自定义的 Powerpoint 演示文稿。这个强大的工具不仅限于此处介绍的功能,还提供了丰富的 API 支持更复杂的场景应用开发。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值