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 = '男'
相关推荐
酉鬼女又兒几秒前
零基础快速入门前端蓝桥杯Web备考:BOM与定时器核心知识点详解(可用于备赛蓝桥杯Web应用开发)
开发语言·前端·javascript·职场和发展·蓝桥杯
望眼欲穿的程序猿1 分钟前
MacOS自定义安装Rust
开发语言·macos·rust
wjs20245 分钟前
CSS 动画:深入浅出的探索与实践
开发语言
wjs202410 分钟前
二分搜索树
开发语言
沐知全栈开发15 分钟前
Memcached delete 命令详解
开发语言
lly20240616 分钟前
Lua 基本语法
开发语言
源码之家22 分钟前
计算机毕业设计:Python汽车销量数据采集分析可视化系统 Flask框架 requests爬虫 可视化 车辆 大数据 机器学习 hadoop(建议收藏)✅
大数据·爬虫·python·django·flask·课程设计·美食
格林威26 分钟前
Baumer相机铝型材表面划伤长度测量:实现损伤量化评估的 5 个关键技术,附 OpenCV+Halcon 实战代码!
开发语言·人工智能·数码相机·opencv·计算机视觉·c#·工业相机
Roselind_Yi28 分钟前
【吴恩达2026 Agentic AI】面试向+项目实战(含面试题+项目案例)-2
人工智能·python·机器学习·面试·职场和发展·langchain·agent
2401_8274999928 分钟前
python核心语法01-数据存储与运算
java·数据结构·python