使用Bottle在python中开发Web应用程序。我想上传xlsx文件以从中加载数据。我使用openpyxl来做到这一点,但我得到一个错误
import wtforms
import openpyxl
from bottle import request, route, run, view,
我的表格:
class UploadForm(wtforms.Form):
file = wtforms.FileField()
查看:
@route("/upload/file", method=['GET', 'POST'])
@view("upload")
def upload_file():
form = UploadForm()
if request.method == 'POST':
form = UploadForm(request.POST)
if form.validate():
wb = openpyxl.load_workbook(form)
return {'form': form}
模板:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="/static/css/normalize.css">
<link rel="stylesheet" href="/static/css/skeleton.css">
</head>
<body>
<form action="/upload/file" method="POST" enctype="multipart/form-data">
{{ !form.file.label }}
{{ !form.file }}
<div class="row">
<input type="submit" value="Upload" />
</div>
</form>
</body>
</html>
`错误:
Traceback (most recent call last):
File "/home/wojjak/.local/lib/python2.7/site-packages/bottle.py", line 862, in _handle
return route.call(**args)
File "/home/wojjak/.local/lib/python2.7/site-packages/bottle.py", line 1740, in wrapper
rv = callback(*a, **ka)
File "/home/wojjak/.local/lib/python2.7/site-packages/bottle.py", line 3639, in wrapper
result = func(*args, **kwargs)
File "/home/wojjak/PycharmProjects/untitled/parsowanie.py", line 84, in upload_file
if form.validate():
File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/excel.py", line 171, in load_workbook
archive = _validate_archive(filename)
File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/excel.py", line 91, in _validate_archive
if not is_file_like and os.path.isfile(filename):
File "/usr/lib/python2.7/genericpath.py", line 37, in isfile
st = os.stat(path)
TypeError: coercing to Unicode: need string or buffer, UploadForm found
任何人都知道如何解决这个问题
编辑:
确定。我解决了它
def upload_file():
form = UploadForm()
if request.method == 'POST':
form = UploadForm(request.POST)
if form.file.data:
wb = openpyxl.load_workbook(request.files[form.file.name].file)