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 = '男'
相关推荐
深蓝海拓1 分钟前
Qt:创建一套基于HSL颜色体系的颜色库
笔记·python·qt·学习·ui
步辞2 分钟前
CSS如何解决小屏幕上的长单词截断版面
jvm·数据库·python
Thanks_ks4 分钟前
【第 001 讲】计算机底层基础与 Python 生态全景:硬件架构 | 语言演进 | 执行机制 | 语言特性 | 解释器 | 版本策略
python·编程语言·python入门·计算机基础·解释器·底层原理·cpython
qq_460978405 分钟前
如何在无向图中找出从任意节点可达的所有节点(连通分量识别)
jvm·数据库·python
大蚂蚁2号9 分钟前
本地视频转文字|video2text
python·音视频·视频转文本
XiYang-DING11 分钟前
【Java EE】工厂模式
java·python·java-ee
龙俊杰的读书笔记12 分钟前
一文读懂python并发&并行编程--以xinference框架应用为例
开发语言·网络·python
liulilittle14 分钟前
递归复制搜索所有的lua文件到指定目录
java·开发语言·lua·cmd
测试199825 分钟前
Selenium自动化测试框架的搭建
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例