FEAT: PDF/TEXT uploading and getting file contents done
This commit is contained in:
parent
33bf7d5264
commit
7f7611f700
47
app.py
47
app.py
@ -2,7 +2,7 @@ import os
|
||||
from flask import Flask, render_template, redirect, url_for
|
||||
from flask.globals import request
|
||||
from werkzeug.utils import secure_filename
|
||||
from PyPDF2 import PdfFileReader
|
||||
from workers import pdf2text
|
||||
|
||||
# Constants
|
||||
UPLOAD_FOLDER = './pdf/'
|
||||
@ -13,44 +13,39 @@ app = Flask(__name__)
|
||||
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
||||
|
||||
|
||||
@app.route('/')
|
||||
@ app.route('/')
|
||||
def index():
|
||||
""" The landing page for the app """
|
||||
return render_template('index.html')
|
||||
|
||||
|
||||
@app.route('/quiz', methods=['GET', 'POST'])
|
||||
@ app.route('/quiz', methods=['GET', 'POST'])
|
||||
def quiz():
|
||||
""" Handle upload and conversion of file + other stuff """
|
||||
|
||||
UPLOAD_STATUS = False
|
||||
if request.method == 'POST':
|
||||
try:
|
||||
uploaded_file = request.files['file']
|
||||
# Make directory to store uploaded files
|
||||
|
||||
# Make directory to store uploaded files, if not exists
|
||||
if not os.path.isdir('./pdf'):
|
||||
os.mkdir('./pdf')
|
||||
# Save uploaded file
|
||||
uploaded_file.save(os.path.join(
|
||||
app.config['UPLOAD_FOLDER'], secure_filename(uploaded_file.filename)))
|
||||
UPLOAD_STATUS = True
|
||||
|
||||
# Identify file type and other stuff
|
||||
uploaded_content = None
|
||||
if request.method == 'POST':
|
||||
try:
|
||||
# Retrieve file from request
|
||||
uploaded_file = request.files['file']
|
||||
file_path = os.path.join(
|
||||
app.config['UPLOAD_FOLDER'], secure_filename(uploaded_file.filename))
|
||||
file_exten = uploaded_file.filename.rsplit('.', 1)[1].lower()
|
||||
if file_exten == 'pdf':
|
||||
# TODO: Move PDF2Text conversion to another file
|
||||
print('PDF detected')
|
||||
with open(os.path.join(
|
||||
app.config['UPLOAD_FOLDER'], secure_filename(uploaded_file.filename)), 'rb') as pdf_file:
|
||||
pdf_reader = PdfFileReader(pdf_file)
|
||||
uploaded_content = pdf_reader.getPage(0).extractText()
|
||||
print(uploaded_content)
|
||||
else:
|
||||
# Read text file and store contents
|
||||
pass
|
||||
|
||||
# Save uploaded file
|
||||
uploaded_file.save(file_path)
|
||||
# Get contents of file
|
||||
uploaded_content = pdf2text(file_path, file_exten)
|
||||
# File upload + convert success
|
||||
if uploaded_content is not None:
|
||||
UPLOAD_STATUS = True
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return render_template('quiz.html', uploaded=UPLOAD_STATUS, pdftext=uploaded_content)
|
||||
return render_template('quiz.html', uploaded=UPLOAD_STATUS)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
BIN
pdf/test.pdf
BIN
pdf/test.pdf
Binary file not shown.
@ -11,10 +11,6 @@
|
||||
{% if uploaded == true %}
|
||||
<h1>Your file was uploaded successfully</h1>
|
||||
<h1>Your quiz will appear here</h1>
|
||||
<code>Contents of your file</code>
|
||||
<br />
|
||||
<br />
|
||||
{{ pdftext }}
|
||||
{% else %}
|
||||
<h1>Could not upload file</h1>
|
||||
{% endif %}
|
||||
|
||||
21
workers.py
Normal file
21
workers.py
Normal file
@ -0,0 +1,21 @@
|
||||
from PyPDF2 import PdfFileReader
|
||||
|
||||
|
||||
def pdf2text(file_path, file_exten) -> str:
|
||||
""" Converts a given file to text content """
|
||||
|
||||
_content = None
|
||||
|
||||
# Identify file type and get its contents
|
||||
if file_exten == 'pdf':
|
||||
with open(file_path, 'rb') as pdf_file:
|
||||
_pdf_reader = PdfFileReader(pdf_file)
|
||||
_content = _pdf_reader
|
||||
print('PDF operation done!')
|
||||
|
||||
elif file_exten == 'txt':
|
||||
with open(file_path, 'r') as txt_file:
|
||||
_content = txt_file.read()
|
||||
print('TXT operation done!')
|
||||
|
||||
return _content
|
||||
Loading…
x
Reference in New Issue
Block a user