Django中可下载的docx文件 [英] Downloadable docx file in Django
问题描述
我的django网络应用制作并保存了docx,我需要将其下载。
我使用简单的 render_to_response
如下。
My django web app makes and save docx and I need to make it downloadable.
I use simple render_to_response
as below.
return render_to_response("test.docx", mimetype='application/vnd.ms-word')
但是,它会引发错误,例如'utf8'编解码器无法解码位置15的字节0xeb:无效的连续字节
However, it raises error like 'utf8' codec can't decode byte 0xeb in position 15: invalid continuation byte
I无法将此文件作为静态文件提供,因此我需要找到一种以此方式提供文件的方法。
非常感谢您的帮助。
I couldn't serve this file as static so I need to find a way to serve it as this. Really appreciate for any help.
推荐答案
是的,按wardk的说法,更干净的选择是使用< a href = https://python-docx.readthedocs.org/ rel = noreferrer> https://python-docx.readthedocs.org/ :
Yep, a cleaner options, as stated by wardk would be, using https://python-docx.readthedocs.org/:
from docx import Document
from django.http import HttpResponse
def download_docx(request):
document = Document()
document.add_heading('Document Title', 0)
response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
response['Content-Disposition'] = 'attachment; filename=download.docx'
document.save(response)
return response
这篇关于Django中可下载的docx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!