Python自动处理pptx:新建、另存、添加幻灯片、添加标题、插入文本图片图形、提取文本

本文主要是介绍Python自动处理pptx:新建、另存、添加幻灯片、添加标题、插入文本图片图形、提取文本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Python-pptx库是一个用于创建、更新和读取Microsoft PowerPoint .pptx 文件的Python库。它允许我们使用Python脚本自动化PowerPoint文件的创建、更新和读取操作,是一个非常方便自动化处理PPTX的工具。
1

安装

pip install python-pptx

创建

from pptx import Presentation
ppt = Presentation()
ppt.save('demo.pptx')

打开、另存

from pptx import Presentation
ppt = Presentation('demo.pptx')
ppt.save('new_demo.pptx')

添加幻灯片

from pptx import Presentation
ppt = Presentation()
for i in range(11):layout = ppt.slide_layouts[i]slide = ppt.slides.add_slide(layout)
ppt.save('添加幻灯片.pptx')

添加标题

from pptx import Presentation
ppt = Presentation()
layout = ppt.slide_layouts[0]
slide = ppt.slides.add_slide(layout)
title = slide.shapes.title
title.text = '演示文稿制作自动化'
subtitle = slide.placeholders[1]
subtitle.text = 'python-pptx库从入门到精通'
ppt.save('添加标题1.pptx')
from pptx import Presentation
ppt = Presentation()
layout = ppt.slide_layouts[1]
slide = ppt.slides.add_slide(layout)
title_shape = slide.shapes.title
title_shape.text = 'python-pptx库的基本操作'
body_shape = slide.shapes.placeholders[1]
tf = body_shape.text_frame
p = tf.add_paragraph()
p.text = '演示文稿的基本操作'
p.level = 0
p = tf.add_paragraph()
p.text = '创建演示文稿'
p.level = 1
p = tf.add_paragraph()
p.text = '打开和保存演示文稿'
p.level = 1
p = tf.add_paragraph()
p.text = '打开演示文稿'
p.level = 2
p = tf.add_paragraph()
p.text = '保存演示文稿'
p.level = 2
ppt.save('添加标题2.pptx')

添加文本框

from pptx import Presentation
from pptx.util import Cm
ppt = Presentation()
layout = ppt.slide_layouts[6]
slide = ppt.slides.add_slide(layout)
left = top = Cm(3)
width = height = Cm(10)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
tf.text = '海内存知己,天涯若比邻。'
ppt.save('添加文本框.pptx')

设置文本格式

from pptx import Presentation
from pptx.util import Cm, Pt
from pptx.dml.color import RGBColor
from pptx.enum.dml import MSO_THEME_COLOR
from pptx.enum.text import MSO_ANCHOR, PP_ALIGN
# 创建演示文稿并添加幻灯片
ppt = Presentation()
layout = ppt.slide_layouts[6]
slide = ppt.slides.add_slide(layout)
left, top, width, height = Cm(2.7), Cm(2), Cm(20), Cm(15)
# 添加文本框,并设置文本区域的格式
txBox = slide.shapes.add_textbox(left, top, width, height)
text_frame = txBox.text_frame
text_frame.margin_top = Cm(0.5)
text_frame.vertical_anchor = MSO_ANCHOR.TOP
text_frame.word_wrap = True
# 添加第1段文本并设置段落格式和字体格式
text_frame.clear()
text_frame.text = '计算机科学经典著作'
p = text_frame.paragraphs[0]
p.alignment = PP_ALIGN.CENTER
p.space_after = Pt(18)
r = p.runs[0]
r.font.name = '方正隶变简体'
r.font.size = Pt(42)
r.font.bold = True
r.font.color.theme_color = MSO_THEME_COLOR.ACCENT_1
# 添加第2个段落并设置段落格式
p = text_frame.add_paragraph()
p.alignment = PP_ALIGN.JUSTIFY
p.line_spacing = 1.5
# 在第2个段落中添加第1个文本片段并设置字体格式
r = p.add_run()
r.text = 'Introduction to Algorithms'
r.font.name = 'Times New Roman'
r.font.size = Pt(26)
r.font.italic = True
r.font.bold = True
r.font.color.rgb = RGBColor(255, 0, 0)
# 在第2个段落中添加第2个文本片段并设置字体格式
r = p.add_run()
r.text = ',中文译名为《算法导论》。本书语言通俗易懂,很适合自学。'
r.font.name = '方正准雅宋_GBK'
r.font.size = Pt(26)
r.font.italic = False
r.font.bold = False
r.font.color.rgb = RGBColor(0, 0, 0)
ppt.save('设置文本格式.pptx')

添加图片

from pptx import Presentation
from pptx.util import Cm
ppt = Presentation()
layout = ppt.slide_layouts[5]
slide = ppt.slides.add_slide(layout)
slide.shapes.title.text = '烟花三月下扬州'
image_file = '水彩.jpg'
left = Cm(2.7)
top = Cm(4)
width = Cm(20)
pic = slide.shapes.add_picture(image_file, left, top, width)
ppt.save('添加图片.pptx')

