python+pptx:(二)添加图片、表格、形状、模版渲染

目录

图片

表格

合并单元格

填充色、边距

写入数据

形状

模版渲染


上一篇:python+pptx:(一)占位符、文本框、段落操作_python输出ppt母版占位符标号-CSDN博客

python 复制代码
from pptx import Presentation
from pptx.util import Cm, Inches, Mm, Pt
from pptx.dml.color import RGBColor
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE, PP_PARAGRAPH_ALIGNMENT, MSO_VERTICAL_ANCHOR
from pptx.enum.shapes import MSO_SHAPE

# 创建一个新的幻灯片文档
file_path = r'C:\Users\Administrator\Desktop\testfile\测试文件\test.pptx'
prs = Presentation(file_path)  # 创建PPT文件对象,没有参数为创建新的PPT,有参数表示打开已有PPT文件对象

图片

python 复制代码
picture_path = r'C:\Users\Administrator\Desktop\testfile\Photos\4.jpg'
p_left, p_top, p_width, p_height = Cm(5), Cm(5), Cm(5), Cm(8)  # left\top图片左上角位置,width\height图片宽高
prs.slides[4].shapes.add_picture(picture_path, p_left, p_top, p_width, p_height)  # left\top\width\height

表格

python 复制代码
# 参数,rows: int, cols: int, left: Length, top: Length, width: Length, height: Length    
prs.slides[6].shapes.title.text = '添加表格测试'                                             
r, c, t_left, t_top, t_width, t_height = 6, 5, Cm(5), Cm(5), Cm(20), Cm(5)             
table = prs.slides[6].shapes.add_table(r, c, t_left, t_top, t_width, t_height).table   
合并单元格
python 复制代码
merge_cell_start = table.cell(2, 2)     
merge_cell_end = table.cell(3, 3)       
merge_cell_start.merge(merge_cell_end)  
填充色、边距
python 复制代码
# 设置背景/前景色样式:同文本框设置,back_color,fore_color 
pp = table.cell(0, 0).fill           
pp.solid()                           
pp.fore_color.rgb = RGBColor(247, 150
                                     
# 设置边距:同文本框设置                        
table.cell(1, 1).margin_top = Cm(1)  
写入数据
python 复制代码
for row in range(r):
    for col in range(c):
        t_c = table.cell(row, col)
        t_c.text = str((row + 1) * (col + 1))  # 直接写入内容(内容必须是str类型)

        # cl = table.cell(row, col).text_frame.add_paragraph()  # 可以通过添加段落的方式修改表格内容样式,设置方式同段落设置,测试发现会多出换行
        # cl.text = str((row+1) * (col+1))
        # cl.alignment = PP_PARAGRAPH_ALIGNMENT.CENTER
        # cl.font.color.rgb = RGBColor(100, 200, 50)

形状

python 复制代码
left, top = Cm(5), Cm(5)
width, height = Cm(3), Cm(3)
for i in range(5):
    left = left + width
    sha = prs.slides[7].shapes.add_shape(MSO_SHAPE.CAN, left, top, width, height)  # MSO_SHAPE对象下有不同的形状可选择
    sha.text = f'ceshi{i + 1}'

模版渲染

模版渲染,简单使用,可以通过提取占位符或文本内容处理数据,下面演示通过处理文本内容替换数据操作。

python 复制代码
for placeholder in prs.slides[0].shapes:
    # placeholder.text = '设置占位符测试'  # 设置占位符内容
    print(placeholder.text_frame.text)  # 获取占位符内容
    if placeholder.text_frame.text == '姓名':
        placeholder.text_frame.text = '张三'
    if placeholder.text_frame.text == '性别':
        placeholder.text_frame.text = '男'
相关推荐
少控科技4 小时前
QT第6个程序 - 网页内容摘取
开发语言·qt
darkb1rd4 小时前
八、PHP SAPI与运行环境差异
开发语言·网络安全·php·webshell
历程里程碑4 小时前
Linux20 : IO
linux·c语言·开发语言·数据结构·c++·算法
郝学胜-神的一滴5 小时前
深入浅出:使用Linux系统函数构建高性能TCP服务器
linux·服务器·开发语言·网络·c++·tcp/ip·程序人生
承渊政道5 小时前
Linux系统学习【Linux系统的进度条实现、版本控制器git和调试器gdb介绍】
linux·开发语言·笔记·git·学习·gitee
JQLvopkk5 小时前
C# 轻量级工业温湿度监控系统(含数据库与源码)
开发语言·数据库·c#
玄同7655 小时前
从 0 到 1:用 Python 开发 MCP 工具,让 AI 智能体拥有 “超能力”
开发语言·人工智能·python·agent·ai编程·mcp·trae
czy87874755 小时前
深入了解 C++ 中的 `std::bind` 函数
开发语言·c++
消失的旧时光-19435 小时前
从 Kotlin 到 Dart:为什么 sealed 是处理「多种返回结果」的最佳方式?
android·开发语言·flutter·架构·kotlin·sealed
yq1982043011565 小时前
静思书屋:基于Java Web技术栈构建高性能图书信息平台实践
java·开发语言·前端