Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FlaskとPyPDF2を使ってPDFをテキストへ変換するWebアプリを作成する

Last updated at Posted at 2017-06-05

オライリー本の「退屈なことはPythonにやらせよう」が届いたので
Flaskを使って、ローカルからアップロードしたPDFファイルをテキストへ変換するアプリケーションをザックリ書きます。

pip install PyPDF2

準備はこれくらいです。

実態

別段変わったことはしてないです。

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="送信">
</fomr>
<div>
  {% if result %}
  {% for i in result %}
    {{ i }}
  {% endfor %}
  {% endif %}
</div>
{% endblock %}

動かしてみる

今回はPagesで作成したテンプレートを使ってみます。

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

こんな感じのPDFファイルをローカルに test.pdf として書き出します。

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

アプリケーション画面は簡素です。とりあえずアップロードしてみます。

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

色々やらないといけないことがありますね・・・。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?