批量处理ppt中的图片大小并移动位置

1 需求

从网页上复制了好多图片到ppt中,但是大小和位置需要移动。

手动太麻烦了,于是编写了一段代码批量处理。

这也充分体现一点:有重复工作时,先想想是否可以用编程来解决。

2 代码

python 复制代码
# 运行pip install python-pptx,安装库
from pptx import Presentation
from pptx.util import Inches


def resize_and_move_images(ppt_file, new_width, new_height, new_left, new_top):
    # 加载现有的 PPT 文件
    prs = Presentation(ppt_file)

    # 遍历每张幻灯片
    for slide in prs.slides:
        # 遍历幻灯片中的所有形状
        for shape in slide.shapes:
            # 检查形状是否为图片
            if shape.shape_type == 13:  # 13 代表图片
                # 调整图片大小
                # shape.width = Inches(new_width)
                shape.width = Inches(new_width)
                shape.height = Inches(new_height)
                # 移动图片到新位置
                shape.left = Inches(new_left)
                shape.top = Inches(new_top)

    # 保存修改后的文件
    prs.save('modified_presentation.pptx')


# 使用示例
resize_and_move_images('origin.pptx', 13.4, 7.51, 0, 0)

# 问题
# 这里的尺寸有问题,不知道为什么。
相关推荐
zzzzzz3102 分钟前
一篇文章发五个平台:我用 TRAE Work 搭了条内容分发流水线,2 小时变 20 分钟
人工智能·markdown
xushichang123_13 分钟前
如何通过云平台与架构优化,解决 Agentic AI 应用 Token 消耗过高问题?
人工智能
用户83562907805114 分钟前
如何使用 Python 将 PowerPoint 转换为 PDF 文档
后端·python
菜冻鱼14 分钟前
Python-pytorch-高级技巧
开发语言·人工智能·pytorch·python·深度学习·神经网络·聚类
lpfasd12316 分钟前
python webview打包版卡死、开发版正常
开发语言·python
用户83562907805125 分钟前
如何使用 Python 为 PowerPoint 幻灯片添加切换效果
后端·python
菜冻鱼33 分钟前
Python-pytorch-模型保存与加载
开发语言·人工智能·pytorch·python·深度学习·机器学习
程序员cxuan1 小时前
Claude Code :如何最大化你的 Session 价值
人工智能·后端·程序员
亿牛云爬虫专家1 小时前
爬虫调度系统设计:用 Celery 实现按媒体权重动态调整的抓取频率控制
爬虫·python·隧道代理·舆情采集·媒体权重·频率控制·ip自动轮换
MatrixOrigin1 小时前
MatrixOne Git4Data 技术详解(十一)·大模型篇:SFT 数据 curation——可审计、可复现的数据清洗
人工智能·矩阵起源·数据底座·git4data