¿如何使用 python-docx-template (docxtpl) 生成 .docx 文档?姜戈 Python
¿How to generate a .docx document with python-docx-template (docxtpl)? Django Python
我正在尝试为此在 Django 中生成一个 .docx 文档我正在使用通过 .docx
模板生成文档的 docxtpl(Python docx 模板)。我尝试使用以下数据生成文档:
listaFinal = [
[
{
"-":"LIBROS"
},
{
"":"Responsabilidad Social de las Organizaciones (RSO): Avances y propuestas en América Latina",
"Editorial":"Imprenta Editora Gráfica Real",
"Año":2015,
"Páginas":457,
"Tipo Libro":53
}
],
[
{
"-":"ARTICULOS"
},
{
"":"Gestión universitaria ética y responsable. Indicadores de RSU ",
"Link Articulo":"http://www.revistalatinacs.org/13SLCS/2013_actas/170_Valarezo.pdf",
"issn":"",
"nombre_conferencia":"V Congreso Internacional Latina de Comunicación Social – V CILCS – Universidad de La Laguna, diciembre 2013 "
},
{
"":"Universidad Tecnica Particular de Loja. Proceso de internacionalización de la UTPL",
"Link Articulo":"http://www.iesalc.unesco.org.ve/index.php?option=com_content&view=article&id=1786&Itemid=1147&lang=es",
"issn":"",
"nombre_conferencia":""
}
]
]
在我的 .views
中,我在其中指明了我的模板路径 docx_filename.docx
并将数据发送到 context
:
def generaraDocumento(request):
response = HttpResponse(content_type='application/msword')
response['Content-Disposition'] = 'attachment; filename="cv.docx"'
doc = DocxTemplate(str(settings.BASE_DIR) + '/cv_api/templates/docx_filename.docx')
context = {'listaFinal': listaFinal}
doc.render(context)
doc.save(response)
return response
我有一个名为 docx_filename.docx
的模板,我所做的是在这个模板中指出我想要的结果,在我的 .docx
模板中我有这个能够显示文件中的数据:
{% for lista in listaFinal %}
{% for i in lista %}
{% for clave, valor in i.items %}
{% if forloop.first %}
{% if clave == '-' %}
{{ clave }}
{% endif %}
{% else %}
{% if clave %}
{{ clave }}
{% endif %}
{{ valor }}
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
它给我这样的错误:异常值:'builtin_function_or_method' 对象不可迭代。
我知道我的 docx_filename.docx
模板的 for 有问题,我尝试迭代到 listaFinal
。我怎样才能正确迭代,以便在我的 .docx 文件中绘制数据。我期待着任何帮助或建议。提前致谢。
我是这样解决的:
{% for lista in listaFinal %}
{% for i in lista %}
{% for clave, valor in i.items() %}
{% if loop.first %}
{% if clave == '-' %}
{{ clave }}
{% endif %}
{% else %}
{% if clave %}
{{ clave }}
{% endif %}
{{ valor }}
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
更改以下内容:
{% for clave, valor in i.items %}
用于此 {% for clave, valor in i.items() %}
和 {% if forloop.first %}
用于此:{% if loop.first %}
我正在尝试为此在 Django 中生成一个 .docx 文档我正在使用通过 .docx
模板生成文档的 docxtpl(Python docx 模板)。我尝试使用以下数据生成文档:
listaFinal = [
[
{
"-":"LIBROS"
},
{
"":"Responsabilidad Social de las Organizaciones (RSO): Avances y propuestas en América Latina",
"Editorial":"Imprenta Editora Gráfica Real",
"Año":2015,
"Páginas":457,
"Tipo Libro":53
}
],
[
{
"-":"ARTICULOS"
},
{
"":"Gestión universitaria ética y responsable. Indicadores de RSU ",
"Link Articulo":"http://www.revistalatinacs.org/13SLCS/2013_actas/170_Valarezo.pdf",
"issn":"",
"nombre_conferencia":"V Congreso Internacional Latina de Comunicación Social – V CILCS – Universidad de La Laguna, diciembre 2013 "
},
{
"":"Universidad Tecnica Particular de Loja. Proceso de internacionalización de la UTPL",
"Link Articulo":"http://www.iesalc.unesco.org.ve/index.php?option=com_content&view=article&id=1786&Itemid=1147&lang=es",
"issn":"",
"nombre_conferencia":""
}
]
]
在我的 .views
中,我在其中指明了我的模板路径 docx_filename.docx
并将数据发送到 context
:
def generaraDocumento(request):
response = HttpResponse(content_type='application/msword')
response['Content-Disposition'] = 'attachment; filename="cv.docx"'
doc = DocxTemplate(str(settings.BASE_DIR) + '/cv_api/templates/docx_filename.docx')
context = {'listaFinal': listaFinal}
doc.render(context)
doc.save(response)
return response
我有一个名为 docx_filename.docx
的模板,我所做的是在这个模板中指出我想要的结果,在我的 .docx
模板中我有这个能够显示文件中的数据:
{% for lista in listaFinal %}
{% for i in lista %}
{% for clave, valor in i.items %}
{% if forloop.first %}
{% if clave == '-' %}
{{ clave }}
{% endif %}
{% else %}
{% if clave %}
{{ clave }}
{% endif %}
{{ valor }}
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
它给我这样的错误:异常值:'builtin_function_or_method' 对象不可迭代。
我知道我的 docx_filename.docx
模板的 for 有问题,我尝试迭代到 listaFinal
。我怎样才能正确迭代,以便在我的 .docx 文件中绘制数据。我期待着任何帮助或建议。提前致谢。
我是这样解决的:
{% for lista in listaFinal %}
{% for i in lista %}
{% for clave, valor in i.items() %}
{% if loop.first %}
{% if clave == '-' %}
{{ clave }}
{% endif %}
{% else %}
{% if clave %}
{{ clave }}
{% endif %}
{{ valor }}
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
更改以下内容:
{% for clave, valor in i.items %}
用于此 {% for clave, valor in i.items() %}
和 {% if forloop.first %}
用于此:{% if loop.first %}