python生成PPT幻灯片程序

"""
   生成PPT幻灯片程序.py
   本程序把images下面的0.png,1.png,2.png.....都添加到一个PPT幻灯片文件中.
   pptx模块安装:pip3 install python-pptx
"""

import os
import pptx
from pptx.util import Inches
from random import randint,choice

letters = [chr(x) for x in range(48,127)]

def autostring():
    """自动生成一个字符串"""
    s = ''.join([choice(letters) for i in range(1,randint(10,1000))])
    return s

def make_images_pptx(image_path,out_path):
    """
    生成幻灯片文件,插入很多图片后加上随机字符串防大数据识别发同一文件
    """
    pptFile = pptx.Presentation()
    # 交换纵横比,相当于设置成纵向幻灯片
    pptFile.slide_width,pptFile.slide_height = pptFile.slide_height,pptFile.slide_width
     
    # 按图片编号顺序导入
    for index in range(15):
        fn =  path + os.sep + str(index) + ".png"
        # 添加一个幻灯片
        slide = pptFile.slides.add_slide(pptFile.slide_layouts[1])

        # 命令格式: add_picture(image_file,left,top,width,height)
        slide.shapes.add_picture(fn, Inches(0), Inches(0), Inches(7.5), Inches(10))

    slide= pptFile.slides.add_slide(pptFile.slide_layouts[1])

    body_shape = slide.shapes.placeholders          # body_shape为本页ppt中所有shapes
    body_shape[0].text = '请忽略以下\n自动生成内容'  # 在第一个文本框中添加文字
    body_shape[1].text =  autostring() # 在第二个文本框中文字框架内添加文字

    pptFile.save(out_path)

path = os.getcwd() + os.sep + "images"
out = os.getcwd() + os.sep + "out" + os.sep


for i in range(100):
    make_images_pptx(path,f'{out}Python创意编程汇编之turtle篇.pptx_{i}.pptx')
 

 

关于李兴球

李兴球的博客是Python创意编程原创博客
此条目发表在python分类目录。将固定链接加入收藏夹。

发表回复