python生成ppt的方法
作者:风匀坊 时间:2021-11-08 11:50:48
本文主要介绍如何通过python生成ppt文件,以及借助ppt模板来生成ppt
环境
python 3
python-pptx
安装
pip3 install python-pptx
将文字输出到ppt
效果图
代码
from pptx import Presentation
# 创建幻灯片 ------
prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
# 设置标题和副标题
title.text = "Hello, World!"
subtitle.text = "pip install python-pptx"
prs.save("test.pptx")
图表输出到ppt
效果图
代码
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches
# 创建幻灯片 ------
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])
# 定义图表数据 ---------------------
chart_data = ChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Series 1', (19.2, 21.4, 16.7))
# 将图表添加到幻灯片 --------------------
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
)
prs.save('chart-01.pptx')
使用ppt模板来生成ppt
准备ppt模板(网络下载或自定义幻灯片母版)
加载ppt模板,并使用指定幻灯片样式
添加数据并生成新ppt
效果图
代码
from pptx import Presentation
from pptx.util import Inches
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Cm #Inches
from pptx.enum.chart import XL_LEGEND_POSITION
if __name__ == '__main__':
# 创建幻灯片 ------
prs = Presentation('template.pptx')
title_only_slide_layout = prs.slide_layouts[5]
slide = prs.slides.add_slide(title_only_slide_layout)
shapes = slide.shapes
shapes.title.text = '报告'
# 定义表格数据 ------
name_objects = ["object1", "object2", "object3"]
name_AIs = ["AI1", "AI2", "AI3"]
val_AI1 = (19.2, 21.4, 16.7)
val_AI2 = (22.3, 28.6, 15.2)
val_AI3 = (20.4, 26.3, 14.2)
val_AIs = [val_AI1, val_AI2, val_AI3]
# 表格样式 --------------------
rows = 4
cols = 4
top = Cm(12.5)
left = Cm(3.5) #Inches(2.0)
width = Cm(24) # Inches(6.0)
height = Cm(6) # Inches(0.8)
# 添加表格到幻灯片 --------------------
table = shapes.add_table(rows, cols, left, top, width, height).table
# 设置单元格宽度
table.columns[0].width = Cm(6)# Inches(2.0)
table.columns[1].width = Cm(6)
table.columns[2].width = Cm(6)
table.columns[3].width = Cm(6)
# 设置标题行
table.cell(0, 1).text = name_objects[0]
table.cell(0, 2).text = name_objects[1]
table.cell(0, 3).text = name_objects[2]
# 填充数据
table.cell(1, 0).text = name_AIs[0]
table.cell(1, 1).text = str(val_AI1[0])
table.cell(1, 2).text = str(val_AI1[1])
table.cell(1, 3).text = str(val_AI1[2])
table.cell(2, 0).text = name_AIs[1]
table.cell(2, 1).text = str(val_AI2[0])
table.cell(2, 2).text = str(val_AI2[1])
table.cell(2, 3).text = str(val_AI2[2])
table.cell(3, 0).text = name_AIs[2]
table.cell(3, 1).text = str(val_AI3[0])
table.cell(3, 2).text = str(val_AI3[1])
table.cell(3, 3).text = str(val_AI3[2])
# 定义图表数据 ---------------------
chart_data = ChartData()
chart_data.categories = name_objects
chart_data.add_series(name_AIs[0], val_AI1)
chart_data.add_series(name_AIs[1], val_AI2)
chart_data.add_series(name_AIs[2], val_AI3)
# 添加图表到幻灯片 --------------------
x, y, cx, cy = Cm(3.5), Cm(4.2), Cm(24), Cm(8)
graphic_frame = slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
)
chart = graphic_frame.chart
chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.TOP
chart.legend.include_in_layout = False
value_axis = chart.value_axis
value_axis.maximum_scale = 100.0
value_axis.has_title = True
value_axis.axis_title.has_text_frame = True
value_axis.axis_title.text_frame.text = "False positive"
value_axis.axis_title.text_frame.auto_size
prs.save('test_template.pptx')
本文用到的源码
下载地址
利用场景
周期性数据汇总及报表生成
业务或项目等数据可视化
来源:https://www.huuinn.com/archives/524
标签:python,ppt
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PHP中DirectIO操作文件扩展的用法大全
2023-06-13 13:58:45
Python爬虫采集微博视频数据
2023-08-11 16:01:03
![](https://img.aspxhome.com/file/2023/2/101012_0s.jpg)
python操作mongodb根据_id查询数据的实现方法
2023-04-04 23:12:28
Python函数之zip函数的介绍与实际应用
2022-06-02 00:52:51
![](https://img.aspxhome.com/file/2023/7/103267_0s.png)
客户端和服务端的编码“陷阱”
2007-12-21 19:19:00
python使用socket实现图像传输功能
2023-08-30 17:10:17
Oracle数据库安全策略分析(一)
2010-07-31 12:57:00
python分分钟绘制精美地图海报
2021-11-10 11:44:25
![](https://img.aspxhome.com/file/2023/8/133428_0s.png)
Python图像运算之图像灰度线性变换详解
2022-06-10 15:12:28
![](https://img.aspxhome.com/file/2023/0/117620_0s.png)
python轻量级orm框架 peewee常用功能速查详情
2021-10-22 20:43:52
![](https://img.aspxhome.com/file/2023/0/131150_0s.png)
学习ASP.NET八天入门:第一天
2007-08-07 13:08:00
![](https://img.aspxhome.com/file/UploadPic/up/2007080714434946.jpg)
pandas学习之df.set_index的具体使用
2021-11-12 07:35:20
python property的使用技巧分享
2022-11-18 21:25:51
python实现图片批量剪切示例
2021-11-18 13:04:38
Python实现语音识别和语音合成功能
2021-07-13 13:06:58
![](https://img.aspxhome.com/file/2023/7/118347_0s.png)
python基于openpyxl生成excel文件
2022-08-03 03:10:47
![](https://img.aspxhome.com/file/2023/4/82684_0s.png)
python中remove函数的踩坑记录
2022-10-25 18:32:50
Django框架之中间件MiddleWare的实现
2021-03-25 15:08:30
用python读写excel的方法
2021-11-11 10:10:50
使用python BeautifulSoup库抓取58手机维修信息
2022-08-10 01:55:20