When saving to datatables, Anvil can only save what it can serialize.
A docx object can’t be serialized so can’t be saved.
We need to convert it to a media object which anvil knows how to handle.
import io
content = io.BytesIO()
doc.save(content)
content_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
media_object = anvil.BlobMedia(content_type=content_type, content=content.getvalue(), name="hello.docx")
# now save the media object to the datatable
# you can also return the media object and download it on the client
(I got the content type from searching for mimetypes in google and looking up docx)
(It also works with None as the content type and this will be inferred when you download the doc)
Another way to do this would be via a temporary file on disk:
with anvil.media.TempFile() as file_name:
doc.save(file_name)
media_object = anvil.media.from_file(file_name, name="hello.docx")