添加形状

from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE
from pptx.util import Cm
ppt = Presentation()
layout = ppt.slide_layouts[5]
slide = ppt.slides.add_slide(layout)
slide.shapes.title.text = '添加形状'
left = top = Cm(4)
width = height = Cm(6)
shape = slide.shapes.add_shape(MSO_SHAPE.SUN, left, top, width, height)
shape.text = '太阳'
ppt.save('添加形状.pptx')

形状填充颜色

from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE
from pptx.enum.dml import MSO_THEME_COLOR
from pptx.util import Cm
ppt = Presentation()
layout = ppt.slide_layouts[5]
slide = ppt.slides.add_slide(layout)
slide.shapes.title.text = '添加形状'
left, top, width, height = Cm(0.8), Cm(4), Cm(5), Cm(2.5)
for n in range(1, 6):shape = slide.shapes.add_shape(MSO_SHAPE.CHEVRON, left, top, width, height)shape.text = f'第 {n} 步'fill = shape.fillfill.solid()fill.fore_color.theme_color = MSO_THEME_COLOR.ACCENT_1fill.fore_color.brightness = -0.1 * nleft = left + width - Cm(0.3)
ppt.save('为形状填充颜色.pptx')

添加表格

from pptx import Presentation
from pptx.util import Cm
ppt = Presentation()
layout = ppt.slide_layouts[5]
slide = ppt.slides.add_slide(layout)
slide.shapes.title.text = '添加表格'
# 添加表格
rows = 5
cols = 4
left = Cm(2.7)
top = Cm(4)
width = Cm(20)
height = Cm(6)
table = slide.shapes.add_table(rows, cols, left, top, width, height).table
# 设置列宽
table.columns[0].width = Cm(6)
table.columns[1].width = Cm(5)
table.columns[2].width = Cm(4.5)
table.columns[3].width = Cm(4.5)
# 填充表头
table.cell(0, 0).text = '财务类型'
table.cell(0, 1).text = '大类'
table.cell(0, 2).text = '金额(万元)'
table.cell(0, 3).text = '占比'
# 填充单元格
table.cell(1, 0).text = '资产'
table.cell(1, 1).text = '长期资产'
table.cell(1, 2).text = '5645'
table.cell(1, 3).text = '61.24%'
table.cell(2, 1).text = '流动资产'
table.cell(2, 2).text = '3573'
table.cell(2, 3).text = '38.76%'table.cell(3, 0).text = '负债'
table.cell(3, 1).text = '股东权益'
table.cell(3, 2).text = '3400'
table.cell(3, 3).text = '48.76%'
table.cell(4, 1).text = '流动负债'
table.cell(4, 2).text = '3573'
table.cell(4, 3).text = '51.24%'
# 合并单元格
cell1 = table.cell(1, 0)
cell1.merge(table.cell(2, 0))
cell2 = table.cell(3, 0)
cell2.merge(table.cell(4, 0))
# 调整内边距
cell1.margin_top = Cm(0.75)
cell1.margin_left = Cm(2.5)
cell2.margin_top = Cm(0.75)
cell2.margin_left = Cm(2.5)ppt.save('添加表格.pptx')

添加图表

from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE, XL_LEGEND_POSITION
from pptx.util import Cm
# 创建一页幻灯片
ppt = Presentation()
layout = ppt.slide_layouts[5]
slide = ppt.slides.add_slide(layout)
slide.shapes.title.text = '各分部参加考试人数'
# 定义图表数据
chart_data = CategoryChartData()
chart_data.categories = ['北京', '上海', '深圳']
chart_data.add_series('男', (19, 21, 16))
chart_data.add_series('女', (27, 15, 20))
# 在幻灯片中添加图表
left, top, width, height = Cm(2.7), Cm(4), Cm(20), Cm(12)
chart = slide.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, left, top, width, height, chart_data).chart
chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.RIGHT
chart.legend.include_in_layout = Falseppt.save('添加图表.pptx')

提取文本

from pptx import Presentation
ppt = Presentation('提取文本.pptx')
text_runs = []
for slide in ppt.slides:for shape in slide.shapes:if shape.has_text_frame:for paragraph in shape.text_frame.paragraphs:for run in paragraph.runs:text_runs.append(run.text)
text_runs = '\n'.join(text_runs)
with open('提取文本.txt', mode='w', encoding='utf-8') as f:f.write(text_runs)

添加备注

from pptx import Presentation
ppt = Presentation('添加备注.pptx')
for slide in ppt.slides:if not slide.has_notes_slide:notes_slide = slide.notes_slidetext_frame = notes_slide.notes_text_frametext_frame.text = '注意控制语速和时间'
ppt.save('添加备注1.pptx')

基于模板制作

