
👉 项目官网:https://www.python-office.com/ 👈

大家好,这里是程序员晚枫,正在all in AI编程实战。
每个产品都要做一个介绍 PPT?手动做太慢了!
今天教你怎么用 python-office 批量生成 PPT。
1、基础:批量生成产品介绍
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| import office
products = [ { 'name': 'Python入门课', 'price': '99元', 'desc': '零基础入门,15节课学会Python基础', 'target': '编程小白' }, { 'name': '自动化办公课', 'price': '199元', 'desc': '50个案例,学会用Python处理Excel、Word、邮件', 'target': '职场人士' }, { 'name': 'AI编程课', 'price': '299元', 'desc': '用ChatGPT辅助编程,效率提升10倍', 'target': '想学AI的程序员' } ]
for p in products: filename = f"PPT_{p['name']}.pptx" office.ppt.create(path=filename, title=p['name']) office.ppt.add( path=filename, title=p['name'], content=f"价格:{p['price']}" ) office.ppt.add( path=filename, title='课程介绍', content=p['desc'] ) office.ppt.add( path=filename, title='适用人群', content=p['target'] ) office.ppt.add( path=filename, title='购买方式', content='微信:aiwf365' ) print(f'已生成: {filename}')
print(f'共生成 {len(products)} 个PPT!')
|
2、中级:带图片的PPT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| import office
def create_product_ppt(product, image_path): """创建带图片的产品PPT""" filename = f"PPT_{product['name']}.pptx" office.ppt.create(path=filename, title=product['name']) office.ppt.add( path=filename, title=product['name'], content=f"💰 价格:{product['price']}", image=image_path ) office.ppt.add( path=filename, title='课程详情', content=product['desc'] ) return filename
for product in products: img = f"{product['name']}_图片.jpg" if os.path.exists(img): create_product_ppt(product, img)
|
3、高级:从Excel读取数据批量生成
先准备一个 Excel 文件 产品列表.xlsx:
| 名称 | 价格 | 描述 | 图片 |
|---|
| 产品A | 99元 | 这是产品A的描述 | a.jpg |
| 产品B | 199元 | 这是产品B的描述 | b.jpg |
然后用代码批量生成:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| import office import os
def batch_create_product_ppts(excel_file): """从Excel批量生成产品PPT""" df = office.excel.read(path=excel_file) success = 0 for idx, row in df.iterrows(): name = row['名称'] price = row['价格'] desc = row['描述'] image = row.get('图片', None) filename = f"产品介绍_{name}.pptx" try: office.ppt.create(path=filename, title=name) content = f"💰 {price}\n\n{desc}" if pd.notna(image) and os.path.exists(str(image)): office.ppt.add(path=filename, title=name, content=content, image=str(image)) else: office.ppt.add(path=filename, title=name, content=content) office.ppt.add(path=filename, title='详细信息', content=desc) office.ppt.add( path=filename, title='购买方式', content=f'产品名称:{name}\n官方微信:aiwf365' ) success += 1 print(f'✅ 已生成: {filename}') except Exception as e: print(f'❌ 生成失败: {name} - {e}') print(f'\n完成!成功 {success} 个,失败 {len(df) - success} 个')
batch_create_product_ppts('产品列表.xlsx')
|
4、实战案例:批量生成公司介绍PPT
每个分公司都要做一个介绍 PPT?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import office
branches = [ { 'name': '北京分公司', 'established': '2010年', 'employees': '50人', 'revenue': '5000万', 'address': '北京市朝阳区XX大厦' }, { 'name': '上海分公司', 'established': '2015年', 'employees': '30人', 'revenue': '3000万', 'address': '上海市浦东新区XX大楼' } ]
for b in branches: filename = f"公司介绍_{b['name']}.pptx" office.ppt.create(path=filename, title=b['name']) office.ppt.add( path=filename, title=f"{b['name']} 概览", content=f"成立时间:{b['established']}\n员工人数:{b['employees']}\n年营业额:{b['revenue']}" ) office.ppt.add( path=filename, title='联系我们', content=f"地址:{b['address']}\n电话:xxx-xxxx-xxxx" ) print(f'已生成: {filename}')
|
5、常见问题
Q:生成的PPT打开格式乱了?
A:python-pptx 对复杂模板支持有限,建议生成后用 PowerPoint 微调。
Q:图片路径找不到?
A:确保图片文件存在,可以用 os.path.exists() 检查。
Q:Mac上能用吗?
A:可以,但部分功能需要 Microsoft PowerPoint。
6、下讲预告
学会了PPT批量生成,下一讲我们学 课程总结:回顾50讲所学,展望未来。
敬请期待!
有问题欢迎加微信 python-office 进群交流~
程序员晚枫专注AI编程培训,小白看完他和图灵社区合作的教程《30讲 · AI编程训练营》就能上手做AI项目。
🎓 AI 编程实战课程
想系统学习 AI 编程?程序员晚枫的 AI 编程实战课 帮你从零上手!