py python-pptx 创建ppt

创建一个简单的PPTX文件

from pptx import Presentation

class Main():
  def __init__(self):
    prs = Presentation()
    title_slide_layout = prs.slide_layouts[0]

    slide = prs.slides.add_slide(title_slide_layout)
    title = slide.shapes.title
    subtitle = slide.placeholders[1]

    slide1 = prs.slides.add_slide(title_slide_layout)
    title1 = slide1.shapes.title
    subtitle1 = slide1.placeholders[1]

    title.text = "Hello, World!"
    subtitle.text = "python-pptx was here!"

    title1.text = "Hello, World! 1"
    subtitle1.text = "python-pptx was here! 1"

    prs.save('./test.pptx')

Main()

使用PPT模板

from pptx import Presentation
from pptx.dml.color import RGBColor

class Main():
  def __init__(self):
    prs = Presentation('./temp.pptx')

    prs.slides[0].shapes.title.text = 'new title'
    prs.slides[0].placeholders[1].text = 'new subtitle'

    prs.slides[1].shapes.title.text = 'new title 1'
    prs.slides[1].shapes.title.text_frame.paragraphs[0].font.color.rgb = RGBColor(255,255, 255) # 设置文本颜色
    prs.slides[1].placeholders[1].text = 'new subtitle 1'
    prs.slides[1].placeholders[1].text_frame.paragraphs[0].font.color.rgb = RGBColor(255,255, 255)
   

    prs.save('./test.pptx')

Main()

See also:

posted @ 2020-11-20 18:08  Ajanuw  阅读(204)  评论(0编辑  收藏  举报