Python API to Generate PowerPoint PPTX Presentation
Open Source Python Library to Create, Read, Modify & Export Microsoft PowerPoint PPTX files. Add Images, Charts or Shapes with ease.
What is Python-PPTX?
Python-PPTX is an Open Source Python library that enables software programmers to easily generate and manipulate Open XML PowerPoint (PPTX) files inside their own Python apps without any external dependencies. It helps developers to automate the production of PPTX presentations by customizing PowerPoint presentation creation from database content or making bulk updates to a library of presentations or automating the production of slides.
The library provides support for several important features such as generating & edit a presentation, inserting new slides, Adding text-box to a slide, manipulating text font size, table addition to the slide, auto shapes support, adding & manipulate column, bar, line, or pie charts and many other features.
Getting Started with Python-PPTX
Python-PPTX is hosted on PyPI, so installing with pip is simple. Please use the following command.
pip command
pip install python-pptx
lxml package and Pillow both are required for Python-PPTX to run smoothly. The charting features depend on XlsxWriter. Both pip and easy_install will take care of satisfying these dependencies for you. If you want to install it using setup.py then you will require installing the dependencies yourself.
Generate & Modify PowerPoint PPTX Presentation using Python API
Python-PPTX API provides functionality for new PowerPoint PPTX presentation creation as well as modification inside Python applications. Software developers can easily modify an existing presentation by opening the existing presentation. It allows adding slides, specifying a layout for new slides, adding titles and contents, inserting images and shapes, etc. Once done you can change the name of the existing presentation as well as can save it to the location of your choice.
How to Create Simple Presentation via Python API?
from pptx import Presentation
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]
title.text = "Hello, World!"
subtitle.text = "python-pptx was here!"
prs.save('test.pptx')
Working with PPTX Slide and Images
Python-PPTX is a great library for using Python to create dynamic PowerPoint slides. Software programmers can add a slide to an existing presentation with ease. Every slide in a presentation is based on a slide layout. Slide layout is like a template for a slide, it allows users to inherit formatting choices, text boxes, titles or graphics, etc.
How to Add Picture to a Presentation via Python API?
from pptx import Presentation
from pptx.util import Inches
img_path = 'monty-truth.png'
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = Inches(1)
pic = slide.shapes.add_picture(img_path, left, top)
left = Inches(5)
height = Inches(5.5)
pic = slide.shapes.add_picture(img_path, left, top, height=height)
prs.save('test.pptx')
Adding & Modifying Charts to A Presentation
Python-PPTX API allows software developers to add charts to a slide and also modify existing ones inside their own applications. The API supports the most common chart types other than 3D types. You can add a single-series column chart, multi-series chart, bubble chart, Line chart, pie charts & more. By default, the colors assigned to each series in a chart are the theme colors Accent 1 through Accent 6, in that order. You can also assign specific colors to data points in pie, bar, and line segments for at least some chart types. The easiest and suggested way will be to change the theme colors in your starting "template" presentation.