前提・実現したいこと
Python(Flask,Matplotlib)で、アップロードされたExcelファイルを基にグラフを作成したい
①WebページからExcelテンプレートファイルをダウンロード
②値を入力したExcelファイルを選択し、送信
③uploadsディレクトリに格納される
④アップロードされたExcelファイルを元にグラフを作成
①~③は問題なく動作しています。
④は完成イメージです。
エラーメッセージ・該当のソースコード
Python
if request.method == 'POST': file = request.files['file'] UPLOAD_FOLDER = r'C:\xxx\xxx\xxx\TestProject\uploads' app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER if file: filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) wb = xlrd.open_workbook(r"C:\xxx\xxx\xxx\TestProject\uploads\test.xlsx") sheets = wb.sheets() month = np.array(sheets['Sheet1'].cell(row=i, column=0).value for i in range(1, 13)) sale = np.array(sheets['Sheet1'].cell(row=i, column=1).value for i in range(1, 13)) cost = np.array(sheets['Sheet1'].cell(row=i, column=2).value for i in range(1, 13)) benefit = sale - cost #プロットの処理が続く
エラー例①
Python
sheets = wb.sheets() month = np.array([sheets['Sheet1'].cell(row=i, column=0).value for i in range(1, 13)]) sale = np.array([sheets['Sheet1'].cell(row=i, column=1).value for i in range(1, 13)]) cost = np.array([sheets['Sheet1'].cell(row=i, column=2).value for i in range(1, 13)]) benefit = sale - cost plt.plot(month, sale) plt.plot(month, cost) plt.plot(month, benefit) # month行でエラー → TypeError: list indices must be integers or slices, not str
エラー例②
Python
sheets = wb.sheets() month = np.array(sheets['Sheet1'].cell(row=i, column=0).value for i in range(1, 13)) sale = np.array(sheets['Sheet1'].cell(row=i, column=1).value for i in range(1, 13)) cost = np.array(sheets['Sheet1'].cell(row=i, column=2).value for i in range(1, 13)) benefit = sale - cost plt.plot(month, sale) plt.plot(month, cost) plt.plot(month, benefit) #benefit行でエラー → TypeError: unsupported operand type(s) for -: 'generator' and 'generator'
試したこと
下記のコードは、正しく動作し冒頭にある④のグラフが作成されます
Python
w = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) x = np.array([10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]) y = np.array([7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]) z = x - y plt.plot(w, x, label='sale') plt.plot(w, y, label='cost') plt.plot(w, z, label='benefit')
環境
開発環境:PyCharm
サーバ :localhost
ブラウザ:Chrome
まだ回答がついていません
会員登録して回答してみよう