Python对ppt进行文本替换、插入图片、生成表格

目录

  • [1. 安装pptx](#1. 安装pptx)
  • [2. 文本替换和插入图片](#2. 文本替换和插入图片)
  • [3. 生成表格](#3. 生成表格)

1. 安装pptx

python 复制代码
pip install python-pptx

2. 文本替换和插入图片

  • 文本通过占位符例如{``{$xxx}}进行标记,然后进行替换;图片通过ppt中的图形和图片中的占位符进行标记
  • ppt如下
  • 具体实现
python 复制代码
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE

# 加载已有的PPT文件
prs = Presentation('ppt.pptx')
# 图片地址
new_image_path = 'python.png'
# 遍历每一张幻灯片
for slide in prs.slides:
    # 遍历幻灯片中的每一个形状
    for shape in slide.shapes:
        # 检查shape是不是文本框
        if shape.has_text_frame:
            if "{{$keyValue_text}}" in shape.text:
                # 替换文本
                shape.text = shape.text.replace("{{$keyValue_text}}", "文本替换后的内容")
        # 检查shape是不是表格
        if shape.has_table:
            for row in shape.table.rows:
                for cell in row.cells:
                    if "{{$keyValue_table}}" in cell.text:
                        cell.text = cell.text.replace('{{$keyValue_table}}', '表格替换后的内容')
        # 检查shape是不是一个图形(MSO_SHAPE_TYPE中还包含TABLE、TEXT_BOX)
        if shape.shape_type == MSO_SHAPE_TYPE.AUTO_SHAPE:
            if '{{$image}}' in shape.text:
                # 删除旧的图片占位符
                slide.shapes._spTree.remove(shape.element)
                # 在相同位置添加新图片{{$image}},同时设置图片尺寸
                left = shape.left
                top = shape.top
                width = shape.width
                height = shape.height
                slide.shapes.add_picture(new_image_path, left, top, width, height)

# 保存修改后的PPT文件
prs.save('new_ppt.pptx')
  • 执行后的效果

3. 生成表格

  • 定义一个表格,并设置表格的名称{``{$table}},表格只包含表头,根据提供的数据动态追加行并填充数据
  • 点击选择窗格,选择ppt中的表格,设置表格名称{``{$table}}
  • 具体实现
python 复制代码
from pptx import Presentation
from copy import deepcopy


# 找出表格
def find_table_by_identifier(slide, identifier):
    for shape in slide.shapes:
        if shape.has_table and shape.name == identifier:
            return shape.table
    return None


# 增加表格行
def add_row_to_table(table):
    # 复制最后一行
    new_row = deepcopy(table._tbl.tr_lst[-1])
    # 添加新行到表格
    table._tbl.append(new_row)


# 添加数据
def add_data_to_table(table, data):
    # 计算数据有多少条,增加相应数量的表格行
    rows_to_add = len(data)
    # 动态增加行
    for _ in range(rows_to_add):
        add_row_to_table(table)
    # 填充数据
    for i, row_data in enumerate(data):
        for j, cell_content in enumerate(row_data):
            cell_text = str(cell_content)
            table.cell(i + 1, j).text = cell_text


# 示例数据
data = [
    ["张三", 28, "北京"],
    ["李四", 22, "上海"],
    ["王五", 35, "广州"]
]

# 使用函数
ppt_path = 'table.pptx'
save_path = 'new_table.pptx'
prs = Presentation(ppt_path)

# 遍历每一张幻灯片寻找名称为{{$table}}的表格
for slide in prs.slides:
    table = find_table_by_identifier(slide, "{{$table}}")
    if table is not None:
        add_data_to_table(table, data)

prs.save(save_path)
  • 执行后的效果
相关推荐
氵文大师11 分钟前
A机通过 python -m http.server 下载B机的文件
linux·开发语言·python·http
程序员爱钓鱼20 分钟前
用 Python 批量生成炫酷扫光 GIF 动效
后端·python·trae
封奚泽优23 分钟前
下降算法(Python实现)
开发语言·python·算法
java1234_小锋28 分钟前
基于Python深度学习的车辆车牌识别系统(PyTorch2卷积神经网络CNN+OpenCV4实现)视频教程 - 自定义字符图片数据集
python·深度学习·cnn·车牌识别
爱笑的眼睛1135 分钟前
深入理解MongoDB PyMongo API:从基础到高级实战
java·人工智能·python·ai
辣椒酱.41 分钟前
jupyter相关
python·jupyter
郝学胜-神的一滴1 小时前
Python中常见的内置类型
开发语言·python·程序人生·个人开发
火白学安全1 小时前
《Python红队攻防零基础脚本编写:进阶篇(一)》
开发语言·python·安全·web安全·网络安全·系统安全
FreeCode1 小时前
LangGraph1.0智能体开发:运行时系统
python·langchain·agent
青瓷程序设计2 小时前
植物识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习