python自动化办公之python操作PPT(四)

简介: python自动化办公之python操作PPT(四)

⑥ 添加表格

shapes.add_table(rows, cols, left, top, width, height)

from pptx import Presentation
from pptx.util import Cm, Pt
prs = Presentation()
# 使用第七个版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)
shapes = slide.shapes
rows, cols = 5, 3
left = top = Cm(5)
width = Cm(18)
height = Cm(3)
table = shapes.add_table(rows, cols, left, top, width, height).table
table.columns[0].width = Cm(6)
table.columns[1].width = Cm(2)
table.columns[2].width = Cm(2)
table.rows[0].height = Cm(2)
data = [
    ["姓名","性别","成绩"],
    ["张三","男",96],
    ["李四","女",87],
    ["王五","女",90],
    ["赵六","男",78]
]
for row in range(rows):
    for col in range(cols):
        table.cell(row,col).text = str(data[row][col])
prs.save("插入表格.pptx")


结果如下:

image.png


5、PPT文档内容样式批量调整

1)文本框位置的调整

上面我们已经知道怎么添加文本框,现在我们需要做的就是,怎么调整文本框的位置。


from pptx import Presentation
from pptx.util import Cm, Pt
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE
prs = Presentation()
# 使用第七个版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)
left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
tf.text = "这是一段文本框里面的文字"
# ----------------------------------------- #
tf.margin_bottom = Cm(0.1) # 下边距
tf.margin_left = 0 # 下边距
# 一定要导入MSO_ANCHOR这个库
tf.vertical_anchor = MSO_ANCHOR.BOTTOM # 对齐文本方式:底端对齐
tf.word_wrap = True # 框中的文字自动换行
prs.save("文本框样式的调整.pptx")


结果如下:

image.png


2)文本框背景颜色调整

from pptx import Presentation
from pptx.util import Cm, Pt
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE
from pptx.dml.color import RGBColor
prs = Presentation()
# 使用第七个版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)
left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
tf.text = "这是一段文本框里面的文字"
# -------------------------------------- #
tf.margin_bottom = Cm(0.1) # 下边距
tf.margin_left = 0 # 下边距
tf.vertical_anchor = MSO_ANCHOR.BOTTOM  
tf.word_wrap = True # 框中的文字自动换行
# -------------------------------------- #
fill = text_box.fill
fill.solid()
# 使用之前一定要导入RGBColor这个库
fill.fore_color.rgb = RGBColor(247, 150, 70)
prs.save("文本框背景色的调整.pptx")


结果如下:

image.png


3)文本框边框样式调整

from pptx import Presentation
from pptx.util import Cm, Pt
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE
from pptx.dml.color import RGBColor
prs = Presentation()
# 使用第七个版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)
left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
tf.text = "这是一段文本框里面的文字"
# -------------------------------------- #
tf.margin_bottom = Cm(0.1) # 下边距
tf.margin_left = 0 # 下边距
tf.vertical_anchor = MSO_ANCHOR.BOTTOM  
tf.word_wrap = True # 框中的文字自动换行
# -------------------------------------- #
fill = text_box.fill
fill.solid()
# 使用之前一定要导入RGBColor这个库
fill.fore_color.rgb = RGBColor(247, 150, 70)
# -------------------------------------- #
line = text_box.line
line.color.rgb = RGBColor(255, 0, 0)
line.width = Cm(0.3)
prs.save("文本框边框样式调整.pptx")


结果如下:

image.png


4)段落对其调整

from pptx import Presentation
from pptx.enum.text import PP_ALIGN
prs = Presentation()
# 使用第七个版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)
left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
# ---------------------------- #
p = tf.add_paragraph()
p.text = "这是第二段文字"
p.alignment = PP_ALIGN.LEFT
prs.save("段落对其调整.pptx")


当然这里还有一些其他样式的调整,和word很类似,就不一一叙述了。

image.png


5)字体样式调整

image.png

代码如下:


from pptx import Presentation
from pptx.util import Cm, Pt
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN
prs = Presentation()
# 使用第七个版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)
left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
# ---------------------------- #
p = tf.add_paragraph()
p.text = "这是第二段文字"
p.alignment = PP_ALIGN.LEFT
# ------------------------------------- #
p.font.bold = True
p.font.name = "宋体"
p.font.color.rgb = RGBColor(247, 150, 70)
p.font.size = Pt(30)
prs.save("字体样式调整.pptx")


结果如下:

image.png

相关文章
|
安全 JavaScript 开发者
Python 自动化办公神器|一键转换所有文档为 PDF
本文介绍一个自动化批量将 Word、Excel、PPT、TXT、HTML 及图片转换为 PDF 的 Python 脚本。支持多格式识别、错误处理与日志记录,适用于文档归档、报告整理等场景,大幅提升办公效率。仅限 Windows 平台,需安装 Office 及相关依赖。
729 0
|
Web App开发 存储 前端开发
Python+Selenium自动化爬取携程动态加载游记
Python+Selenium自动化爬取携程动态加载游记
|
机器学习/深度学习 数据采集 API
Python自动化解决滑块验证码的最佳实践
Python自动化解决滑块验证码的最佳实践
|
存储 数据采集 监控
Python定时爬取新闻网站头条:从零到一的自动化实践
在信息爆炸时代,本文教你用Python定时爬取腾讯新闻头条,实现自动化监控。涵盖请求、解析、存储、去重、代理及异常通知,助你构建高效新闻采集系统,适用于金融、电商、媒体等场景。(238字)
1848 2
|
数据采集 人工智能 API
推荐一款Python开源的AI自动化工具:Browser Use
Browser Use 是一款基于 Python 的开源 AI 自动化工具,融合大型语言模型与浏览器自动化技术,支持网页导航、数据抓取、智能决策等操作,适用于测试、爬虫、信息提取等多种场景。
1776 4
推荐一款Python开源的AI自动化工具:Browser Use
|
数据采集 监控 Shell
无需Python:Shell脚本如何成为你的自动化爬虫引擎?
Shell脚本利用curl/wget发起请求,结合文本处理工具构建轻量级爬虫,支持并行加速、定时任务、增量抓取及分布式部署。通过随机UA、异常重试等优化提升稳定性,适用于日志监控、价格追踪等场景。相比Python,具备启动快、资源占用低的优势,适合嵌入式或老旧服务器环境,复杂任务可结合Python实现混合编程。
|
人工智能 安全 虚拟化
Cua:Mac用户狂喜!这个开源框架让AI直接接管你的电脑,快速实现AI自动化办公
Cua是一个结合高性能虚拟化与AI代理能力的开源框架,能在Apple Silicon上以接近原生性能运行虚拟机,并让AI直接操作系统应用。
2576 17
Cua:Mac用户狂喜!这个开源框架让AI直接接管你的电脑,快速实现AI自动化办公
|
Python
Python 办公实战:用 python-docx 自动生成 Word 文档
本文详解如何使用 python-docx 库实现 Word 文档自动化生成,涵盖环境搭建、文档创建、格式设置、表格与图片处理、模板填充、批量生成及性能优化等实战技巧,助你高效完成办公场景中的文档自动化任务。
3338 1
|
数据采集 存储 前端开发
Python爬虫自动化:批量抓取网页中的A链接
Python爬虫自动化:批量抓取网页中的A链接
|
存储 数据采集 数据可视化
Python自动化分析知网文献:爬取、存储与可视化
Python自动化分析知网文献:爬取、存储与可视化

热门文章

最新文章

推荐镜像

更多