Flask无法使用xlrd读取Excel文件

时间:2018-12-01 00:12:06

标签: python flask

我将xlrd与python结合使用,通过将文件与python文件放在smae目录中来从excel文件中获取数据,并且它可以正常工作,但是当我使用flask应用程序执行此操作时,该应用程序需要一些输入并对其进行处理并搜索结果在excel文件中无效,并且在html页面中提交表单时发生错误。

app.py文件代码

from flask import Flask, render_template, request
import jinja2
from math import exp
import xlrd

app = Flask(__name__)
env = jinja2.Environment()
app.jinja_env.filters['zip'] = zip
env.globals.update(zip=zip)

otis = xlrd.open_workbook("otis.xlsx")
sheet = otis.sheet_by_index(0)
portd = [sheet.cell(i,0).value for i in range(1,sheet.nrows)]

def R_value(qg, Pup, Pdn, C, k, Yg, Tup):
    Ap = (qg)/(1248*C*Pup*(k/((k-1)*Yg*(Tup+460))*((Pdn/Pup)**(2/k)-(Pdn/Pup)**((k+1)/k)))**.5)
    d = 1.1284*(Ap)**.5
    for x in portd:
        r = None
        if x == d :
            r = x
        elif round(x,4) == round(d,4):
            r = x
        elif round(x,3) == round(d,3):
            r = x
        elif round(x,2) == round(d,2):
            r = x
        elif round(x,1) == round(d,1):
            r = x

    row = portd.index(r) + 1
    tab = sheet.cell(row,0).value

    for i in [1,2,3]:
        if sheet.cell(row, i).value != xlrd.empty_cell.value :
            return R
            R = sheet.cell(row, i).value

@app.route('/')
def index():
   result = False
   return render_template('index.html', result=result)

@app.route('/',methods = ['POST', 'GET'])


def result():
   result = True
   if request.method == 'POST':
      qg = float(request.form['qg'])
      Pup = float(request.form['Pup'])
      Pdn = float(request.form['Pdn'])
      Tup = float(request.form['Tup'])
      k = float(request.form['k'])
      C = float(request.form['C'])
      gravity = .75

      R = R_value(qg, Pup, Pdn, C, k, gravity, Tup)

      return render_template("index.html", R=R, qg=qg, Pup=Pup, Pdn=Pdn, Tup=Tup, k=k, C=C )

if __name__ == '__main__':
   app.run(debug = True)

并且提交html页面按钮时发生了错误

builtins.ValueError ValueError:列表中没有任何内容

File "C:\Python37\Scripts\venv\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python37\Scripts\venv\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Python37\Scripts\venv\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python37\Scripts\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Python37\Scripts\venv\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python37\Scripts\venv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python37\Scripts\venv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python37\Scripts\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Python37\Scripts\venv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python37\Scripts\venv\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\A.Toba\Desktop\New folder (2)\app.py", line 58, in result
R = R_value(qg, Pup, Pdn, C, k, gravity, Tup)
File "C:\Users\A.Toba\Desktop\New folder (2)\app.py", line 31, in R_value
Open an interactive python shell in this framerow = portd.index(r) + 1

0 个答案:

没有答案