2023年12月14日发(作者:)
小土豆编程软件 打开ppt
Pythonpptx库用于创建/编辑PowerPoint(.pptx)文件。这不适用于MS Office 20XX和以前的版本。
可以使用这个库添加形状、段落、文本和幻灯片等等。一个PPT文件为presentation,基本的结构为展示文件presentation-幻灯片页slide-形状shape组成,形状就需要区分开,是包含文本的形状还是不包含文本的形状(纯图片等)。
如果是包含文本的形状,则可以获取内部的文本框,一个文本框又可以看作是一个小的word文档,包含段落paragraph - 文字块run
1、打开PPT文件
from pptx import Presentation
# 这里给出需要打开的文件路径
file_path = r'...'
pptx = Presentation(file_path)
2、获取幻灯片页
用 可以获得一个列表,包括所有的幻灯片页 slide
对象
for slide in :
print(slide)
例:创建带有标题和字幕幻灯片的新PowerPoint文件。
Placeholder占位符已经完成了样式设置,包括字体、字号、颜色等等。
在特定占位符内输入文字可直接转化为特定的样式
# import Presentation class
# from pptx library
from pptx import Presentation
# Creating presentation object
root = Presentation()
# Creating slide layout
first_slide_layout = _layouts[0]
""" Ref for slide types:
0 -> title and subtitle
1 -> title and content
2 -> section header
3 -> two content
5 -> Title only
6 -> Blank
7 -> Content with caption
8 -> Pic with caption
"""
# Creating slide object to add
# in Attaching slides
# with ppt slide = _slide(first_slide_layout)
# Adding title and subtitle in
# first page of slide
= " Created By python-pptx"
# We have different formats of
# subtitles in ppts, for simple
# subtitle this method should
# implemented, you can change
# 0 to 1 for different design
olders[1].text = " This is 2nd way"
# Saving file
("")
print("done")


发布评论