from pptx import Presentation
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[8])
# 输出slide中的每个占位符的索引和名字
for shape in slide.placeholders:
print(shape.placeholder_format.idx,shape.name)
# 知道索引后也可以这样访问名称
print(slide.palceholder[1])
for shape in slide.shapes:
if shape.is_placeholder:
phf = shape.placeholder_format
print(phf.index,phf.type)
slide_02 = prs.slides.add_slide(prs.slide_layouts[8])
place_holder = slide_02.placeholders[1]
# print(place_holder.name)
# print(place_holder.placeholder_format.type)
picture = place_holder.insert_picture('壁纸.jpg')
prs.save('test04_ppt.pptx')
文本框
from pptx import Presentation
from pptx.enum.text import MSO_VERTICAL_ANCHOR,MSO_AUTO_SIZE
from pptx.util import Inches,Pt
from pptx.enum.dml import MSO_THEME_COLOR
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[1])
shapes = slide.shapes
for shape in shapes:
if not shape.has_text_frame: # 判断如果说形状没有文本框,跳出,进行下一次循环
continue
text_frame = shape.text_frame
print(text_frame)
访问段落
para_strs = [
'Egg,bacon,sausage and spam',
'spam,bacon,sausage and spam',
'spam,egg,spam,spam,bacon and spam'
]
for shape in shapes:
if not shape.has_text_frame:
continue
text_frame = shape.text_frame # 因为文本框中时常有自带的段落,所以我们首先需清空文本框
text_frame.clear() # 清空文本框
para = text_frame.paragraphs[0] # 获取索引为0的段落
print(para)
para.text = para_strs[0] # 将第一条数据放置段落1中
run = para.add_run()
run.text = 'hello world'
for para_str in para_strs[1:]: # 取出第一条下面的所有数据
p = text_frame.add_paragraph()
p.text = para_str
文本框格式
slide_02 = prs.slides.add_slide(prs.slide_layouts[1])
shapes = slide_02.shapes
shape = shapes[0]
text_frame = shape.text_frame
text_frame.text = 'hello world bababbababbababa'
text_frame.margin_bottom = Inches(0.08) # 设置下边距
text_frame.vertical_anchor = MSO_VERTICAL_ANCHOR.TOP # 垂直方向顶锚点
text_frame.word_wrap = False # True 允许自动换行 False不允许自动换行
text_frame.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT # 文本框自适应字体大小
text_frame_02 = shapes[1].text_frame
text_frame_02.clear()
p = text_frame_02.paragraphs[0]
run = p.add_run()
run.text = 'how are you,I am study python'
from pptx import Presentation
from pptx.util import Inches,Pt
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE, XL_LABEL_POSITION
from pptx import Presentation
from pptx.util import Inches
prs = Presentation()
blank_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_layout)
# slide.shapes.add_picture??
left = top = Inches(1)
img_path = '壁纸.jpg'
slide.shapes.add_picture(
img_path,left,top
)
prs.save('text2.pptx')
添加形状(五边形)
from pptx import Presentation
from pptx.util import Inches
from pptx.enum.shapes import MSO_AUTO_SHAPE_TYPE
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])
shapes = slide.shapes
shapes.title.text = '添加形状'
left = Inches(0.93)
top = Inches(3)
width = Inches(1.75)
height = Inches(1)
# shapes.add_shape??
shape = shapes.add_shape(MSO_AUTO_SHAPE_TYPE.PENTAGON,left,top,width,height) # 添加五边形
shape.text = 'step'
'''
如何在第一个五边形后边添加V型
- 添加第一个
- 通过for循环改变其内容以及位置
'''
left = left + width - Inches(0.4)
width = Inches(2)
for n in range(2,6):
shape = shapes.add_shape(MSO_AUTO_SHAPE_TYPE.CHEVRON,left,top,width,height) # 创建v型图
shape.text = 'step %d '%n # 每个V型的图片的内容填充
left = left + width - Inches(0.4) # 每个图片的位置进行累加 往右移动
prs.save('test3_shape.pptx')
读取幻灯片内容
from pptx import Presentation
prs = Presentation(r'python-ppt\text1.pptx') # 打开text1.pptx文件
text_runs = []
for slide in prs.slides:
for shape in slide.shapes:
if not shape.has_text_frame:
continue
for para in shape.text_frame.paragraphs:
for run in para.runs:
text_runs.append(run.text)
print(text_runs)