TypeError:使用烧瓶读取pdf文件时,预期的str,字节或os.PathLike对象,而不是FileStorage [英] TypeError: expected str, bytes or os.PathLike object, not FileStorage while reading pdf files using flask
问题描述
我正在尝试使用flask应用程序读取python文件.我正在使用pdfminer来阅读pdf文本.
I am trying to read a python file using flask application. I am using pdfminer to read the pdf text.
@app.route('/getfile', methods=['POST'])
def getfile():
request_data = request.files['file']
rsrcmgr = PDFResourceManager()
retstr = io.StringIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
fp = open(request_data, 'rb')
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
maxpages = 0
caching = True
pagenos = set()
for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages,
password=password,
caching=caching,
check_extractable=True):
interpreter.process_page(page)
text = retstr.getvalue()
fp.close()
device.close()
retstr.close()
return text
不幸的是,它引发了错误,
Unfortunately it throws error,
- 在 http://0.0.0.0:5000/上运行(按CTRL + C退出)127.0.0.1--[11/Apr/2018 16:07:53]"GET/hello HTTP/1.1" 200-[2018-04-11 16:07:55,720]应用程序错误:/getfile异常[POST]追溯(最近一次通话):文件"c:\ users \ rb287jd \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ flask \ app.py",第1982行,在wsgi_app中响应= self.full_dispatch_request()文件"c:\ users \ rb287jd \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ flask \ app.py",第1614行,在full_dispatch_request中rv = self.handle_user_exception(e)文件"c:\ users \ rb287jd \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ flask \ app.py",第1517行,在handle_user_exception中重新提升(exc_type,exc_value,tb)文件"c:\ users \ rb287jd \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ flask_compat.py",第33行,加价提高价值文件"c:\ users \ rb287jd \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ flask \ app.py",第1612行,在full_dispatch_request中rv = self.dispatch_request()文件"c:\ users \ rb287jd \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ flask \ app.py",第1598行,在dispatch_request中在getfile中,返回self.view_functionsrule.endpoint文件"C:/Users/RB287JD/Documents/Programs/flask_1.py",第27行fp = open(request_data,'rb').decode("utf-8")TypeError:预期的str,字节或os.PathLike对象,而不是FileStorage127.0.0.1--[11/Apr/2018 16:07:55]"POST/getfile HTTP/1.1" 500-
- Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) 127.0.0.1 - - [11/Apr/2018 16:07:53] "GET /hello HTTP/1.1" 200 - [2018-04-11 16:07:55,720] ERROR in app: Exception on /getfile [POST] Traceback (most recent call last): File "c:\users\rb287jd\appdata\local\programs\python\python36\lib\site-packages\flask\app.py", line 1982, in wsgi_app response = self.full_dispatch_request() File "c:\users\rb287jd\appdata\local\programs\python\python36\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request rv = self.handle_user_exception(e) File "c:\users\rb287jd\appdata\local\programs\python\python36\lib\site-packages\flask\app.py", line 1517, in handle_user_exception reraise(exc_type, exc_value, tb) File "c:\users\rb287jd\appdata\local\programs\python\python36\lib\site-packages\flask_compat.py", line 33, in reraise raise value File "c:\users\rb287jd\appdata\local\programs\python\python36\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request rv = self.dispatch_request() File "c:\users\rb287jd\appdata\local\programs\python\python36\lib\site-packages\flask\app.py", line 1598, in dispatch_request return self.view_functionsrule.endpoint File "C:/Users/RB287JD/Documents/Programs/flask_1.py", line 27, in getfile fp = open(request_data, 'rb').decode("utf-8") TypeError: expected str, bytes or os.PathLike object, not FileStorage 127.0.0.1 - - [11/Apr/2018 16:07:55] "POST /getfile HTTP/1.1" 500 -
我如何读取烧瓶内的输入pdf文件?PS.我不想在代码中的任何位置提供我的文件位置.我想即时进行.
How do i read an input pdf file inside the flask? PS. i dont want to provide my file location inside anywhere in the code. I want to do it on the fly.
推荐答案
request.files ['file']
是 http://flask.pocoo.org/docs/0.12/api/#flask.Request.files ),因此您不能执行 fp = open(request_data,'rb')
.FileStorage对象包含一个 stream
属性,该属性应指向一个打开的临时文件,您可以将其传递给 PDFPage.get_pages()
The request.files['file']
is an instance of a FileStorage class (see also http://flask.pocoo.org/docs/0.12/api/#flask.Request.files), so you can't do the fp = open(request_data, 'rb')
. The FileStorage object contains a stream
attribute that should point to an open temporary file, and probably you can pass that to PDFPage.get_pages()
所以,像这样:
@app.route('/getfile', methods=['POST'])
def getfile():
file = request.files['file']
rsrcmgr = PDFResourceManager()
retstr = io.StringIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
maxpages = 0
caching = True
pagenos = set()
for page in PDFPage.get_pages(file.stream, pagenos, maxpages=maxpages,
password=password,
caching=caching,
check_extractable=True):
interpreter.process_page(page)
text = retstr.getvalue()
device.close()
retstr.close()
return text
这篇关于TypeError:使用烧瓶读取pdf文件时,预期的str,字节或os.PathLike对象,而不是FileStorage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!