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)
  • 执行后的效果
相关推荐
山烛6 分钟前
KNN 算法中的各种距离:从原理到应用
人工智能·python·算法·机器学习·knn·k近邻算法·距离公式
guozhetao19 分钟前
【ST表、倍增】P7167 [eJOI 2020] Fountain (Day1)
java·c++·python·算法·leetcode·深度优先·图论
墨染点香25 分钟前
第七章 Pytorch构建模型详解【构建CIFAR10模型结构】
人工智能·pytorch·python
阿什么名字不会重复呢1 小时前
在线工具+网页平台来学习和操作Python与Excel相关技能
python·数据分析
Vertira1 小时前
python 阿里云 安装 dashscope的简介、安装
开发语言·python
gc_22992 小时前
学习Python中Selenium模块的基本用法(1:简介)
python·selenium
先做个垃圾出来………3 小时前
2116. 判断一个括号字符串是否有效
python
兮℡檬,3 小时前
房价预测|Pytorch
人工智能·pytorch·python
im_AMBER7 小时前
学习日志19 python
python·学习
mortimer10 小时前
安装NVIDIA Parakeet时,我遇到的两个Pip“小插曲”
python·github