Hello,
I currently struggling with the use of the streamlit.download_button .
- I’m uploading a xlsx file to the streamlit app via file_uploader
- Read the excel file with pd.read_excel to data_source object.
- Write the dataframe object with st.write. The dataframe displays correctly
- Download the file with st.download_button with the following code :
st.download_button(‘Download file’,data=pd.DataFrame.to_csv(data_source,index=False), mime=“text/csv”)
Results = I get a csv file but with encoding problems. When I open the file I get the following :

Allée van
When I don’t specify a mime argument. it download as a .txt file and doesn’t give those encoding errors

Allée van
I tried to export as a xlsx file also (which was the initual target) but it’s giving me a error message when I open the file so I swithced to csv.
Could you help ?
Simon
2 Likes
Hi Simon
I was struggled with the same problem before, you can have a shot with following code which works fine with me.
import pandas as pd
from io import BytesIO
from pyxlsb import open_workbook as open_xlsb
import streamlit as st
def to_excel(df):
output = BytesIO()
writer = pd.ExcelWriter(output, engine='xlsxwriter')
df.to_excel(writer, index=False, sheet_name='Sheet1')
workbook = writer.book
worksheet = writer.sheets['Sheet1']
format1 = workbook.add_format({'num_format': '0.00'})
worksheet.set_column('A:A', None, format1)
writer.save()
processed_data = output.getvalue()
return processed_data
df_xlsx = to_excel(df)
st.download_button(label='📥 Download Current Result',
data=df_xlsx ,
file_name= 'df_test.xlsx')
14 Likes
I use the following code if you have the Excel file at the beginning, prompt for the user to download.
with open(file_name, "rb") as template_file:
template_byte = template_file.read()
st.download_button(label="Click to Download Template File",
data=template_byte,
file_name="template.xlsx",
mime='application/octet-stream')
system
Closed
6
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.