【问题标题】:Downloadable docx file in Django在 Django 中可下载的 docx 文件
【发布时间】:2013-10-16 09:45:20
【问题描述】:

我的 django web 应用程序制作并保存 docx,我需要使其可下载。 我使用简单的render_to_response,如下所示。

return render_to_response("test.docx", mimetype='application/vnd.ms-word')

但是,它会引发类似'utf8' codec can't decode byte 0xeb in position 15: invalid continuation byte的错误

我无法将此文件作为静态文件提供,因此我需要找到一种方法将其作为静态文件提供。 非常感谢您的帮助。

【问题讨论】:

  • 也许这会有所帮助link

标签: python django python-docx


【解决方案1】:

是的,正如 wardk 所说,更简洁的选项是使用https://python-docx.readthedocs.org/

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

【讨论】:

  • 这是一个很好的解决方案,特别是因为文件不需要保存到驱动器。
  • 不确定,但您不需要在HttpResponse中添加文档,即response = HttpResponse(document, content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
【解决方案2】:

用于下载已经存在的 DOCX 文件

我稍微修改了@luc 的回答。他的帖子让我成功了 98%。我进行了更改,因为我不需要创建 Word 文件。我的用法只是传递文件服务器上存在的文件,然后准备下载。

因为下载的文档已经存在。

def download(request):
filepath = os.path.abspath(r"path\to\file.docx")
print('SLA FILE: ', filepath)
if os.path.exists(filepath):
    with open(filepath, 'rb') as worddoc: # read as binary
        content = worddoc.read() # Read the file
        response = HttpResponse(
            content,
            content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
        )
        response['Content-Disposition'] = 'attachment; filename=download_filename.docx'
        response['Content-Length'] = len(content) #calculate length of content
        return response
else:
    return HttpResponse("Failed to Download SLA")

【讨论】:

    【解决方案3】:

    感谢python-docx,我设法从 django 视图生成了一个 docx 文档。

    这是一个例子。希望对你有帮助

    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
    

    【讨论】:

    • 为什么是 StringIO?我只是做“document.save(response)”
    【解决方案4】:

    试试这个回复:

    response = HttpResponse(mydata, mimetype='application/vnd.ms-word')
    response['Content-Disposition'] = 'attachment; filename=example.doc'
    return response 
    

    【讨论】:

    • 内容在哪里?
    • 将 mimetype 传递给 HttpResponse 在 Django 1.7 中已弃用和删除。
    【解决方案5】:

    您的“test.docx”路径是否可能包含非 ascii 字符?你检查了django调试页面上的所有局部变量吗?

    我下载 xml 文件所做的不是在磁盘上创建文件,而是使用内存文件(使我免于处理文件系统、路径……):

        memory_file = StringIO.StringIO()
        memory_file.writelines(out) #out is an XMLSerializer object in m case
    
        response = HttpResponse(memory_file.getvalue(), content_type='application/xml')
        response['Content-Disposition'] = 'attachment; filename="my_file.xml"'
        response['Content-Length'] = memory_file.tell()
        return response
    

    也许您可以根据您的 docx 情况进行调整。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      • 2016-10-18
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多