python 基于模板生成ppt_python生成ppt的方法
本文主要介绍如何通过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')
本文用到的源码
利用场景
周期性数据汇总及报表生成
业务或项目等数据可视化
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。
python 基于模板生成ppt_python生成ppt的方法相关推荐
- 史上最全PPT制作教程和PPT模板,毕业论文答辩PPT制作方法
史上最全PPT制作教程和PPT模板,毕业论文答辩PPT制作方法 每年到毕业季,各位大学毕业生们都在为毕业论文答辩做准备,而毕业论文答辩PPT的制作则是答辩准备当中一项非常重要的工作.因为,答辩委员会不 ...
- python 根据word生成ppt_python 生成 word、ppt 操作源码分享
使用 python 生成 word # coding=utf-8 from docx import Document from docx.shared import Pt from docx.shar ...
- 使用python生成ppt_python生成ppt的方法
本文主要介绍如何通过python生成ppt文件,以及借助ppt模板来生成ppt 环境 python 3 python-pptx 安装 pip3 install python-pptx 将文字输出到pp ...
- 用python合并多个ppt_Python合并PPT
将多个PPT文件合并成一个PPT文件 def joinPPT(): ''' http://www.s-anand.net/blog/automating-powerpoint-with-python/ ...
- 【Unity3D】基于模板测试和顶点膨胀的描边方法
1 前言 选中物体描边特效 中介绍了基于模板纹理模糊膨胀的描边方法,该方法实现了软描边,效果较好,但是为了得到模糊纹理,对屏幕像素进行了多次渲染,效率欠佳.本文将介绍另一种描边方法:基于模板测试和顶点 ...
- python加密库_python基于pyDes库实现des加密的方法
本文实例讲述了python基于pyDes库实现des加密的方法.分享给大家供大家参考,具体如下: 下载及简介地址:https://twhiteman.netfirms.com/des.html 如需要 ...
- 时序分析:DTW算法(基于模板)
对时序对象进行分析,使用KMP算法可以分析速率不变的模式,参考时序分析:欧式空间轨迹模式识别.使用基于模板匹配的方法,对于速率发生变化的模式,需要用新的对速率要求松散的方法,DTW方法为一种广泛使用的 ...
- python 法律检索_Python基于正则表达式实现检查文件内容的方法【文件检索】
本文实例讲述了Python基于正则表达式实现检查文件内容的方法分享给大家供大家参考,具体如下: 这个是之前就在学python,欣赏python的小巧但是功能强大,是连电池都自带的语言.平时工作中用Ja ...
- python excel处理框架_django框架基于模板 生成 excel(xls) 文件操作示例
本文实例讲述了django框架基于模板 生成 excel(xls) 文件操作.分享给大家供大家参考,具体如下: 生成Excel 文件,很多人会采用一些开源的库来实现,比如python 自带 csv 库 ...
最新文章
- PyTorch 学习笔记(四):权值初始化的十种方法
- SHELL中的三神器:管道、重定向、backtick
- Fortinet宣布收购AccelOps
- 把sqlserver中存储过程改写到oracle中
- 利用Web Services开发分布式应用
- Word 2010 制作文档结构之章节自动编号
- Ext grid js上移下移样例
- Linux入门笔记——文件操作命令1
- java.rmi.server.port_java.rmi.server.ExportException: internal error: ObjID already in use报错处理...
- POJ 1953 (DP)
- Deep Learning 参数--学习率α、批样本数量batch_size
- linux抓本来端口包,Linux抓包
- 删除的PSD文件在哪可以恢复?一定要看看
- 浅学DSP原理及应用
- Unity Mask制作圆形头像
- CMake 之 BUILD_SHARED_LIBS 和 CMAKE_BUILD_TYPE 用法教程
- 洛谷2990 牛跳房子Cow Hopscotch
- 路由器单区域 OSPF 基本配置
- 关于淘客软件的那些事儿
- 烟雨江湖服务器维护,烟雨江湖初始属性选择推荐 烟雨江湖出生攻略