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

# 问题
# 这里的尺寸有问题,不知道为什么。
相关推荐
不才不才不不才8 分钟前
Spring AI 实战:聊天、提示词、记忆三件套
java·人工智能·spring·ai
汤姆yu13 分钟前
Anthropic Claude Fable 5 深度解析
人工智能·ai·大模型·智能体·视频模型
skylar016 分钟前
小白1分钟安装flash-attn
开发语言·python
JustNow_Man19 分钟前
psmux快捷键
人工智能·python
默子昂24 分钟前
ollama 自定义ui
开发语言·python·ui
abcy07121324 分钟前
Python中使用FastAPI和HDFS进行异步文件上传
python·fastapi
abcy07121325 分钟前
flask hdfs 异步上传图文教程csdn
python·flask
神奇的小猴程序员25 分钟前
提升 AI 与开发效率!两款实用 Skill 开源工具 FunctionCool-Skill & StyleCool-Skill 深度体验
人工智能·开源·s
哈哈,柳暗花明44 分钟前
人工智能专业术语详解(L)
人工智能·专业术语
在放️1 小时前
Python 爬虫 · PyQuery 模块基础
爬虫·python