擅长:python、mysql、java
<p>是的,一个更干净的选项,正如wardk所说,使用<a href="https://python-docx.readthedocs.org/" rel="noreferrer">https://python-docx.readthedocs.org/</a>:</p>
<pre><code>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
</code></pre>