I have an app that allows to edit an uploaded Word document. The edits would need to be saved as a separate document and then saved. To accomplish this I use the st.download_button. However, when I run the code I get this error: RuntimeError: Invalid binary data format: <class ‘NoneType’>. And this one as well: RuntimeError(“Invalid binary data format: %s” % type(data)).
Why do these errors occur? Any form of help would be appreciated.
I am using Python 3.10.4, Streamlit 1.11.1, Virtualenv, PyCharm Community Edition 2022.1.4, Windows 11, Firefox 103.0.1 (64-bit).
Python-docx’s method accepts a stream instead of a filename. Thus, you can initialize an io.BytesIO() object to save the document into, then dump that to the user.
I have tried this and it worked for me
import io
doc_download = doc_file_creation(doc_file)
bio = io.BytesIO()
doc_download.save(bio)
if doc_download:
st.download_button(
label="Click here to download",
data=bio.getvalue(),
file_name="Report.docx",
mime="docx"
)
doc_file_creation is just a function name which I used for returning an already created word document. You can use docx or docxtpl modules for creating a docx file.
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.