擅长:python、mysql、java
<p>由于<a href="https://python-docx.readthedocs.org" rel="noreferrer">python-docx</a>,我成功地从django视图生成了docx文档。</p>
<p>这是一个例子。我希望能帮上忙</p>
<pre><code>from django.http import HttpResponse
from docx import Document
from cStringIO import StringIO
def your_view(request):
document = Document()
document.add_heading(u"My title", 0)
# add more things to your document with python-docx
f = StringIO()
document.save(f)
length = f.tell()
f.seek(0)
response = HttpResponse(
f.getvalue(),
content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
)
response['Content-Disposition'] = 'attachment; filename=example.docx'
response['Content-Length'] = length
return response
</code></pre>