Hi,
document.save() receives a file path or stream, right? What am I doing wrong?
This a flask application....
`@app.route('/doc')
def index():
document = Document()
document.add_heading('Document Title', 0)
document.add_paragraph('Some text')
buffer = io.StringIO()
document.save(buffer)
buffer.seek(0)
return Response(buffer, mimetype='application/vnd.openxmlformats-officedocument.wordprocessingml.document')`
ERROR:
Pretty sure you want io.BytesIO rather that io.StringIO because the saved file is binary, not unicode. Also, I'm not sure what Flask is looking for as the first parameter of the Response() call, but I expect it's bytes rather than a file-like object. You can get bytes from an io.BytesIO object with buffer.getvalue(), and there's no need to .seek(0) before that call.
@scanny thanks a lot!
I appreciate your help and many thanks for the incredible tool that you are building...
You gave me the solution. My code now works fine...
`
def index():
document = Document()
document.add_paragraph('A plain paragraph having some ')
document.add_heading('Heading, level 1')
document.add_paragraph('Intense quote')
buffer = io.BytesIO()
document.save(buffer)
buffer.seek(0)
return send_file(buffer, as_attachment=True, attachment_filename='report.doc')`
Pretty sure you want
io.BytesIOrather thatio.StringIObecause the saved file is binary, not unicode. Also, I'm not sure what Flask is looking for as the first parameter of theResponse()call, but I expect it'sbytesrather than a file-like object. You can get bytes from anio.BytesIOobject withbuffer.getvalue(), and there's no need to.seek(0)before that call.
Thank you so much! help me a lot
Most helpful comment
Pretty sure you want
io.BytesIOrather thatio.StringIObecause the saved file is binary, not unicode. Also, I'm not sure what Flask is looking for as the first parameter of theResponse()call, but I expect it'sbytesrather than a file-like object. You can get bytes from anio.BytesIOobject withbuffer.getvalue(), and there's no need to.seek(0)before that call.