Closed rodolfolotte closed 6 months ago
The doc.save() actually creates the "zip" document.
In [10]: d=Document() In [11]: d Out[11]: <odfdo.document.Document at 0x1119878b0> In [12]: with BytesIO() as output: ...: d.save(output) ...: In [13]: output Out[13]: <_io.BytesIO at 0x111a65e50>
regards,
Fix: I think I need to not close the bytesIO too early, so: d=Document() b=BytesIO() d.save(b)
use the bytesio in some way: with open("test.zip", "wb") as f: f.write(b.getvalue())
only close now b.close()
Fast reply! Thank you so much!
I will try right now and will post the solution here!
I have a django project which will build an odt file and delivery it as a response ('application/vnd.oasis.opendocument.text'') to download. I will try your suggestion in a response.write
regards,
@jdum that works fine! Thank you!
filename = "filename.odt"
response = HttpResponse(content_type='application/vnd.oasis.opendocument.text')
response['Content-Disposition'] = 'attachment; filename=' + filename
b = BytesIO()
doc.save(b)
response.write(b.getvalue())
b.close()
Added 2 recipes for save/read from io.BytesIO
Is there any feature that would avoid the use of
doc.save(target=PATH, pretty=True)
in order to write the produced Document() in a response object?