【python办公自动化(17)】利用python向PPT文档中写入内容(证书生成器)

幻灯片的母版行及占位符

PPT母版

【python办公自动化(17)】利用python向PPT文档中写入内容(证书生成器)_第1张图片

占位符

【python办公自动化(17)】利用python向PPT文档中写入内容(证书生成器)_第2张图片

添加Slide和内容

查看母版信息

import os
os.chdir("D:\\python_major\\auto_office17")
from pptx import Presentation

prs = Presentation("奖学金证书模板.pptx")
slide = prs.slides.add_slide(prs.slide_layouts[0])
for shape in slide.placeholders:
	phf = shape.placeholder_format
	print("%s--%s--%s" %(phf.idx,shape.name,phf.type))
	#print(f'{phf.idx}--{shape.name}--{phf.type}')
	#print("{}--{}--{}".format(phf.idx,shape.name,phf.type))

不同的格式化的方式,最后输出的结果是一致的
【python办公自动化(17)】利用python向PPT文档中写入内容(证书生成器)_第3张图片
其中.slide_layouts[0]代表的是母版下的的第一张样板(红线框的那个),如下
【python办公自动化(17)】利用python向PPT文档中写入内容(证书生成器)_第4张图片

向占位符内填写内容

shape.text = 字符串
prs.save(文件路径)
import os
os.chdir("D:\\python_major\\auto_office17")
from pptx import Presentation

prs = Presentation("奖学金证书模板.pptx")
slide = prs.slides.add_slide(prs.slide_layouts[0])
for shape in slide.placeholders:
	phf = shape.placeholder_format
	print("%s--%s--%s" %(phf.idx,shape.name,phf.type))
	shape.text = f'{phf.idx}--{phf.type}'

prs.save('test.pptx')

–> 输出结果为:

根据占位符ID确定要填哪里

slide.placeholders[占位符ID]
prs = Presentation("奖学金证书模板.pptx")
slide = prs.slides.add_slide(prs.slide_layouts[0])

winner_name = slide.placeholders[0]
certificate_type = slide.placeholders[1]
this_is_to_certify_that = slide.placeholders[20]
winning_reaon = slide.placeholders[19]
award_presenter = slide.placeholders[17]
award_date = slide.placeholders[21]

修改占位符里的内容

Placeholder.text = 字符串
import datetime
winner_name.text = '李雷'
certificate_type.text = '三好学生奖状'
this_is_to_certify_that.text = '兹证明'
winning_reaon.text = '身体好、脾气好、长得好'
award_presenter.text = '超不正经协会'
award_date.text = str(datetime.datetime.now().date())
prs.save('test.pptx')

–> 输出结果为:
【python办公自动化(17)】利用python向PPT文档中写入内容(证书生成器)_第5张图片

添加段落

import os
os.chdir("D:\\python_major\\auto_office17")
from pptx import Presentation

prs = Presentation()
bullet_slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(bullet_slide_layout)
shapes = slide.shapes

title_shape = shapes.title
body_shape = shapes.placeholders[1]

title_shape.text = '添加项目符号列表页'

tf = body_shape.text_frame
tf.text = '带圆点的项目符号行1'

p = tf.add_paragraph()
p.text = '带圆点的项目符号行2'

p = tf.add_paragraph()
p.text = '带圆点的项目符号行3'

prs.save('test2.pptx')

–> 输出结果为:
【python办公自动化(17)】利用python向PPT文档中写入内容(证书生成器)_第6张图片

设定层级关系

paragraph.level = 层级数 (0为最顶层,默认)
p = tf.add_paragraph()
p.text = '带圆点的项目符号行2'
p.level = 1

p = tf.add_paragraph()
p.text = '带圆点的项目符号行3'
p.level = 2

–> 输出结果为:
【python办公自动化(17)】利用python向PPT文档中写入内容(证书生成器)_第7张图片

添加一个文本框

slide.shapes.add_textbox(left,top,width,height)
import os
os.chdir("D:\\python_major\\auto_office17")
from pptx import Presentation
from pptx.util import Cm,Pt

prs = Presentation()
bullet_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(bullet_slide_layout)

left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left,top,width,height)
tf = text_box.text_frame
tf.text = '这是一段文本框里的文字'

p = tf.add_paragraph()
p.text = "这是第二段文字,加粗,字号"
p.font.bold = True
p.font.size = Pt(40)
prs.save('test3.pptx')

–> 输出结果为:
【python办公自动化(17)】利用python向PPT文档中写入内容(证书生成器)_第8张图片

添加图片

slide.shapes.add_picture(图片路径,距离左边,距离顶端,宽度,高度)
import os
os.chdir("D:\\python_major\\auto_office17")
from pptx import Presentation
from pptx.util import Cm

prs = Presentation()
bullet_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(bullet_slide_layout)

left = top = Cm(3)
height = Cm(5)
pic = slide.shapes.add_picture("demo.png",left,top,height = height)

left = Cm(5)
top = Cm(5)
height = Cm(5.5)

