python-pptx的简单使用

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则,新建;

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值