【问题标题】:hug create GET endpoint to download created word document拥抱创建 GET 端点以下载创建的 Word 文档
【发布时间】:2020-04-06 10:19:30
【问题描述】:

我正在使用 Python hug API 想为前端创建一个 GET API。前端可以下载创建的 Word 文档文件,例如通过下载按钮。但是,经过documentation 之后,我仍然想不出办法。

到目前为止,这是我的工作脚本:

import os
import hug
from docx import Document

@hug.get("/download_submission_document")
def download_submission_document():
    file_name = 'example.docx'
    document = Document()
    document.add_heading('Test header', level=2)
    document.add_paragraph('Test paragraph')
    document.save(file_name)
    # TO DO: send a created file to frontend

我不确定我们是否可以立即发送对象,或者我们必须先将其保存在某个地方,然后再发送前端。 (要求:hugpython-docx

我正在尝试使用类似的东西

@hug.get("/download_submission_document", output=hug.output_format.file)

但不确定如何返回文件。

【问题讨论】:

    标签: python python-3.x hug


    【解决方案1】:

    好的,我找到了一个比我想象的更容易的解决方案。只需执行以下操作:

    @hug.get("/download_submission_document", output=hug.output_format.file)
    def download_submission_document():
        file_name = 'example.docx'
        document = Document()
        document.add_heading('Test header', level=2)
        document.add_paragraph('Test paragraph')
        document.save(file_name)
        return file_name
    

    返回file_name已经下载了docx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多