Skip to main content

A library using Plotly and Powerpoint to easily generate slides with plotly charts in them

Project description

Plotly & Powerpoint

A library used to create powerpoint slides including plotly charts. Use this to automate powerpoint creation including certiain charts/visualizations.

Getting Started

Below is a quick tutorial taking you through how to setup and use this library. I will use public data to showcase how it works.

Step 1 - Install Dependencies

If you are using juptyer notebooks, you may need to do some configuration in order to view charts propertly. We are using Plotly for all graphing, so please refer to the getting started page to learn how to install everything properly.

Step 2 - Install Package

pip install plotlyPowerpoint

Step 3 - Prepare Your Powerpoint

The main function of this library is meant to generate slides which include a plotly chart. In order to do this, you must prepare a powerpoint template with the proper layout. You can design any layout you desire, but make sure that each element you desire to fill with python is created as a placeholder. You can use a powerpoint template I've included in my source code to make things easy (assets/powerpoint_templates/template.pptx).

In order to do this, open up your desired powerpoint and go into the slide master. Insert a new slide or use a current one already created. I recommend to drag this to be first in the order, this will make your life easier later down the road. Now insert the proper elements and arragne them as you please. For the chart, insert an image placeholder and make the apsect ratio similar to a laptop screen. Most images created are in landscape view but not too wide.

Here is an example of what my template looks like.

Step 4 - Load Library and Prepare Data

If you want to follow along with my tutorial, feel free and copy/paste my code. However, ensure you have pydataset installed before you do.

from pptx import Presentation
import pandas as pd
from pydataset import data
import plotlyPowerpoint as pp

############
## Prepare Data
############

#load datasets
df = data('InsectSprays')
df2 = data("JohnsonJohnson")

#Data transformation
df['m2'] = df['count'] * 1.1
df2['year'] = df2['time'].astype(int)
df2 = df2.groupby(['year']).agg({'JohnsonJohnson': 'mean'}).reset_index()

Step 5 - Find Your Powerpoint Elements

In order for this library to know where to place each element, you must give it each element index. See the following code to learn how to do this:

#load presentation
prs = Presentation("template.pptx")

#set the slide we are after - the first slide in the master layout is at index 0
slide = prs.slides.add_slide(prs.slide_layouts[0])

#print index and name
for shape in slide.placeholders:
    print('%d %s' % (shape.placeholder_format.idx, shape.name))

You will need this information later on.

Step 6 - Set Template

Now that we have our powerpoint template, we need to tell the library where to find this file. Do this with the following:

pp.setTemplate("path_to_template")

Step 7 - Define Charts

Here is where the work is done. Creating slides with charts is done by defining an array of dictionary objects. Each object represents one slide. Everything must be represented as a string except your dataframe object. Additionally, this library automatically groups every dataframe down to the level needed for the specific chart. Therefore, you don't have to create a new dataframe for each chart. As long as your df has the right data in it, this function will do the rest.

As a start, I will define two basic charts:

#define slides
slides = [
    {
        "title": "Executive Summary",
        "description": "This is a test",
        "item-index": {
            "slide": 1,
            "title": 0,
            "description": 11
        },
        "charts": [
            {
                "name": "Sessions by Channel over Time",
                "data": df,
                "type": "line",
                "metrics": [
                    {"name": "count", "prettyName": "Testing", "method": "sum"}
                ],
                "axis": "spray",
                "x-axis-title": "Test",
                "item-index": 10
            },
            {
                "name": "Sessions by Channel",
                "data": df,
                "type": "bar",
                "metrics": [
                    {"name": "count", "prettyName": "Testing", "method": "sum"}
                ],
                "axis": "spray",
                "x-axis-title": "Test",
                "item-index": 11
            }
        ]
    }
]

I will go into more detail on each one of these variables in a later section.

Step 8 - Run Function

#run function
pp.createSlides(slides)

This will output a file called output.pptx. I suggest you do not make this your final file, as each time you run this function you will overwrite the powerpoint. Create a copy of this file and start to create your analysis/report there. From here, you can use output.pptx as a slide library, where you can include or delete any chart you create.

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

plotlyPowerpoint-1.3.31.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

plotlyPowerpoint-1.3.31-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file plotlyPowerpoint-1.3.31.tar.gz.

File metadata

  • Download URL: plotlyPowerpoint-1.3.31.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.7

File hashes

Hashes for plotlyPowerpoint-1.3.31.tar.gz
Algorithm Hash digest
SHA256 444c8555237afb669ae07281e23d57a4eb9d1d6125452126fb44e5687fc4f43e
MD5 e5130f9c08a1cd0d4e803e0db3ffba87
BLAKE2b-256 5723fa8b238fd500a2f6f2e4c200f5a61ef89b0ebd40a79d16c6ac02e3b96b93

See more details on using hashes here.

File details

Details for the file plotlyPowerpoint-1.3.31-py3-none-any.whl.

File metadata

File hashes

Hashes for plotlyPowerpoint-1.3.31-py3-none-any.whl
Algorithm Hash digest
SHA256 febd3bad935bd64e5506f5c068b3204899e9e286ee9aa9a0a28b478cc56e1cae
MD5 7bafad9e2c538d06a25551055e49b268
BLAKE2b-256 49a890c0cbe1eeeb422ad830d439c0a36091a224447f1af9ae933fe7a3d5d495

See more details on using hashes here.

Supported by

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