Creating Presentations with Python

If you are a data scientist or an automation engineer or just a data analyst. How much time do you spend preparing PowerPoint slides every time you have to present your data. Quite a lot isn’t it.

Ok, let's say you have a template format that you just need to update with data, charts, plots, etc. But still, how much time do you spend on updating them. At least an hour. So here comes the use of python-pptx Python library to automate the creation of ppts in .pptx format.

I am not a data scientist but a software engineer. Let me be clear here as I am going to provide a few tricks which are more like using programing statements and conditions to create the desired slide.

PowerPoint automation

Where I have used:

1. I had an automation project which was based on manipulating data and generating filtered data as output in excel/CSV files. And later it was used by a team to create ppt from it. And for this, I was using Python. With pptx I just automate the PowerPoint creation. So every time the scheduler ran the python script to manipulate the raw data and do the calculation it starts creating the presentation itself. So the other team just needs to download the ppt and send it across.

2. I had another Machine learning project where I was doing the part of data analysis or data engineering part for filtering a few data matrices for the ML engineer. And same as above they used to create the presentations by hand spending their valuable time, which was later automated by me to create the presentation with the ML script execution.

Let’s Get started:

It is simple as any other python lib to install and use. If you are using any IDE that will be better as it will suggest the building function and variables.


pip install python-pptx

I suggest you use the pip install code line because this package depends on Ixml, Pillow and XlsxWriter packages. If you use the installation method you will need to install these dependencies yourself.

What you can do:

Slide format by a number

When you need to choose a format for a slide you can choose it by the number of layouts from the ms-pptx.

title_slide_layout = prs.slide_layouts[5]slide = prs.slides.add_slide(title_slide_layout),filename.pptx)

Insert image

img_path = os.path.join(imagepath,image_name+’.png’)pic = slide.shapes.add_picture(img_path, Inches(10.0), Inches(0.5),width=Inches(3), height=Inches(2)) #parameters are left, top, width and height.

You can move the image from left to right by increasing 1st argument in inches. Same for moving top to bottom.

Table’s creation

For creating a table in the slide, 1st need to define the position and size of the table and rows and columns:

left = Inches(0.7)right = Inches(0.1)top = Inches(2.4)width = Inches(4.0)columns= 4Rows=5Table = shapes.add_table(1, columns, left, top, width, right).tableThen we need to define what will be the width of each column.num = 12.0/columns # here 12 is the width of the silde in inches. It depends on which slide format you have choosed.Create the table by using for loop, which makes it easier to add values to each cell.for e in range(0,c):table2.columns[e].width = Inches(num)for i in range(0,1):for e in range(0,c):cell = table2.rows[i].cells[e]table.cell(i,e).text = ‘some text or value’ + ‘\n’+’ ‘fill = cell.fill #fill the legend as wellfill.solid()fill.fore_color.rgb = RGBColor(250,0,0)#redparagraph = cell.text_frame.paragraphs[0]paragraph.alignment = PP_ALIG.CENTER

Note: the table will take the color of your to default slide format, if not chosen any organizational standard.

Adding chart

NOTE: you can easily modify the chart style text or format manually also, after creation of chart.

Organisational standard slide:

Sometimes we need to maintain the organizational standard for the presentation that we created. Organizational standard means the font of the intro slides or the cover picture should according to the organizational standard.

For that, you can take the 1st Intro/ title slide, which says the presentation name and the required picture.

Lets consider this as the Google organisation standard template for the presentation.

Then make sure the text font is chosen correctly if it is required. Then make sure the theme for the presentation is according to your office standard. (go to Design -> Variants -> Colours, font, background, etc are as per your organizational standard.

prs = Presentation(os.path.join(dirpath,’Intro.pptx’)) # dirpath is the path to your directory where the intro silde.#add other sildestitle_slide_layout = prs.slide_layouts[5]slide = prs.slides.add_slide(title_slide_layout),filename.pptx)

Color scale

For applying color scale on a table you can choose each cell and apply some RBG color on it but if the result you are expecting is somewhat like mine,

Then you can use the below definitions. Also, the color can be modified by the RBG color code.

For choosing a color for a cell in color scale it required 4 input types.

1. From where value range starts e.g start_value = 0

2. From where Value range end e.g end_value = 80

3. From where colur range start e.g start_color = ‘edf7ed’

4. From where color range ends e.g end_color = ‘9dfa9d’

And this Definition return an RGB colour based on the input.

def colorlist(value,start_value,start_color,end_value,end_color):
output = []
index = 0
if end_value==100 and start_value==0:
index = int((value/10)*100)
value = int(np.round(value,0))
index = value-start_value
r1, g1, b1 = RGBColor.from_string(start_color)
r2, g2, b2 = RGBColor.from_string(end_color)
rdelta, gdelta, bdelta = (r2-r1)/(end_value-start_value), (g2-g1)/(end_value-start_value), (b2-b1)/(end_value-start_value)
for step in range(0,end_value - start_value):
r1 += rdelta
g1 += gdelta
b1 += bdelta
output.append((int(r1), int(g1), int(b1)))
color = output[index]
return color

Problems I faced

As there are many default functions in pptx you can use as per your requirement but still, there are some limitations with the latest version (I am using currently 0.6.19 with python 3.7).

1. Inserting a picture in table cells.

While creating a presentation you might need to insert images like charts and status of values into table cells, something like below. This is not actually possible with the python-pptx lib. ( as of my knowledge) and I dint find any alternative to this also.

Solution to them

So as per the solution, you can break the table into small tables, like below table has been broken into 1. Column names table, 2. 1st two rows as one table 3. The images in each row are inserted as images only with exact positioning.

Hope it will help you to create an automated presentation for your automation process or data presentation or ML graph presentations. There are definitely many more tips you can get on other blogs if you google. Thank you.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store