我正在尝试从 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”来解决它。
关于Python Flask Swagger Flasgger 下载 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38118900/