I have a python code that calculates a file with excel format, and I usually save it with openpyxl using the command : workbook.save(filename) when I work in local.
Now, I want to do the same thing using streamlit. The file “workbook” is calculated alright, but I do not succeed in retrieving it using a download button, here is what I am doing so far :
st.download_button(“Récupérer le fichier”,
data=workbook,
mime=‘xlsx’,
file_name=“coucou.xlsx”)
but the format is invalid. I have tried to convert in Dataframe using Pandas and convert in binary but no success so far…
Or alternatively, would it be possible to save the file using openpyxl as I do in local, and then retrieve it from the streamlit buffer ?
Hello there, I have finally found a workaround ! So here I share it with the community
from openpyxl import Workbook
from io import BytesIO
workbook = Workbook()
with NamedTemporaryFile() as tmp:
workbook.save(tmp.name)
data = BytesIO(tmp.read())
st.download_button("Retrieve file",
data=data,
mime='xlsx',
file_name="name_of_file.xlsx")
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.