A Python Code that gets value and the others from pdf table published on WEB site(WEB site上に公開されているpdfの表から数値などを読み取るPythonCode)

 Web上で公表されている比較的複雑な表(including relatively complex table)を読み込んで(reading)、CSV StyleのFileに変換するCodeをCreateしました。

 As material of PDF table, I selected 日本取引所グループ(JPX Group)がEveryWeekにPublishingする信用取引残高等(Margin Trading Balances, etc.)のPageにある銘柄別信用取引週末残高(Weekend Margin Trading Balances by Stock)のPDFにしました。

https://www.jpx.co.jp/markets/statistics-equities/margin/05.html

 The reason I chose this PDF, is because I want to be going to predict some stock price future,  and I want to use this data for machine learning prediction and analysis.

 Of course, I ask Gemini when I create this code.

 Since It was difficult to read the JPX's PDF , I threw a lot of questions to Gemini. Especially it is very difficult for me and Gemini to identify Japanese Minus symbol (vertically reversed triangle) and value separator(comma symbol',') etc.  

 I implement varius function in the code, examply like below.

> At first, the code basicly read the values on the table of Weekend Margin Trading Balances by Stock of JPX's PDF(In this step, I don't expect accurate reading, but there is no error to read)

> I think that I download target PDF files to my local PC manually, the code that I write, read target PDF files from my local PC.

> the target PDF is published every Tuesday, the code automatically judge and search the new PDF file.  If there is some new PDF file, the code read data from new PDF file and additionally write existing CSV file. If there is no new PDF file, the code don't act anything.

 I think that I explain the code, I create from now. 

But It is unclear whether the target PDF file has been read correctly (not all data has been checked). Use of this code and the data read by this code is at your own risk.(正確に対象PDFファイルを読み込めているかどうかは不明です(すべてのデータを確認できていません)。このコードの利用やこのコードで読み込んだデータの利用については自己責任でお願いします。)

import pdfplumber
import pandas as pd
import os, glob


Gemini's explanation about above code is like below. It's important to use a library of pdfplumber.

ご提示いただいたコードは、Pythonで複数のPDFファイルからデータを抽出・集計する自動化処理などを行う際によく使われるライブラリ群の読み込み(インポート)文です。それぞれの役割は以下の通りです。

  • import pdfplumber: PDFファイルからテキストや表(テーブル)などの情報を抽出するための外部ライブラリです。他のPDF操作ライブラリと比較して、特にPDF内の「表データ」を高精度で抽出することに長けています。

  • import pandas as pd: データ解析や操作を行うための強力な外部ライブラリ「Pandas」をインポートし、コード内で短くpdとして呼び出せるようにする慣例的な書き方です。pdfplumberで抽出した表データを、表計算ソフトのように扱いやすいデータフレーム(DataFrame)に変換・加工し、最終的にCSVやExcelファイルとして保存するためによく併用されます。

  • import os, glob: どちらもファイルやディレクトリ(フォルダ)の操作・検索を行うPythonの標準ライブラリです。osはフォルダの作成やパスの結合などOSに関連する機能を提供し、globは「特定のフォルダ内にある拡張子が.pdfのファイルをすべて取得する」といったパターンマッチングによるファイル検索に使用します。

これらの組み合わせから、このコードに続く処理としては「特定のフォルダ内にある複数のPDFファイルをglobosを使って一括で検索・取得し、それらをpdfplumberで順番に読み込んで表データを抽出し、最後にpandasを使って1つのデータに結合・集計する」といったプログラムが想定されます。

 By the way, I ask Gemini what libralise does Python have for reading PDF file, Answer is like bleow.

PythonでPDFファイルを読み込むためのライブラリは、用途(テキスト抽出、表の取得、編集など)によって最適なものが異なります。代表的なものをいくつか紹介します。


1. PyPDF (旧 PyPDF2)

最も基本的で広く使われているライブラリです。

  • 特徴: テキストの抽出、ページの回転、分割、結合、パスワード保護の解除などが可能です。

  • メリット: 外部依存関係が少なく、インストールが簡単です。

  • デメリット: 複雑なレイアウトのテキスト抽出や、画像ベースのPDFには不向きです。

2. pdfplumber

テキスト抽出の精度が高く、特に「表(テーブル)」のデータ取得に非常に強いライブラリです。

  • 特徴: 各文字や線の座標情報を細かく取得できるため、視覚的な構造を維持したまま読み込めます。

  • メリット: 表データの抽出機能が非常に優秀です。

  • デメリット: 動作がPyPDFに比べると少し重めです。

3. PyMuPDF (fitz)

C++で書かれた高速なPDFエンジン「MuPDF」のPythonバインディングです。

  • 特徴: 読み込み速度が非常に速く、PDFから画像への変換や注釈の操作も得意です。

  • メリット: 高速かつ多機能。画像やフォント情報の抽出も強力です。

  • デメリット: ライセンス(AGPL)に注意が必要な場合があります。

4. tabula-py

PDF内の「表」を pandas の DataFrame として読み込むことに特化したライブラリです。

  • 特徴: Javaの tabula-java をPythonから利用できるようにしたものです。

  • メリット: 複雑な表を数行のコードでデータ分析可能な形式に変換できます。

  • デメリット: 実行環境に Java (JRE) のインストールが必須です。


ライブラリ選定の目安

目的おすすめライブラリ
単純なテキスト抽出・結合PyPDF
精度の高いテキスト・表抽出pdfplumber
高速処理・画像変換・編集PyMuPDF
表データをDataFrameに変換tabula-py


コメント

このブログの人気の投稿

Gourmet from June 2025 to September 2025

Food and Drink from April 2024

DINNER, LUNCH AND DRINK from Sept. 2024