DEV Community

Discussion on: Create a document using python-docx and send as attachment through django

Collapse
 
rhymes profile image
rhymes • Edited

I'm not familiar with the library BUT:

BytesIO is basically a buffer of bytes in memory, somewhere your library wants bytes, not the buffer itself, hence the:

TypeError: expected bytes-like object, not BytesIO

In the file_list you're doing this:

file_list.append([filename, f, mime_type])

where f is the BytesIO. What you need is the value contained in it:

file_list.append([filename, f.getvalue(), mime_type])

you can see the documentation for that method here: docs.python.org/3/library/io.html#...

BTW you should probably tag this post as #help because... you're asking for help :D

Collapse
 
bharathbk profile image
BharathKumar (BK) Inbasekaran

Thank you, your suggestion worked for me. I added the tag too :)