我试图从Swagger返回一个excel文件。使用用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说文件是损坏的,这就是。
我试过pyexcel和熊猫出类拔萃的作家,但没有成功。请帮帮我!
发布于 2018-07-20 03:37:24
尝试使用flasgger下载excel。您可以将响应类型更改为“应用程序/八位元流”来解析它。

发布于 2016-06-30 13:02:10
你试过改变米制吗?
我认为excel的传统mimetype是application/vnd.ms-excel。
您可以在这里找到有关microsoft文件mimetype的更多详细信息:What is a correct mime type for docx, pptx etc?
https://stackoverflow.com/questions/38118900
复制相似问题