Python 瓶子上传xlsx并使用xls2db进行处理

Python 瓶子上传xlsx并使用xls2db进行处理,python,bottle,Python,Bottle,我希望从瓶子上传中获取一个xlsx文件,并将该文件发送到xls2db模块以填充sqlite db,但我似乎没有正确地传递该文件。 # 更新-我现在得到这个错误 2016-02-16 22:16:55 ERROR:root:Traceback (most recent call last): 2016-02-16 22:16:55 ERROR:root: File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 7

我希望从瓶子上传中获取一个xlsx文件,并将该文件发送到xls2db模块以填充sqlite db,但我似乎没有正确地传递该文件。 #

更新-我现在得到这个错误

    2016-02-16 22:16:55 ERROR:root:Traceback (most recent call last):
    2016-02-16 22:16:55 ERROR:root:  File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 764, in _handle
    2016-02-16 22:16:55 ERROR:root:    return route.call(**args)
    2016-02-16 22:16:55 ERROR:root:  File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 1575, in wrapper
   2016-02-16 22:16:55 ERROR:root:    rv = callback(*a, **ka)
   2016-02-16 22:16:55 ERROR:root:  File "/home/mcsl/mysite/bottle_app.py", line 91, in do_upload
   2016-02-16 22:16:55 ERROR:root:    upload_xls(file_path)
   2016-02-16 22:16:55 ERROR:root:  File "/home/mcsl/mysite/bottle_app.py", line 22, in upload_xls
   2016-02-16 22:16:55 ERROR:root:    xls2db(file_name, "test_db.db")
   2016-02-16 22:16:55 ERROR:root:  File "/home/mcsl/.local/lib/python2.7/site-packages/xls2db/__init__.py", line 29, in xls2db
  2016-02-16 22:16:55 ERROR:root:    wb = xlrd.open_workbook(infile)
  2016-02-16 22:16:55 ERROR:root:  File "/usr/local/lib/python2.7/dist-packages/xlrd/__init__.py", line 435, in open_workbook
  2016-02-16 22:16:55 ERROR:root:    ragged_rows=ragged_rows,
  2016-02-16 22:16:55 ERROR:root:  File "/usr/local/lib/python2.7/dist-packages/xlrd/book.py", line 87, in open_workbook_xls
  2016-02-16 22:16:55 ERROR:root:    ragged_rows=ragged_rows,
  2016-02-16 22:16:55 ERROR:root:  File "/usr/local/lib/python2.7/dist-packages/xlrd/book.py", line 594, in biff2_8_load
  2016-02-16 22:16:55 ERROR:root:    raise XLRDError("File size is 0 bytes")
 2016-02-16 22:16:55 ERROR:root:XLRDError: File size is 0 bytes
我可以上传文件,现在我可以在服务器上看到它,但是当我把它传递给函数时,它会报告它是空的

    @route('/upload', method='POST')
    def do_upload():
        data = request.files.upload
        upload = request.files.get('upload')
        name, ext = os.path.splitext(data.filename)
        #check the file is an excel file
        if ext not in ( '.xls', '.xlsx'):
            return "File extension not allowed."

            save_path = "/home/mcslo/mysite".encode("utf-8")
            file_path= "{path}/{file}".format(path=save_path,        file=data.filename)
            raw = data.file.read() # This is dangerous for big file

           open_file = open(file_path, 'w')
           open_file.write(raw)
           open_file.close
           upload_xls(file_path)


    def upload_xls(file_name):
file_name = file_name
conn = sqlite.connect("test_db.sqlite")
cur = conn.cursor()
cur.execute("DROP TABLE IF EXISTS Requests;")
cur.execute("DROP TABLE IF EXISTS Complaints;")
xls2db(file_name, "test_db.db")
redirect("/work")

根据,您需要将文件名或工作簿传递到
xls2b
。啊,这是因为它没有保存到磁盘。。。让我想一想,你能试试吗:
importopenpyxl;wb=openpyxl.load_工作簿(data.filename)
然后将
wb
传递给调用
upload_xls(raw,wb)
然后执行:
xls2db(filename,“test_db.db”)
openpyxl.load_工作簿(data.filename)2016-02-12 14:34:52错误:root:File“/usr/local/lib/python2.7/dist packages/openpyxl/reader/excel.py”,第119行,在load_工作簿2016-02-12 14:34:52中错误:root:raiseinvalidFileException(unicode(e))2016-02-12 14:34:52错误:root:InvalidFileException:[Errno 2]没有这样的文件或目录:“Testthis.xlsx.xlsx”。这就是我得到的。我已经决定上传文件并保存到磁盘,然后发送它,但它保存为空文件?
    2016-02-16 22:16:55 ERROR:root:Traceback (most recent call last):
    2016-02-16 22:16:55 ERROR:root:  File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 764, in _handle
    2016-02-16 22:16:55 ERROR:root:    return route.call(**args)
    2016-02-16 22:16:55 ERROR:root:  File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 1575, in wrapper
   2016-02-16 22:16:55 ERROR:root:    rv = callback(*a, **ka)
   2016-02-16 22:16:55 ERROR:root:  File "/home/mcsl/mysite/bottle_app.py", line 91, in do_upload
   2016-02-16 22:16:55 ERROR:root:    upload_xls(file_path)
   2016-02-16 22:16:55 ERROR:root:  File "/home/mcsl/mysite/bottle_app.py", line 22, in upload_xls
   2016-02-16 22:16:55 ERROR:root:    xls2db(file_name, "test_db.db")
   2016-02-16 22:16:55 ERROR:root:  File "/home/mcsl/.local/lib/python2.7/site-packages/xls2db/__init__.py", line 29, in xls2db
  2016-02-16 22:16:55 ERROR:root:    wb = xlrd.open_workbook(infile)
  2016-02-16 22:16:55 ERROR:root:  File "/usr/local/lib/python2.7/dist-packages/xlrd/__init__.py", line 435, in open_workbook
  2016-02-16 22:16:55 ERROR:root:    ragged_rows=ragged_rows,
  2016-02-16 22:16:55 ERROR:root:  File "/usr/local/lib/python2.7/dist-packages/xlrd/book.py", line 87, in open_workbook_xls
  2016-02-16 22:16:55 ERROR:root:    ragged_rows=ragged_rows,
  2016-02-16 22:16:55 ERROR:root:  File "/usr/local/lib/python2.7/dist-packages/xlrd/book.py", line 594, in biff2_8_load
  2016-02-16 22:16:55 ERROR:root:    raise XLRDError("File size is 0 bytes")
 2016-02-16 22:16:55 ERROR:root:XLRDError: File size is 0 bytes
    @route('/upload', method='POST')
    def do_upload():
        data = request.files.upload
        upload = request.files.get('upload')
        name, ext = os.path.splitext(data.filename)
        #check the file is an excel file
        if ext not in ( '.xls', '.xlsx'):
            return "File extension not allowed."

            save_path = "/home/mcslo/mysite".encode("utf-8")
            file_path= "{path}/{file}".format(path=save_path,        file=data.filename)
            raw = data.file.read() # This is dangerous for big file

           open_file = open(file_path, 'w')
           open_file.write(raw)
           open_file.close
           upload_xls(file_path)


    def upload_xls(file_name):
file_name = file_name
conn = sqlite.connect("test_db.sqlite")
cur = conn.cursor()
cur.execute("DROP TABLE IF EXISTS Requests;")
cur.execute("DROP TABLE IF EXISTS Complaints;")
xls2db(file_name, "test_db.db")
redirect("/work")