1.使用prs.slides.add_slide(blank_slide_layout),添加空白页;
2.使用slide.shapes.add_picture(img_path, left, top, width, height),添加picture;
3.使用tf = slide.shapes.add_textbox(left=left, top=top, width=width, height=height).text_frame,添加文本框;
4.使用f.text = picture_name/tf.paragraphs[0].text = img_path,向目标文本框中添加文本。
from pptx import Presentation
from pptx.util import Inches
def paste_picture_content(picture_name , img_path):
prs = Presentation(r'D:\Project_python\test004.pptx') #读取模板ppt
blank_slide_layout = prs.slide_layouts[6] #选择模板中的空白页[6]
slide = prs.slides.add_slide(blank_slide_layout) #新建空白页[6]
#确定图片在空白ppt中的位置,以及图片的大小;将图片贴到空白页中
left, top, width, height = Inches(2), Inches(1), Inches(5), Inches(5)
pic = slide.shapes.add_picture(img_path, left, top, width, height)
#确定文本框在空白ppt中的位置,以及文本框在的大小;将文本框在贴到空白页中
left, top, width, height = Inches(0.5), Inches(0.5), Inches(8), Inches(0.5)
tf = slide.shapes.add_textbox(left=left, top=top, width=width, height=height).text_frame
tf.text = picture_name
left, top, width, height = Inches(0.5), Inches(6.5), Inches(8), Inches(0.5)
tf = slide.shapes.add_textbox(left=left, top=top, width=width, height=height).text_frame
tf.paragraphs[0].text = img_path
prs.save(r'D:\Project_python\test004.pptx') # 另存目标ppt,存在name,则覆盖;不存在name则,新建;