利用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')