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))
不同的格式化的方式,最后输出的结果是一致的
其中.slide_layouts[0]代表的是母版下的的第一张样板(红线框的那个),如下
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')
–> 输出结果为:
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]
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')
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')
p = tf.add_paragraph()
p.text = '带圆点的项目符号行2'
p.level = 1
p = tf.add_paragraph()
p.text = '带圆点的项目符号行3'
p.level = 2
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')
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')
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程序,要求
(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
该部分代码完成的功能是:设置程序运行路径、导入相关的库
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)
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]))