2016-06-30 65 views
0

我试图返回从扬鞭API的Excel文件。使用带有Flasgger的Swagger包装的Flask构建。下面的代码 -的Python瓶扬鞭Flasgger下载Excel中

@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和熊猫出类拔萃的作家,没有工作了。请帮忙!

回答

0

您是否尝试更改mimetype?

我认为,Excel中的MIME类型的传统是application/vnd.ms-excel

你能找到的微软文件的详细信息MIMETYPE这里:What is a correct mime type for docx, pptx etc?

+0

感谢切斯特是的,我做到了!我尝试了'application/vnd.ms-excel'和'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'。我可以下载该文件,将其重命名为myfile.xlsx,但出现错误说文件已损坏。如果我在记事本中打开它,我会看到很多垃圾字符... –