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 = '男'
相关推荐
碧海蓝天20226 分钟前
C++法则21:避免将#include放在命名空间内部。
开发语言·c++
兮动人13 分钟前
Java应用全链路故障排查实战指南:从系统资源到JVM深度诊断
java·开发语言·jvm
R-sz24 分钟前
导出word并且插入图片
开发语言·c#·word
CodeWithMe24 分钟前
【读书笔记】《C++ Software Design》第一章《The Art of Software Design》
开发语言·c++
脑袋大大的42 分钟前
判断当前是否为钉钉环境
开发语言·前端·javascript·钉钉·企业应用开发
运器12344 分钟前
【一起来学AI大模型】PyTorch DataLoader 实战指南
大数据·人工智能·pytorch·python·深度学习·ai·ai编程
音元系统1 小时前
Copilot 在 VS Code 中的免费替代方案
python·github·copilot
超龄超能程序猿1 小时前
(5)机器学习小白入门 YOLOv:数据需求与图像不足应对策略
人工智能·python·机器学习·numpy·pandas·scipy
Wy. Lsy1 小时前
Kotlin基础学习记录
开发语言·学习·kotlin
Tanecious.2 小时前
C++--红黑树
开发语言·c++