Skip to main content

Render ppt like a jupyter notebook

Project description

PPTX Renderer

This package let's you run your powerpoint presentations like a jupyter-notebook. You can insert placeholders in the ppt and also write python code in the ppt's notes and use either a python function or an equivalent commandline tool to convert it into an output rendered presentation.

Documentation

Installation

pip install pptx-renderer

Usage

Below is a simple example.

from pptx_renderer import PPTXRenderer
p = PPTXRenderer("template.pptx")

someval = "hello"
def mymethod(abc):
    return f"{abc} " * 5

myimage = r"is_it_worth_the_time.png"
mytable = [["a", "b", "c", "d", "e"]] * 10
p.render(
    "output.pptx", 
    {
        "someval": someval, "mymethod": mymethod, "myimage": myimage,
        "mytable": mytable,
    }
)

This will convert this

Before

to this.

After

You can define some functions within the ppt itself by writing python code in the notes section of slides. And the variables and functions in this code can be used in the main ppt.

For example: write the following in one of the slide's notes.

```python
def myfunc(input):
    return input * 42
```

Now you can, for example, add the placeholder {{{myfunc(42)}}} in your slides.

If the template ppt is a self contained python script ie: if it does not require variable values and function definition to be passed from outside, you can generate the output ppt directly from the commandline using the following command.

pptx-renderer input_template.pptx output_file.pptx

Placeholders

You can have placeholders for text, image or a table. Placeholders can be added inside text boxes and shapes. All placeholders should be enclosed within a pair of triple braces ({{{ and }}}).

Text

Any placeholder which can be evaluated into a string can act as a text placeholder.

For example: {{{"hello " * 10/2}}} or {{{abs(-2)}}}

Image

if you have added :image() as a suffix to the python statement, the renderer will try to convert the value of python statement to a file location and insert an image from that file location.

For example: {{{"c:\temp\myimage.png":image()}}}

Table

Tables are similar to images, but only that instead of a string, the python statement should evaluate to a list of lists. Then you can add :table() as a suffix and it will be convert to a table inside the ppt.

For example: {{{[["col1", "col2", "col3"],[1, 2, 3]]:table()}}} will render to

col1 col2 col3
1 2 3

Code in slide notes

You can write regular python code in the slide notes but enclosed between ```python and ```.

For example: Create a new pptx and write the following in the first slide's notes

```python
import numpy as np
myarr = np.array([[1, 2], [3, 4]])
```

And in the slide, create a rectangluar shape and add the text {{{myarr:table()}}} and a text box with the text The determinant of the array is {{{np.linalg.det(myarr)}}}

Repeating slides

If you define loop_groups keyword argument as part of render method, you can repeat groups of slides. The value of loop_groups should be a list of dictionaries. Each dictionary should have the following keys

  • start: The slide number where the loop should start
  • end: The slide number where the loop should end
  • iterable: The iterable which should be looped over.
  • variable: The name of the variable which will be available inside the loop.

For example, if you want to insert all images from a folder into a ppt, one image per slide, you can do the following.

Create a template ppt with a single slide which contains a rectangle shape where the image should be inserted. Then insert a placeholder text in the format {{{path_to_image:image()}}} inside the shape. Then you can use the following

from pathlib import Path
from pptx_renderer import PPTXRenderer
p = PPTXRenderer("template.pptx")

images = Path("path/to/images").glob("*.png")
loop_groups = [
    {
        "start": 0,
        "end": 0,
        "iterable": images,
        "variable": "path_to_image"
    }
]

p.render(
    "output.pptx",
    loop_groups = loop_groups
)

This will create a new ppt with each image from the folder inserted into a new slide.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pptx_renderer-0.5.2.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pptx_renderer-0.5.2-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file pptx_renderer-0.5.2.tar.gz.

File metadata

  • Download URL: pptx_renderer-0.5.2.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for pptx_renderer-0.5.2.tar.gz
Algorithm Hash digest
SHA256 313cab87ff62c45c264efcff562b5fc90f6a0066d669b0af1d2836df7dec359a
MD5 48ddc42046aa26d67ead868398fb3f93
BLAKE2b-256 cc948b7ebbc47a214a59769d1a0495e3ad96b280f80ebc4cf65e36f83397721d

See more details on using hashes here.

File details

Details for the file pptx_renderer-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: pptx_renderer-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for pptx_renderer-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cd8cdbc6b6f4dfeea891d0d46ea5530749ad557d1f12821ba58babdedba22ceb
MD5 71ad4bf55a1623da519f9f4eb6c4f00d
BLAKE2b-256 9da2c9c9d84a299eac80a650f585ccbf3cad5ff4d987d4e4b4e09848283069cb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page