我正在学习 Flask,所以我想创建一个可以接受文件并从中提取文本的应用程序
这是错误:
fitz.fitz.FileNotFoundError: no such file: 'test.pdf'
文件是从 HTML 模板中检索的,其中上传了 pdf 文件并将其转换为文本。我认为问题在于该函数需要一个文件路径而不是文件本身作为“FileStorage”类型。
现在这是名为“app.py”的烧瓶应用程序的代码
@app.route('/upload', methods=['POST'])
def upload():
global keywords
if request.method == 'POST':
file = request.files['file']
file_name = str(file).split()[1].replace("'", "")
file_extension = os.path.splitext(file_name)
if file_extension[1] == '.txt':
text = file.read().decode('utf-8')
elif file_extension[1] == '.docx':
document = Document(file)
text = ' '.join([paragraph.text for paragraph in document.paragraphs])
elif file_extension[1] == '.pdf':
fitz.open(file.filename)
text = ""
for page in fitz.pages():
text += page.get_text()
else:
#unsupport file type
return render_template('error.html', error_message="Unsupported file type")
return render_template('index.html', final=text)
关于问题的一些额外信息:
我确实看到有人建议将文件存储到服务器中,然后从那里访问它,但我不确定该怎么做。然后程序是否需要访问外部服务器或者它可以存储在本地主机中?