从 Django 填充 MS Word 模板
Filling MS Word Template from Django
我在这个 link 找到了一些 python 与 docxtpl 相关的文档:
https://docxtpl.readthedocs.io/en/latest/
我按照说明将在此站点找到的代码输入到视图中并创建了关联的 URL。当我转到 URL 时,我想生成一个文档 - 但我收到一个错误,没有返回任何 HTTP 响应。我知道我没有定义一个,但我对我需要定义什么 HTTP 响应有点困惑(我对此还是很陌生)。我保存的MS word模板标题为'template.docx'.
如有任何帮助,我们将不胜感激!
VIEWS.PY
def doc_test(request):
doc = DocxTemplate("template.docx")
context = { 'ultimate_consignee' : "World company" }
doc.render(context)
doc.save("generated_doc.docx")
我想访问此视图以生成文档,其中变量填充了上面上下文中定义的内容。
要点:读取文件内容和returnHTTP响应中的数据。
首先,您必须将文件保存在内存中,以便于阅读。您需要将其保存为 file-like object.
,而不是像 doc.save("generated_doc.docx")
这样的文件名
然后在 HTTP 响应中读取此 file-like object 和 return 的内容。
import io
from django.http import HttpResponse
def doc_test(request):
doc = DocxTemplate("template.docx")
# ... your other code ...
doc_io = io.BytesIO() # create a file-like object
doc.save(doc_io) # save data to file-like object
doc_io.seek(0) # go to the beginning of the file-like object
response = HttpResponse(doc_io.read())
# Content-Disposition header makes a file downloadable
response["Content-Disposition"] = "attachment; filename=generated_doc.docx"
# Set the appropriate Content-Type for docx file
response["Content-Type"] = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
return response
注意:此代码可能有效也可能无效,因为我尚未对其进行测试。但一般原则保持不变,即读取文件的内容并在 HTTP 响应中 return 适当 headers。
因此,如果此代码不起作用,可能是因为您使用的包不支持写入 file-like objects 或出于其他原因,那么它将是最好询问包的创建者或在他们的 Github 上提交有关如何读取文件内容的问题。
这里有一个更简洁的解决方案:
import os
from io import BytesIO
from django.http import FileResponse
from docxtpl import DocxTemplate
def downloadWord(request, pk):
context = {'first_name' : 'xxx', 'sur_name': 'yyy'}
byte_io = BytesIO()
tpl = DocxTemplate(os.path.join(BASE_PATH, 'template.docx'))
tpl.render(context)
tpl.save(byte_io)
byte_io.seek(0)
return FileResponse(byte_io, as_attachment=True, filename=f'generated_{pk}.docx')
我在这个 link 找到了一些 python 与 docxtpl 相关的文档:
https://docxtpl.readthedocs.io/en/latest/
我按照说明将在此站点找到的代码输入到视图中并创建了关联的 URL。当我转到 URL 时,我想生成一个文档 - 但我收到一个错误,没有返回任何 HTTP 响应。我知道我没有定义一个,但我对我需要定义什么 HTTP 响应有点困惑(我对此还是很陌生)。我保存的MS word模板标题为'template.docx'.
如有任何帮助,我们将不胜感激!
VIEWS.PY
def doc_test(request):
doc = DocxTemplate("template.docx")
context = { 'ultimate_consignee' : "World company" }
doc.render(context)
doc.save("generated_doc.docx")
我想访问此视图以生成文档,其中变量填充了上面上下文中定义的内容。
要点:读取文件内容和returnHTTP响应中的数据。
首先,您必须将文件保存在内存中,以便于阅读。您需要将其保存为 file-like object.
,而不是像doc.save("generated_doc.docx")
这样的文件名
然后在 HTTP 响应中读取此 file-like object 和 return 的内容。
import io
from django.http import HttpResponse
def doc_test(request):
doc = DocxTemplate("template.docx")
# ... your other code ...
doc_io = io.BytesIO() # create a file-like object
doc.save(doc_io) # save data to file-like object
doc_io.seek(0) # go to the beginning of the file-like object
response = HttpResponse(doc_io.read())
# Content-Disposition header makes a file downloadable
response["Content-Disposition"] = "attachment; filename=generated_doc.docx"
# Set the appropriate Content-Type for docx file
response["Content-Type"] = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
return response
注意:此代码可能有效也可能无效,因为我尚未对其进行测试。但一般原则保持不变,即读取文件的内容并在 HTTP 响应中 return 适当 headers。
因此,如果此代码不起作用,可能是因为您使用的包不支持写入 file-like objects 或出于其他原因,那么它将是最好询问包的创建者或在他们的 Github 上提交有关如何读取文件内容的问题。
这里有一个更简洁的解决方案:
import os
from io import BytesIO
from django.http import FileResponse
from docxtpl import DocxTemplate
def downloadWord(request, pk):
context = {'first_name' : 'xxx', 'sur_name': 'yyy'}
byte_io = BytesIO()
tpl = DocxTemplate(os.path.join(BASE_PATH, 'template.docx'))
tpl.render(context)
tpl.save(byte_io)
byte_io.seek(0)
return FileResponse(byte_io, as_attachment=True, filename=f'generated_{pk}.docx')