批量处理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)

# 问题
# 这里的尺寸有问题,不知道为什么。
相关推荐
阳光是sunny3 小时前
别再被 worktree 绕晕了!AI 编程时代你必须掌握的 Git 隔离神器
前端·人工智能·后端
冬奇Lab4 小时前
每日一个开源项目(第148篇):obsidian-skills - Obsidian CEO 亲写的 AI Agent 格式规范,让 Agent 不再破坏你的 Vault
人工智能·开源·资讯
ethantan4 小时前
AI Agent 组成:像人一样思考的智能体
人工智能·程序员·架构
冬奇Lab4 小时前
Workflow 系列(05):评测体系——三层测试结构与 Trace 追踪
人工智能·工作流引擎
ethantan5 小时前
一篇讲解AI Agent 组成:像人一样思考的智能体
人工智能·后端·程序员
apocelipes6 小时前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
Cosolar7 小时前
vLLM 生产级部署完全指南
人工智能·后端·架构
CodePlayer竟然被占用了7 小时前
被美国政府封杀18天,Claude Fable 5 回来了——但代价是什么?
人工智能
IT_陈寒7 小时前
垃圾回收器选错了,我的Java服务内存炸了
前端·人工智能·后端
smartpi8 小时前
SmartPi GPIO 脉冲与回复语执行时序指南
人工智能