pic = slide.shapes.add_picture("demo.png",left,top,height)

prs.save('test4.pptx')

–> 输出结果为:
【python办公自动化(17)】利用python向PPT文档中写入内容(证书生成器)_第9张图片

添加表格

shape.add_tanle(rows,cols,left,top,width,height)
import os
os.chdir("D:\\python_major\\auto_office17")
from pptx import Presentation
from pptx.util import Cm

prs = Presentation()
bullet_slide_layout = prs.slide_layouts[6]
shapes = prs.slides.add_slide(bullet_slide_layout).shapes

rows,cols = 4,2
left = top = Cm(5)
height = Cm(3)
width = Cm(18)

table = shapes.add_table(rows,cols,left,top,width,height).table

table.columns[0].width = Cm(6)
table.columns[1].width = Cm(4)
table.rows[0].height = Cm(2)

data = [
	['姓名','成绩'],
	['李雷',98],
	['韩梅梅',99],
	['马冬梅',100]
]

for row in range(rows):
	for col in range(cols):
		table.cell(row,col).text = str(data[row][col])

prs.save('test5.pptx')

–> 输出结果为:
【python办公自动化(17)】利用python向PPT文档中写入内容(证书生成器)_第10张图片

综合应用

编写一个Python程序,要求

(1)从学生名单及获奖理由.xlsx文件中提取数据

(2)根据奖学金证书模板.pptx为每个人生成一个PPT

(3)保存为学生姓名-获奖理由.pptx

参考代码

步骤一、前期准备

import os
os.chdir("D:\\python_major\\auto_office17")
from pptx import Presentation
import datetime
from pptx.util import Cm

该部分代码完成的功能是:设置程序运行路径、导入相关的库

步骤二、取出Excel文件数据

def get_data():
	from openpyxl import load_workbook
	workbook = load_workbook("学生名单及获奖理由.xlsx")
	sheet = workbook.active
	data_size = sheet.dimensions
	size_ls = data_size.split(":")
	col_min,row_min,col_max,row_max = size_ls[0][0],size_ls[0][1],size_ls[1][0],size_ls[1][1:]
	ls_data = []
	for row in range(int(row_min),int(row_max)+1):
		ls_data.append([sheet[col_min+str(row)].value,sheet[col_max+str(row)].value])
	return(ls_data)

调用函数后输出的结果为:
【python办公自动化(17)】利用python向PPT文档中写入内容(证书生成器)_第11张图片

步骤三、加载模板

prs = Presentation("奖学金证书模板.pptx")
slide = prs.slides.add_slide(prs.slide_layouts[0])

winner_name = slide.placeholders[0]
certificate_type = slide.placeholders[1]
this_is_to_certify_that = slide.placeholders[20]
winning_reaon = slide.placeholders[19]
award_presenter = slide.placeholders[17]
award_date = slide.placeholders[21]

步骤四、输入对应信息,并保存文件

for data in get_data():
	winner_name.text = data[0]
	certificate_type.text = '奖状'
	this_is_to_certify_that.text = '兹证明'
	winning_reaon.text = data[1]
	award_presenter.text = 'Python实用技能微专业'
	award_date.text = str(datetime.datetime.now().date())
	prs.save('{}-{}.pptx'.format(data[0],data[1]))

全部代码及输出结果

import os
os.chdir("D:\\python_major\\auto_office17")
from pptx import Presentation
import datetime
from pptx.util import Cm

def get_data():
	from openpyxl import load_workbook
	workbook = load_workbook("学生名单及获奖理由.xlsx")
	sheet = workbook.active
	data_size = sheet.dimensions
	size_ls = data_size.split(":")
	col_min,row_min,col_max,row_max = size_ls[0][0],size_ls[0][1],size_ls[1][0],size_ls[1][1:]
	ls_data = []
	for row in range(int(row_min),int(row_max)+1):
		ls_data.append([sheet[col_min+str(row)].value,sheet[col_max+str(row)].value])
	return(ls_data)
#print(get_data())

prs = Presentation("奖学金证书模板.pptx")
slide = prs.slides.add_slide(prs.slide_layouts[0])

winner_name = slide.placeholders[0]
certificate_type = slide.placeholders[1]
this_is_to_certify_that = slide.placeholders[20]
winning_reaon = slide.placeholders[19]
award_presenter = slide.placeholders[17]
award_date = slide.placeholders[21]

for data in get_data():
	winner_name.text = data[0]
	certificate_type.text = '奖状'
	this_is_to_certify_that.text = '兹证明'
	winning_reaon.text = data[1]
	award_presenter.text = 'Python实用技能微专业'
	award_date.text = str(datetime.datetime.now().date())
	prs.save('{}-{}.pptx'.format(data[0],data[1]))

–> 输出结果为:
【python办公自动化(17)】利用python向PPT文档中写入内容(证书生成器)_第12张图片
【python办公自动化(17)】利用python向PPT文档中写入内容(证书生成器)_第13张图片

你可能感兴趣的:(python办公自动化,python,excel,html,poi,csv)