创建
Presentation() + slides.add_slide()
pptx 是处理 PowerPoint 演示文稿的技能。它的核心是:创建、编辑和格式化 PPT 文件。
当用户提到以下内容时使用:
from pptx import Presentation
prs = Presentation()prs.save('presentation.pptx')from pptx import Presentation
prs = Presentation()
# 添加空白幻灯片slide = prs.slides.add_slide(prs.slide_layouts[1])
# 添加标题title = slide.shapes.titletitle.text = '演示标题'
# 添加内容subtitle = slide.placeholders[1]subtitle.text = '副标题内容'
prs.save('slides.pptx')from pptx import Presentationfrom pptx.util import Inches, Pt
prs = Presentation()slide = prs.slides.add_slide(prs.slide_layouts[1])
# 添加文本框left = Inches(1)top = Inches(2)width = Inches(8)height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)tf = txBox.text_frametf.text = '这是文本框内容'
# 添加段落p = tf.add_paragraph()p.text = '第二段文字'p.font.size = Pt(24)p.font.bold = True
prs.save('textbox.pptx')from pptx import Presentationfrom pptx.util import Inches
prs = Presentation()slide = prs.slides.add_slide(prs.slide_layouts[5]) # 空白布局
# 添加表格rows = 3cols = 3left = Inches(1)top = Inches(2)width = Inches(8)height = Inches(2)
table = slide.shapes.add_table(rows, cols, left, top, width, height).table
# 设置表格内容table.cell(0, 0).text = '姓名'table.cell(0, 1).text = '年龄'table.cell(0, 2).text = '城市'
table.cell(1, 0).text = '张三'table.cell(1, 1).text = '25'table.cell(1, 2).text = '北京'
prs.save('table.pptx')from pptx import Presentationfrom pptx.chart.data import CategoryChartDatafrom pptx.enum.chart import XL_CHART_TYPEfrom pptx.util import Inches
prs = Presentation()slide = prs.slides.add_slide(prs.slide_layouts[5])
# 创建图表数据chart_data = CategoryChartData()chart_data.categories = ['Q1', 'Q2', 'Q3', 'Q4']chart_data.add_series('销售额', (100, 150, 120, 180))
# 添加图表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).chart
prs.save('chart.pptx')from pptx import Presentationfrom pptx.util import Inches
prs = Presentation()slide = prs.slides.add_slide(prs.slide_layouts[1])
# 添加图片slide.shapes.add_picture( 'image.jpg', Inches(1), Inches(1), width=Inches(4))
prs.save('with_image.pptx')需求:创建商业计划书 PPT
步骤:1. 添加封面2. 添加目录3. 添加问题/解决方案4. 添加市场分析5. 添加财务预测6. 添加团队介绍需求:创建季度报告
步骤:1. 添加数据图表2. 添加表格3. 添加分析文字4. 统一格式创建
Presentation() + slides.add_slide()
文本
add_textbox() + text_frame
表格
add_table() + cell
图表
CategoryChartData + add_chart()
查看源文件: GitHub原始文件