I received the O'Reilly book "Let Python do the boring things" Use Flask to write an application that converts a locally uploaded PDF file to text.
pip install PyPDF2
This is all the preparation.
I haven't changed anything.
index.py
import os
import PyPDF2
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, session
from werkzeug import secure_filename
app = Flask(__name__)
UPLOAD_FOLDER = './uploads'
ALLOWED_EXTENSIONS = set(['pdf'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['SECRET_KEY'] = os.urandom(24)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/')
def index():
return render_template('pdf.html')
@app.route('/show_pdf', methods=['GET', 'POST'])
def show_pdf():
if request.method == 'POST':
send_data = request.files['send_data']
if send_data and allowed_file(send_data.filename):
filename = secure_filename(send_data.filename)
send_data.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
pdf_file_obj = open('uploads/' + filename, 'rb')
pdf_reader = PyPDF2.PdfFileReader(pdf_file_obj)
page_obj = pdf_reader.getPage(0)
result = page_obj.extractText()
return render_template('pdf.html', result=result)
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
if __name__ == '__main__':
app.debug = True
app.run()
pdf.html
{% extends "base.html" %}
{% block content %}
<form method="post" action="{{ url_for('show_pdf') }}" enctype="multipart/form-data">
<input type="file" id="send_data" name="send_data">
<input type="submit" value="Send">
</fomr>
<div>
{% if result %}
{% for i in result %}
{{ i }}
{% endfor %}
{% endif %}
</div>
{% endblock %}
This time I will use the template created in Pages.
Export a PDF file like this locally as test.pdf.
The application screen is simple. I will upload it for the time being.
There are a lot of things I have to do ...