Django创建.odt或.docx文件下载 [英] Django create .odt or .docx documents to download

查看:589
本文介绍了Django创建.odt或.docx文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据我的数据库中的信息生成.odt或.docx文件。假设我有一个模型:

I need to generate either .odt or .docx files based on the information I have in my database. Let's say I have a model:

class Contact(models.Model):
    first_name = models.CharField()
    last_name = models.CharField()
    email = models.EmailField()

我希望用户能够生成包含该信息的Office文档以及其他一些文本。我查看了这个例子,它使用了python-docx,它给了我一个想法如何生成该文档。但是我无法确定这个文件被保存或甚至创建的位置。在我的模板中,我有一个链接:

I want that users are able to generate office document that contains that information and also some other text. I took a look for this example which is using python-docx and it gives me an idea how to generate that document. But I can't figure out where this file is saved or is it even created. In my template I have a link:

<a href="{{ contact.generate_docx }}">generate .docx document</a>

其中 generate_docx()运行代码可以从上面提供的链接中找到。

where generate_docx() runs the code which can be found from the link I provided above.

我如何实现我的系统,以便当我的链接被点击时,应该创建或更新文档关于数据库中的数据,然后下载到用户计算机?将该文档保存到数据库不是强制性的,但我也有兴趣听到如何做到这一点。

How I can implement my system so that when my link is clicked, then document should be created or updated based on the data in database and after that downloaded to users computer? It's not mandatory to save that document to database but I'm also interested to hear how to do that as well.

推荐答案

您可以在docx文件中使用django模板语言,这是一个真正的xml文件的zip存档,然后通过模板引擎运行相应的xml文件。我得到了这个想法: http://reinout.vanrees.org /weblog/2012/07/04/document-automation.html

You can use the django template language inside a docx file, which is really a zip archive of xml files, and then run the appropriate xml file through the template engine. I got the idea here: http://reinout.vanrees.org/weblog/2012/07/04/document-automation.html

说起来比说起来容易。最终,我以这样的方式在python3中运行:

Easier said than done though. Eventually, I got it running in python3 like this:

from zipfile import ZipFile
from io import BytesIO

from django.template import Context, Template

def render_to_docx(docx, context):
    tmp = BytesIO()
    with ZipFile(tmp, 'w') as document_zip, ZipFile(docx) as template_zip:
        template_archive = {name: template_zip.read(name) for name in template_zip.namelist()}
        template_xml = template_archive.pop('word/document.xml')
        for n, f in template_archive.items():
            document_zip.writestr(n, f)
        t = Template(template_xml)
        document_zip.writestr('word/document.xml', t.render(Context(context)))        
    return tmp

在视图中:

response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
response['Content-Disposition'] = 'attachment; filename=document.docx'
zipfile = render_to_docx('template.docx'), context_dictionary)
response.write(zipfile.getvalue())
return response

这篇关于Django创建.odt或.docx文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