41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
# -*- coding:utf-8 -*-
|
|
# @Author len
|
|
# @Create 2023/10/29 10:16
|
|
|
|
import os
|
|
from pptx import Presentation
|
|
from pptx.util import Inches
|
|
from pptx.dml.color import RGBColor
|
|
|
|
|
|
def add_image_as_slide_background(prs, image_path):
|
|
slide_layout = prs.slide_layouts[5] # 使用没有标题和内容的幻灯片布局
|
|
slide = prs.slides.add_slide(slide_layout)
|
|
|
|
# 设置图片大小和位置以匹配幻灯片大小
|
|
slide_width = prs.slide_width
|
|
slide_height = prs.slide_height
|
|
|
|
# 将图片添加到幻灯片上
|
|
slide.shapes.add_picture(image_path, 0, 0, width=slide_width, height=slide_height)
|
|
|
|
# 如果你希望有一个纯色背景,你可以设置以下背景颜色,否则可以注释掉这部分
|
|
background = slide.background
|
|
fill = background.fill
|
|
fill.solid()
|
|
fill.fore_color.rgb = RGBColor(255, 255, 255) # 设置为白色背景,你可以根据需要更改
|
|
|
|
|
|
folder_path = r"D:\Waste\嵌入式\数据集\背景" # 替换为你的文件夹路径
|
|
image_extensions = ['.jpg', '.jpeg', '.png'] # 常用图片文件扩展名,可以根据需要修改
|
|
|
|
prs = Presentation()
|
|
|
|
for file in os.listdir(folder_path):
|
|
if any(file.endswith(ext) for ext in image_extensions):
|
|
image_path = os.path.join(folder_path, file)
|
|
add_image_as_slide_background(prs, image_path)
|
|
|
|
prs.save("output.pptx")
|
|
|