python pptx_python-pptx
python-pptx的使用首先需要了解几个基本概念:
1.引入python-pptx
frompptximportpresentation
# 实例化Presentation
prs= Presentation()
2.ppt模板的选择
a、使用ppt自带的模板
prs= Presentation()
prs.slide_layouts[index]
ppt自带了常用的1-48种模板通过index选择对应的模板
b、使用自定义ppt模板
prs= Presentation('template.pptx')
3.新建一页幻灯片
slide= prs.slides.add_slide(prs.slide_layouts[1])
# prs.slides.add_slide()增加一页幻灯片方法
4.编辑幻灯中的元素
a、根据placeholdes索引获取一页幻灯片中的元素
body_shape= slide.shapes.placeholders
# body_shape为本页ppt中所有shapes
body_shape[0].text= 'this is placeholders[0]'
body_shape[1].text= 'this is placeholders[1]'
在ppt中所有的元素均被当成一个shape,slide.shapes表示幻灯片类中的模型类,placeholders中为每个模型,采用slide_layouts[1]中包含两个文本框,所以printlen(slide.shapes.placeholders) 话为2
b、获取幻灯片中的title元素(本页幻灯片必须含有标题元素才能通过此方法获取)
title_shape= slide.shapes.title
# 获取本页ppt的title元素
title_shape.text= 'this is a title'
c、在本页幻灯片中新增元素
new_paragraph= body_shape[1].text_frame.add_paragraph()
# 在第二个shape中的文本框中添加新段落
new_paragraph.text= 'add_paragraph'#新段落中的文字
ew_paragraph.font.bold= True # 文字加粗
new_paragraph.font.italic= True # 文字斜体
frompptx.utilimportPt#设置文字大小必须引入pptx.util中的Pt
new_paragraph.font.size= Pt(15) # 文字大小
new_paragraph.font.underline= True # 文字下划线new_paragraph.level = 1 # 新段落的级别
5.新增幻灯中的元素
a、添加新文本框
frompptx.utilimportInches
left= top= width= height= Inches(5)
# 预设位置及大小
textbox= slide.shapes.add_textbox(left, top, width, height)
# left,top为相对位置,width,height为文本框大小
textbox.text= 'this is a new textbox'
# 文本框中文字
new_para= textbox.text_frame.add_paragraph()
# 在新文本框中添加段落
new_para.text= 'this is second para in textbox'
# 段落文字
b、添加图片
img_path= 'img_path.jpg'
# 文件路径
left, top, width, height= Inches(1), Inches(4.5), Inches(2), Inches(2)
# 预设位置及大小
pic= slide.shapes.add_picture(img_path, left, top, width, height)
# 在指定位置按预设值添加图片
c、添加形状
frompptx.enum.shapesimportMSO_SHAPE
left, top, width, height= Inches(1), Inches(3), Inches(1.8), Inches(1)
# 预设位置及大小
shape= slide.shapes.add_shape(MSO_SHAPE.PENTAGON, left, top, width, height)
# 在指定位置按预设值添加类型为PENTAGON的形状
shape.text= 'Step 1'
forninrange(2, 6):
left= left+width-Inches(0.3)
shape= slide.shapes.add_shape(MSO_SHAPE.CHEVRON, left, top, width, height)
shape.text= 'Step{}'.format(n)
d、添加表格
rows, cols, left, top, width, height= 2, 2, Inches(3.5), Inches(4.5), Inches(6), Inches(0.8)
table= slide.shapes.add_table(rows, cols, left, top, width, height).table
# 添加表格,并取表格类
table.columns[0].width= Inches(2.0)
# 第一纵列宽度
table.columns[1].width= Inches(4.0)
# 第二纵列宽度
table.cell(0, 0).text= 'text00'
# 指定位置写入文本
table.cell(0, 1).text= 'text01'
table.cell(1, 0).text= 'text10'
table.cell(1, 1).text= 'text11'
python pptx_python-pptx相关推荐
- python pptx_python python-pptx对pptx文件读取示例
python python-pptx对pptx文件读取示例,以下是如何使用pptx.Presentation()的代码示例. python-pptx安装: pip install python-pp ...
- python的pptx文档_通过python-pptx模块操作ppt文件
ppt通过其精美的可视化技巧以及良好的演示效果,成为了职场人士的必备技能.ppt的设计是一门大学问,无论是设计技巧,还是操作方法,都衍生出了专门的课程. 本文主要介绍python操作ppt的技巧,编程 ...
- 咨询报告生成:使用python生成pptx格式的报告
已经有人做了,思路和自己想做的一个模式差不多,见模式一: 先做好ppt模板 用python计算数据,生成图形 用python调用模板,将图形插入pptx页面中,生成分析报告 模式二与模式一的差别在第三 ...
- reportgen :python生产pptx
欢迎关注博主主页,学习python视频资源 https://www.cnblogs.com/gasongjian/p/8670925.html reportgen v0.1.8 更新介绍 这段时间,我 ...
- 用python导出pptx中每一页的标题
problem PPT每一页的标题 solution 大纲视图好像也行? python导出(需要先pip3 install python-pptx一下,以及更新pip3版本和安装lxml包) from ...
- Python利用pptx模块三步将图片插入特定PPT模板
任务描述 有一个如下图所示的PPT模板,希望将已有的图片以特定的顺序插入到模板占位符中 现有PPT模板 希望的结果 代码演示 import os from os.path import ...
- 推荐一个利用 python 生成 pptx 分析报告的工具包:reportgen
reportgen v0.1.8 更新介绍 这段时间,我对 reportgen 进行了大工程量的修改和更新.将之前在各个文章中出现的函数进行了封装,同时也对现有工具包的一些逻辑进行了调整. 1.rep ...
- python自动化系列之python操作pptx文件
ppt文档是日常办公中经常用到的工具,好的ppt文档需要大量的时间和精力去写,但是简单的ppt文档,可以使用python批量生成: python-pptx介绍 python-pptx是用于创建和更新P ...
- python的pptx文档remove_Python之pptx实现添加内容与删除(移动)页操作
问题背景 大量表格数据需要生成指定格式的ppt文件,内容以文字和表格为主,首尾页与内容有固定格式.博主不熟悉VBA操作,希望通过模板用Python完成自动化. 基本思路 使用xlrd模块读取xlsx文 ...
- python和报表生成工具_推荐一个利用 python 生成 pptx 分析报告的工具包:reportgen...
reportgen v0.1.8 更新介绍 这段时间,我对 reportgen 进行了大工程量的修改和更新.将之前在各个文章中出现的函数进行了封装,同时也对现有工具包的一些逻辑进行了调整. 1.rep ...
最新文章
- 浏览器端JS导出EXCEL
- discuz 二次开发
- matlab练习程序(二值图像连通区域标记法,一步法)
- 【树链剖分】染色(luogu 2486/金牌导航 树链剖分-3)
- 台式电脑麦克风_还有人买台式PC吗?这份新品推荐攻略值得收藏
- Zookeeper java客户端ZkClient使用详解
- java面向对象编程 漫画_Java面向对象编程(一)
- 【IOS】从android角度来实现(理解)IOS的UITableView
- red hat linux 改ip,Red Hat Enterprise Linux 7(RHEL7)配置静态IP地址
- vue大屏可视化布局
- Git版本控制:Github的使用之 多人协作及参与项目
- boost::unorder_map如何插入元素_「leetcode」701. 二叉搜索树中的插入操作:【递归法】【迭代法】详解...
- 物理计算机仿真实验报告,大学物理实验实验报告模板
- URGENT/11 白皮书笔记——远程危害VxWorks的关键漏洞6.9.3及以上
- 一篇文章带你了解jsMind
- 小米盒子 smb Android,客厅里的多媒体 小米盒子SMB本地连接
- nginx的配置文件详解
- 在ts文件中使用require报错解决
- Science重磅 | 新技术Slide-seq能以高空间分辨率测量全基因组的表达情况
- 需求工程:软件建模与分析 读书笔记三