<摘要>
在大部分人的职场中,PPT已经是日常工作中不可或缺的部分,但有时存在一些重复性的操作,拖累了我们工作效率。
现在有一个强大的 python-pptx 库,可以帮助我们用代码自动化创建和编辑PPT,实现一键生成专业演示文稿。本文将手把手教你从零开始掌握这项技能:添加幻灯片、文本、图片、表格和图表,甚至批量生成报告。
在开始操作之前,必须清楚一个原则。
绝对不要用 Python 从零去生成一个 PPT!
一、快速上手:安装python-pptx库
pip install python-pptx安装完成后,我们来创建一个最简单的PPT测试一下:
from pptx import Presentation# 创建一个空的Presentation对象prs = Presentation()# 添加第一页幻灯片(标题布局)slide_layout = prs.slide_layouts[0] # 0通常是标题页布局slide = prs.slides.add_slide(slide_layout)# 添加标题和副标题title = slide.shapes.titlesubtitle = slide.placeholders[1]title.text = "我的第一个Python自动化PPT"subtitle.text = "使用python-pptx库生成"# 保存文件prs.save('first_ppt.pptx')二、常用属性:
1. Presentation 对象(prs = Presentation())
- slide_width:幻灯片宽度(EMU 单位,可读写)
- slide_height:幻灯片高度(EMU 单位,可读写)
- slides:所有幻灯片的集合(Slides 对象)
- slide_layouts:当前模板的布局集合
- slide_masters:幻灯片母版集合
- core_properties:核心文档属性(如 title、author、subject 等)
2. Slide 对象(slide = prs.slides.add_slide(layout))
- shapes:该幻灯片上所有形状的集合(Shapes 对象)。
- placeholders:占位符集合。
- background:背景设置。
3. Shapes 对象(slide.shapes)
- title:标题形状(如果存在,直接访问 slide.shapes.title)。
- add_picture():添加图片。
- add_table():添加表格。
- add_textbox():添加文本框。
- add_chart():添加图表。
4. Shape 对象(单个形状,如标题、图片、文本框)
- text:快捷设置文本(仅保留第一段)。
- left、top、width、height:位置和大小(EMU 或 Inches)。
- text_frame:文本框架(用于多段落)。
- is_placeholder:是否为占位符。
5. Textframe 和 Paragraph / Run(文本相关)
- text_frame.text:设置整个文本框内容。
- text_frame.add_paragraph():添加新段落。
- Paragraph 属性:level(缩进级别)、font(字体设置)。
- Font 属性:size、bold、italic、name、color。
三、核心基础:添加幻灯片、标题和文本内容
from pptx import Presentationfrom pptx.util import Inches, Ptprs = Presentation()# 第一页:标题页slide1 = prs.slides.add_slide(prs.slide_layouts[0])slide1.shapes.title.text = "Python自动化PPT教程"slide1.placeholders[1].text = "提升职场效率的利器"# 第二页:标题 + 内容(项目符号)slide2 = prs.slides.add_slide(prs.slide_layouts[1])slide2.shapes.title.text = "核心优势"content = slide2.placeholders[1].text_framecontent.text = "节省时间"p = content.add_paragraph()p.text = "数据驱动生成"p.level = 1 # 缩进一级p = content.add_paragraph()p.text = "批量处理报告"p.level = 1# 设置字体大小和颜色(可选进阶)for paragraph in content.paragraphs: for run in paragraph.runs: run.font.size = Pt(24) run.font.bold = Trueprs.save('basic_ppt.pptx')
nerror="javascript:errorimg.call(this);">四、实用进阶:图片、表格和数据图表自动化
插入图片:
# 在现有slide中添加图片img_path = 'your_image.jpg' # 替换为你的图片路径left = Inches(1)top = Inches(2)slide.shapes.add_picture(img_path, left, top, height=Inches(4))创建表格:
rows, cols = 5, 4left = Inches(1)top = Inches(2)width = Inches(8)height = Inches(4)table = slide.shapes.add_table(rows, cols, left, top, width, height).table# 填充数据table.cell(0, 0).text = "月份"table.cell(0, 1).text = "销售额"# ... 继续填充五、添加图表(柱状图、饼图等)
python-pptx内置支持多种图表类型,从数据直接生成。
from pptx import Presentationfrom pptx.util import Inchesfrom pptx.chart.data import CategoryChartDatafrom pptx.enum.chart import XL_CHART_TYPE# 创建新的PPTprs = Presentation()# 添加一张空白布局的幻灯片(布局索引5通常是空白)slide = prs.slides.add_slide(prs.slide_layouts[5])# 准备图表数据chart_data = CategoryChartData()chart_data.categories = ['Q1', 'Q2', 'Q3', 'Q4']chart_data.add_series('销售额', (19.2, 21.4, 18.5, 22.1))# 设置图表位置和大小(左、上、宽、高)x, y, cx, cy = Inches(1), Inches(2), Inches(6), Inches(4.5)# 添加簇状柱形图chart = slide.shapes.add_chart( XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data).chart# 可选:添加图表标题chart.has_title = Truechart.chart_title.text_frame.text = "季度销售额(单位:万元)"# 保存PPT文件prs.save('季度销售额图表.pptx')
nerror="javascript:errorimg.call(this);">六、真实职场应用:批量生成月度报告PPT
import pandas as pdfrom pptx import Presentationdf = pd.read_csv('sales_data.csv')prs = Presentation('template.pptx') # 可选:使用现有模板for i, row in df.iterrows(): slide = prs.slides.add_slide(prs.slide_layouts[5]) # 空白布局 title = slide.shapes.title title.text = f"{row['月份']}销售报告" # 添加表格、图表等(如上示例)prs.save('monthly_report.pptx')通过python-pptx,我们可以简单实现添加文本、图片、表格、图表的自动化。有类似工作流程可以用其中代码进行优化。减轻工作负担。
