【办公类-40-02】20240311 python模仿PPT相册功能批量插入照片,更改背景颜色 (家长会系列二)

作品展示------用Python插入PPT相册

背景需求:

马上就要家长会,我负责做会议前的照片滚动PPT,通常都是使用PPT的相册功能批量导入照片,

生成给一个新的PPT文件

更改背景颜色

设置4秒间隔,应用到全部

保存,改名字

我想用Python实现"PPT相册插入图片"的效果

参考:

1、AI对话大师

2、专业代码

2、python程序24:批量导入图片至一个PPT文件 - 知乎有时候一个在做PPT的时候,可能需要很多的图片导入到文件中,那么怎么做才能准确又高效呢?今天我们写一个python用于批量导入图片至每一页PPT中,并填充图片自动匹配纵横比。 这里需要引入模块: pip install pyth...https://zhuanlan.zhihu.com/p/642958843?utm_id=0

素材准备:

代码展示

python 复制代码
'''
家长会PPT:相册功能插入照片(保持原图长宽比,但适应幻灯片大小),背景颜色更改
作者:
1、https://zhuanlan.zhihu.com/p/642958843?utm_id=0
2、aI对话大师 、阿夏
时间:2024年3月11日
'''


from pptx import Presentation
from PIL import Image
import os

def create_slideshow(images_folder):
    prs = Presentation()

     # 设置幻灯片背景颜色为浅蓝色
    for slide in prs.slides:
        background = slide.background
        fill = background.fill
        fill.solid()
        fill.fore_color.rgb = (79, 129, 189)  # 浅蓝色的RGB值

    # 设置每页切换时间为4秒
    prs.slide_milliseconds = 4000


    # 获取文件夹中的所有图片文件
    image_files = [file for file in os.listdir(images_folder) if file.endswith(('.jpg', '.jpeg', '.png'))]

    for image_file in image_files:
        # 打开图片并获取其宽度和高度
        image_path = os.path.join(images_folder, image_file)
        image = Image.open(image_path)
        width, height = image.size

        # 设置16:9比例的幻灯片大小
        prs.slide_width = 16 * 914400  # 16 inches converted to EMUs
        prs.slide_height = 9 * 914400   # 9 inches converted to EMUs

        # 计算图片在幻灯片上的大小和位置
        slide_width = prs.slide_width
        slide_height = prs.slide_height
        slide_ratio = slide_width / slide_height
        image_ratio = width / height

        if slide_ratio > image_ratio:
            # 在幻灯片上添加带有黑色边框的图片
            left = (slide_width - slide_height * image_ratio) / 2
            top = 0
            prs.slides.add_slide(prs.slide_layouts[1])
            slide = prs.slides[-1]
            slide.shapes.add_picture(image_path, left, top, slide_height * image_ratio, slide_height)
        else:
            # 在幻灯片上添加带有黑色边框的图片
            left = 0
            top = (slide_height - slide_width / image_ratio) / 2
            prs.slides.add_slide(prs.slide_layouts[1])
            slide = prs.slides[-1]
            slide.shapes.add_picture(image_path, left, top, slide_width, slide_width / image_ratio)

    from pptx.dml.color import RGBColor

    # 设置幻灯片背景颜色为浅蓝色
    for slide in prs.slides:
        background = slide.background
        fill = background.fill
        fill.solid()
        fill.fore_color.rgb = RGBColor(173, 216, 230)  # 浅蓝色的RGB值

    # 设置每页切换时间为4秒
    prs.slide_milliseconds = 4000

    # 保存幻灯片为PPT文件
    prs.save(r'D:\家长会PPT\20240313中4班家长会相册.pptx')

# 传入存储图片的文件夹路径
create_slideshow(r'D:\家长会PPT\照片')

说明:

1向AI问了无数次,本文终于实现了"相册插入图片"+"背景颜色修改""自动保存pptx名称"三个功能,

2、幻灯片每页切换和插入循环音乐还没有实现......

感悟:

用Python代码几乎5秒就生成了PPT相册导入的照片的效果,比起手动点击的诸多步骤------新建PPT-"相册"-添加路径-创建-更改背景色-应用到全部等,不知道快多少,还可以反复生成。

技术提升效率,改变思维!!!

相关推荐
新子y9 小时前
【小白笔记】PyTorch 和 Python 基础的这些问题
pytorch·笔记·python
我是李武涯9 小时前
PyTorch DataLoader 高级用法
人工智能·pytorch·python
Lynnxiaowen9 小时前
今天我们开始学习python语句和模块
linux·运维·开发语言·python·学习
ThreeAu.9 小时前
pytest 实战:用例管理、插件技巧、断言详解
python·单元测试·pytest·测试开发工程师
资源补给站10 小时前
服务器高效操作指南:Python 环境退出与 Linux 终端快捷键全解析
linux·服务器·python
一苓二肆10 小时前
代码加密技术
linux·windows·python·spring·eclipse
青春不败 177-3266-052010 小时前
AI+Python驱动的无人机生态三维建模与碳储、生物量、LULC估算技术
人工智能·python·无人机·生态学·遥感·多光谱遥感
将车24411 小时前
selenium实现自动化脚本的常用函数
python·selenium·自动化
ZhengEnCi11 小时前
Excel 文件结构完全指南-从基础概念到 Python 读取的实用宝典
python·excel
一百天成为python专家11 小时前
python爬虫入门(小白五分钟从入门到精通)
开发语言·爬虫·python·opencv·yolo·计算机视觉·正则表达式