import pptx
from pptx import Presentation
from pptx.dml.color import RGBColor
from pptx.util import Cm, Pt
from pptx.enum.text import MSO_ANCHOR, PP_ALIGN
ppt = Presentation('模板.pptx')
# 制作标题幻灯片
layout = ppt.slide_layouts[0]
slide = ppt.slides.add_slide(layout)
slide.shapes.placeholders[0].text = '个人作品展示'
slide.shapes.placeholders[1].text = '2022年1月'
# 制作第1页正文
layout = ppt.slide_layouts[1]
slide = ppt.slides.add_slide(layout)
slide.shapes.placeholders[0].text = '零基础学Python网络爬虫案例实战\n全流程详解(入门与提高篇)'
slide.shapes.placeholders[1].text = '定价:99.00元\nISBN:978-7-111-68368-1\n出版时间:2021年7月'
left, top, width, height = Cm(1.3), Cm(10.2), Cm(15), Cm(7)
content_box = slide.shapes.add_textbox(left, top, width, height)
text_frame = content_box.text_frame
text_frame.vertical_anchor = MSO_ANCHOR.TOP
text_frame.word_wrap = True
p = text_frame.paragraphs[0]
p.alignment = PP_ALIGN.JUSTIFY
r = p.add_run()
r.text = '网络爬虫是当今获取数据不可或缺的重要手段。本书讲解了爬虫的基础知识和必备技能,能够帮助零基础的读者快速入门并熟练使用爬虫。'
r.font.name = '方正兰亭细黑_GBK'
r.font.size = Pt(24)
r.font.color.rgb = RGBColor(0, 0, 0)
image_file = '封面1.png'
left, top, height = Cm(18), Cm(4.3), Cm(13.5)
slide.shapes.add_picture(image_file=image_file, left=left, top=top, height=height)
# 制作第2页正文
layout = ppt.slide_layouts[1]
slide = ppt.slides.add_slide(layout)
slide.shapes.placeholders[0].text = '零基础学Python网络爬虫案例实战\n全流程详解(高级进阶篇)'
slide.shapes.placeholders[1].text = '定价:89.80元\nISBN:978-7-111-68474-9\n出版时间:2021年7月'
left, top, width, height = Cm(1.3), Cm(10.2), Cm(15), Cm(7)
content_box = slide.shapes.add_textbox(left, top, width, height)
text_frame = content_box.text_frame
text_frame.vertical_anchor = MSO_ANCHOR.TOP
text_frame.word_wrap = True
p = text_frame.paragraphs[0]
p.alignment = PP_ALIGN.JUSTIFY
r = p.add_run()
r.text = '网络爬虫是当今获取数据不可或缺的重要手段。本书讲解了爬虫的进阶理论与技术,能够帮助读者进一步提升实际应用水平。' 
r.font.name = '方正兰亭细黑_GBK'
r.font.size = Pt(24)
r.font.color.rgb = RGBColor(0, 0, 0)
image_file = '封面2.png'
left, top, height = Cm(18), Cm(4.3), Cm(13.5)
slide.shapes.add_picture(image_file=image_file, left=left, top=top, height=height)
# 制作结尾幻灯片
layout = ppt.slide_layouts[2]
slide = ppt.slides.add_slide(layout)
slide.shapes.placeholders[0].text = '感谢关注'
ppt.save('作品展示.pptx')

参考

https://python-pptx.readthedocs.io/en/latest/index.html

这篇关于Python自动处理pptx:新建、另存、添加幻灯片、添加标题、插入文本图片图形、提取文本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/338824

相关文章

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

Java堆转储文件之1.6G大文件处理完整指南

《Java堆转储文件之1.6G大文件处理完整指南》堆转储文件是优化、分析内存消耗的重要工具,:本文主要介绍Java堆转储文件之1.6G大文件处理的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言文件为什么这么大?如何处理这个文件?分析文件内容(推荐)删除文件(如果不需要)查看错误来源如何避

Python自动化批量重命名与整理文件系统

《Python自动化批量重命名与整理文件系统》这篇文章主要为大家详细介绍了如何使用Python实现一个强大的文件批量重命名与整理工具,帮助开发者自动化这一繁琐过程,有需要的小伙伴可以了解下... 目录简介环境准备项目功能概述代码详细解析1. 导入必要的库2. 配置参数设置3. 创建日志系统4. 安全文件名处

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Java docx4j高效处理Word文档的实战指南

《Javadocx4j高效处理Word文档的实战指南》对于需要在Java应用程序中生成、修改或处理Word文档的开发者来说,docx4j是一个强大而专业的选择,下面我们就来看看docx4j的具体使用... 目录引言一、环境准备与基础配置1.1 Maven依赖配置1.2 初始化测试类二、增强版文档操作示例2.

Ubuntu 24.04启用root图形登录的操作流程

《Ubuntu24.04启用root图形登录的操作流程》Ubuntu默认禁用root账户的图形与SSH登录,这是为了安全,但在某些场景你可能需要直接用root登录GNOME桌面,本文以Ubuntu2... 目录一、前言二、准备工作三、设置 root 密码四、启用图形界面 root 登录1. 修改 GDM 配

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与