使用xls2db

时间:2016-02-11 20:57:15

标签: python bottle

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

     @route('/',method="GET")   # Select the xls sheet from this route
def testRoute():
    return ('''</br>
           <h1> Select the spreadsheet</h1>
           <form action="/upload" method="post" enctype="multipart/form-data">
            <input type="text" name="name" />
            <input type="file" name="data" />

            <input type="submit"/>
       </form>  ''')


@route('/upload', method='POST')
def do_upload():
    name = request.forms.name
    data = request.files.data
    if name and data and data.file:
        raw = data.file.read() # This is dangerous for big files
        filename = data.filename
        #return "Hello %s! You uploaded %s (%d bytes)." % (name,   filename, len(raw))
        upload_xls(raw, filename)

连接到数据库删除表(如果存在)并将xls表转换为sqlite数据库

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

更新 - 我现在收到此错误。

    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")

0 个答案:

没有答案