Tips for (automatically) generating PowerPoint reports with python
In python-pptx
, it is convenient to handle in inches.
So, use A4 size = = 11.69 inch x 8.27 inch.
(python-pptx uses a unit called English Metric Units (EMU) for length. The method to convert this to inches is pptx.util.Inches.)
from pptx import Presentation
from pptx.util import Inches
prs = Presentation()
prs.slide_height=Inches(11.69)
prs.slide_width=Inches(8.27)
prs.save("./hoge.pptx")
With this, I can make it for the time being.
If you want to paste the title slide layout and a blank slide layout here,
from pptx import Presentation
from pptx.util import Inches
prs = Presentation()
prs.slide_height=Inches(11.69)
prs.slide_width=Inches(8.27)
#Title slide
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "Hello, World!"
subtitle.text = "python-pptx was here!"
#Blank slide
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
prs.save("./hoge.pptx")
Will be.
python-pptx 0.6.18 documentation
Recommended Posts