2016-06-30 2 views
0

私はSwagger APIからExcelファイルを返そうとしています。 Flaskerを使ってSwaggerラッパーを使ってFlaskを使って構築しました。ここにコードがあります -Python Flask Swagger 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 

これは、動作させるために名前を変更する必要があるダウンロード可能なリンクを返します。

質問:私はExcelファイルでこれを行うことができません。どのようにしても、ダウンロードして名前を変更すると、ファイルが壊れているとExcelが判断します。

私はpyexcelとpandasを賞賛した作家を試みましたが、うまくいかなかった。助けてください!

答えて

0

mimetypeを変更しようとしましたか?

私は、Microsoftのファイルの詳細については、こちらをmimetypeを見つけることができるExcelの伝統的なMIMEタイプがapplication/vnd.ms-excel

であることを考える:チェスターは、はい、私がやったWhat is a correct mime type for docx, pptx etc?

+0

感謝を!私は 'application/vnd.ms-excel'と' application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'の両方を試しました。ファイルをダウンロードして、名前をmyfile.xlsxに変更できますが、ファイルが壊れているとエラーが表示されます。私がメモ帳でそれを開くと、私が見ているたくさんのジャンクなキャラクター... –

関連する問題