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

# 问题
# 这里的尺寸有问题,不知道为什么。
相关推荐
2501_945423545 小时前
用Matplotlib绘制专业图表:从基础到高级
jvm·数据库·python
2301_793804695 小时前
使用Python处理计算机图形学(PIL/Pillow)
jvm·数据库·python
万里鹏程转瞬至5 小时前
InternVL(1~3.5版本)多模型大模型训练中的数据集构造总结
人工智能
badhope10 小时前
Mobile-Skills:移动端技能可视化的创新实践
开发语言·人工智能·git·智能手机·github
吴佳浩11 小时前
GPU 编号进阶:CUDA\_VISIBLE\_DEVICES、多进程与容器化陷阱
人工智能·pytorch·python
吴佳浩12 小时前
GPU 编号错乱踩坑指南:PyTorch cuda 编号与 nvidia-smi 不一致
人工智能·pytorch·nvidia
小饕12 小时前
苏格拉底式提问对抗315 AI投毒:实操指南
网络·人工智能
全栈凯哥12 小时前
18.Python中的导入类完全指南
python
卧蚕土豆12 小时前
【有啥问啥】OpenClaw 安装与使用教程
人工智能·深度学习
GoCodingInMyWay12 小时前
开源好物 26/03
人工智能·开源