python批量生成ppt

 利用pptx批量生成ppt,其实只是使用了模板,然后把图片变成固定格式,塞到固定位置

唯一值得说的是prs.slides.add_slide(prs.slide_layouts[3]),这个序号3是指打开的ppt所对应的模板的序号(而不是ppt的页数)

# -*- coding: utf-8 -*-
"""
Created on Wed May  6 14:07:51 2020

@author: XX
"""

from pptx import Presentation
from pptx.util import Cm
from pptx.shapes.picture import Picture
import os
import pandas as pd
preppt='C:\\Users\\验证.pptx'
#preppt=preppt.encode('gbk')
prs= Presentation(preppt)
print(len(prs.slide_layouts))
points=[2,3,4,5,6,7,8,9,10,11,12,13,40,42,45]
rsimage='C:\\Users\\图'
rslist = os.listdir(rsimage) #列出文件夹下所有的目录与文件
rslist=sorted(rslist,key=lambda x:int(x.split('-')[0]))
proimage='C:\\points\\LCLUPNG'
prolist = os.listdir(proimage) #列出文件夹下所有的目录与文件
for i in points:
    
    #下面的是处理ppt
    copied_slide = prs.slides.add_slide(prs.slide_layouts[3])
    img_path = os.path.join(rsimage,rslist[i-1])
    print(img_path)
#    # 文件路径
    left, top, width, height= Cm(1), Cm(3), Cm(30), Cm(16) 
#    # 预设位置及大小
    pic= copied_slide.shapes.add_picture(img_path, left, top, height=height)
    
    for j in range(0,8):
        
        if j%2==0: #0,2,4,6
            copied_slide = prs.slides.add_slide(prs.slide_layouts[3])
            top, width, height= Cm(3), Cm(10), Cm(7.5) 
        else:
            top, width, height= Cm(12), Cm(10), Cm(7.5) 
        
        left=Cm(0.5)
        img_path = os.path.join(proimage,str(i)+'-方向'+str(j+1)+'-行列I.png')
        pic= copied_slide.shapes.add_picture(img_path, left, top, height=height)
        print(str(i)+'-方向'+str(j+1)+'-行列Index取值.png')
        left=Cm(14)
        img_path = os.path.join(proimage,str(i)+'-方向'+str(j+1)+'-线.png')
        pic= copied_slide.shapes.add_picture(img_path, left, top, height=height)
        print(str(i)+'-方向'+str(j+1)+'-延高线.png')
        left=Cm(24)
        img_path = os.path.join(proimage,str(i)+'-方向'+str(j+1)+'-延示Z0.png')
        pic= copied_slide.shapes.add_picture(img_path, left, top, height=height)
        print(str(i)+'-方向'+str(j+1)+'-延示Z0.png')
#    # 在指定位置按预设值添加图片
#left= top= width= height= Inches(5)  
## 预设位置及大小
#textbox= slide.shapes.add_textbox(left, top, width, height)  
## left,top为相对位置,width,height为文本框大小
#textbox.text= 'this is a new textbox'
## 文本框中文字
#new_para= textbox.text_frame.add_paragraph()  
## 在新文本框中添加段落
#new_para.text= 'this is second para in textbox'
## 段落文字
prs.save('C:\\Users\\test.pptx')

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

就是一只白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值