download python-pptx from streamlit

Question:

I’m using python-pptx and streamlit. I’m creating a ppt from python like this:

from pptx import Presentation
import streamlit as st

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') # This works, just saves it in the app path.
# But I want a clickable button, to be downloaded from the streamlit app.
# Therefore, I've tried to do this: 

st.download_button(label = 'Download ppw', data = prs, file_name = 'my_power.ppt')

But I’m getting the following error:

RuntimeError: Invalid binary data format: <class 'NoneType'>

Requeriments to reproduce:

Install streamlit (pip install streamlit)

Install python-pptx (pip install python-pptx)

Asked By: Tonino Fernandez

||

Answers:

I took inspiration from this answer and I understood that you have to generate a binary representation for your Presentation object.

from pptx import Presentation
import streamlit as st
from io import BytesIO

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!"

# save the output into binary form
binary_output = BytesIO()
prs.save(binary_output) 

st.download_button(label = 'Download ppw',
                   data = binary_output.getvalue(),
                   file_name = 'my_power.pptx')
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.