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

# 问题
# 这里的尺寸有问题,不知道为什么。
相关推荐
hboot1 小时前
AI工程师第三课 - 机器学习基础
python·scikit-learn·kaggle
冬奇Lab2 小时前
每日一个开源项目(第139篇):Voicebox - 本地运行的开源 ElevenLabs 替代品
人工智能·开源·资讯
冬奇Lab2 小时前
Skill 系列(03):Skill 设计范式——5 个模式让输出从混沌到可预测
人工智能·开源·agent
IT_陈寒4 小时前
Python搞不定字符串编码?这破玩意坑我两小时!
前端·人工智能·后端
大模型真好玩6 小时前
什么是Loop Engineering?最通俗易懂的Loop Engineering核心概念
人工智能·agent·deepseek
叁两6 小时前
前端转型AI Agent该如何学习?(前置篇)
前端·人工智能·node.js
顾林海6 小时前
Agent入门阶段-编程基础-Python:流程控制
python·agent·ai编程
LaiYoung_6 小时前
🎁 送你一套超好用超实用的 FE AI-Coding Skills
前端·人工智能·开源
ZzT9 小时前
怎么做才不会被 AI 替代?
人工智能·程序员