Python Flask Swagger Flasgger 下载 Excel

标签 python flask swagger xlsx

我正在尝试从 Swagger API 返回 excel 文件。使用 Flask 和带有 Flasgger 的 Swagger 包装器构建它。这是代码 -

@app.route('/cluster', methods=['POST'])
def index():
    """
    This API will help you generate clusters based on keywords present in unstructured text
    Call this api passing the following parameters - 
        Dataset Path - <hostname>\\<path to dataset>
        Column Name based on which clustering needs to be done
        Number of Clusters
    Sample URL: http://localhost:8180/cluster/clusters.csv?dataset=\\\\W1400368\\c$\\Users\\VK046010\\Documents\\Python%20Scripts\\RevCycle_PatientAcc.csv&ext=csv&col=SR_SUM_TXT&no_of_clusters=100
    ---
    tags:
      - Clustering API
    parameters:
      - name: dataset
        in: formData
        type: file
        required: true
        description: The fully qualified path of the dataset without the extension.
      - name: col
        in: query
        type: string
        required: true
        description: The column name on which the clustering needs to be done
      - name: no_of_clusters
        in: query
        type: integer
        required: true
        description: The number of clusters
    """    
    global data      
    data = data.fillna('NULL')



output = StringIO.StringIO()
data.to_csv(output,index=False)

resp = Response(output.getvalue(), mimetype="text/csv")
resp.headers["Accept"] = "text/csv"
resp.headers['Access-Control-Allow-Origin'] = '*'
resp.headers["Content-Disposition"] = "attachment; filename=clusters.csv"
return resp

这会返回一个可下载链接,我必须将其重命名为 csv 才能使其正常工作。

问题:我无法对 Excel 文件执行此操作。不管我怎么做,一旦我下载并重命名,excel 就会说文件已损坏,就是这样。

我尝试了 pyexcel 和 pandas excel writer,但没有成功。请帮忙!

最佳答案

尝试使用flasgger下载excel。您可以将响应类型更改为“application/octet-stream”来解决它。 Sample Image

关于Python Flask Swagger Flasgger 下载 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38118900/

相关文章:

python - 如何在 python 中使用 kivy 在 TextInput 中启用/禁用编辑

python - Flask socketio session 查询字符串

python - Gunicorn 导入错误 : url_for not found in flask, 但 native Flask 运行正常

java - 找不到@EnableSwagger 注释

rest - 如何将swagger-ui与部署在运行在eclipse上的tomcat中的服务一起使用

go - 具有不同“必填”字段的POST和GET模型

python - Ctypes:OSError:异常:堆栈溢出

python - 使用 [n :] and [n: end] gives different results when using . value_list() 切片 QuerySet

sqlalchemy - Flask 和 SQLAlchemy 问题

python - 如何使用 raw_input 获取汉字