

生成给一个新的PPT文件

更改背景颜色


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

保存,改名字

参考:
1、AI对话大师

2、专业代码


- '''
- 家长会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\照片')


