Create a web app that converts PDF to text using Flask and PyPDF2

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.

Actual situation

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 %}

Try to move

This time I will use the template created in Pages.

スクリーンショット 2017-06-05 17.37.29.png

Export a PDF file like this locally as test.pdf.

スクリーンショット 2017-06-05 17.38.02.png

The application screen is simple. I will upload it for the time being.

スクリーンショット 2017-06-05 17.38.35.png

There are a lot of things I have to do ...

Recommended Posts

Create a web app that converts PDF to text using Flask and PyPDF2
Create a simple web app with flask
I want to make a web application using React and Python flask
Create a web map using Python and GDAL
Create a Mac app using py2app and Python3! !!
How to deploy a web app made with Flask to Heroku
Steps to set up Pipenv, create a CRUD app with Flask, and containerize it with Docker
Creating a web application using Flask ②
Creating a web application using Flask ①
Creating a web application using Flask ③
Creating a web application using Flask ④
Python: Introduction to Flask: Creating a number identification app using MNIST
Create and deploy a Django (PTVS) app using Azure Table storage
I made a web application in Python that converts Markdown to HTML
Create a web app that can be easily visualized with Plotly Dash
Create a web service with Docker + Flask
Start a web server using Bottle and Flask (I also tried using Apache)
I tried to create a sample to access Salesforce using Python and Bottle
Create a simple app that incorporates the Fetch API of Ajax requests in Flask and explain it quickly
Create an application that inputs, displays, and deletes forms by using an array as a DB with Python / Flask.
Go beginner tried to create a cloud native web application using Datastore / GAE
Try to get a web page and JSON file using Python's Requests library
Create a plugin that allows you to search Sublime Text 3 tabs in Python
[Python, PyPDF2] A script that divides a spread PDF into two left and right
A spell that eliminates non-Japanese characters and symbols to create a Japanese plaintext corpus
[ES Lab] I tried to develop a WEB application with Python and Flask ②
Build a flask app made with tensorflow and dlib to work on centos7
I want to create a web application that uses League of Legends data ①
A script that converts between Django language files (PO) and tab-separated text (TSV)
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
Create a beauty pageant support app using PyLearn2
Launch a web server with Python and Flask
How to create a multi-platform app with kivy
How to create a USB that Linux and Win10 installer and winpe can boot UEFI
I made a web application that maps IT event information with Vue and Flask
How to host web app backend processing in Python using a rental server subdomain
Create a simple API just to input and output JSON files ~ Python / Flask edition ~
Create a color sensor using a Raspberry Pi and a camera
(Python) Try to develop a web application using Django
Easy machine learning with scikit-learn and flask ✕ Web app
Create a web page that runs a model that increases the resolution of the image using gradio, which makes it easy to create a web screen
[Python] A convenient library that converts kanji to hiragana
Write code to Unit Test a Python web app
To myself as a Django beginner (1) --Create a project app--
To myself as a Django beginner (4) --Create a memo app--
A Python program that converts ical data into text
Deploy a web app created with Streamlit to Heroku
I made a tool that makes it a little easier to create and install a public key.
A story that makes it easy to estimate the living area using Elasticsearch and Python
Steps to create a Job that pulls a Docker image and tests it with Github Actions
I want to create a karaoke sound source by separating instruments and vocals using Python
Create a web application that recognizes numbers with a neural network
Create a plugin that always highlights arbitrary text in Sublime Text 2
Create a shogi game record management app using Django 4 ~ Create View ~
Create a game to control puzzle & dragons drops using pygame
Create a web surveillance camera with Raspberry Pi and OpenCV
Sample to put Python Flask web app on Azure App Service (Web App)
Probably the easiest way to create a pdf with Python3
I tried to get Web information using "Requests" and "lxml"
(Failure) Deploy a web app made with Flask on heroku
Convert garbled scanned images to PDF with Pillow and PyPDF