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 = '男'
相关推荐
点云SLAM2 分钟前
Boost库中Boost.PropertyTree使用和实战示例
开发语言·c++·josn·boost库·参数读取
lly2024067 分钟前
CSS3 分页技术解析
开发语言
CodeCraft Studio13 分钟前
国产化Excel开发组件Spire.XLS教程:Python将列表导出为CSV文件(含一维/二维/字典列表)
开发语言·python·excel·csv·spire.xls·列表导出为csv
Blossom.11818 分钟前
大模型知识蒸馏实战:从Qwen-72B到Qwen-7B的压缩艺术
大数据·人工智能·python·深度学习·算法·机器学习·pygame
guygg8827 分钟前
Alpha稳定分布概率密度函数的MATLAB实现
开发语言·matlab
刘逸潇200534 分钟前
Socket:TCP/UDP通信详解
python·websocket·udp·tcp
The_Second_Coming1 小时前
Python 学习笔记:基础篇
运维·笔记·python·学习
诗句藏于尽头1 小时前
python实战学习记录
python·学习
草莓熊Lotso1 小时前
C++ 二叉搜索树(BST)完全指南:从概念原理、核心操作到底层实现
java·运维·开发语言·c++·人工智能·经验分享·c++进阶
oliveira-time1 小时前
单例模式中的饿汉式
java·开发语言