banner
andrewji8

Being towards death

Heed not to the tree-rustling and leaf-lashing rain, Why not stroll along, whistle and sing under its rein. Lighter and better suited than horses are straw sandals and a bamboo staff, Who's afraid? A palm-leaf plaited cape provides enough to misty weather in life sustain. A thorny spring breeze sobers up the spirit, I feel a slight chill, The setting sun over the mountain offers greetings still. Looking back over the bleak passage survived, The return in time Shall not be affected by windswept rain or shine.
telegram
twitter
github

Python-Camelot:PDFテーブルデータを抽出するための3行のコード

PDF ファイルは非常に一般的なファイル形式であり、通常は公式の電子版ファイルに使用されます。異なるレイアウト形式を固定して、明確で美しい表示効果を作成することができます。しかし、PDF から情報を抽出したい人々にとっては、特に表に関しては悪夢です。

image

多くの学術報告書、論文、分析記事では、表データが PDF で表示されますが、表からデータを直接コピーする場合は非常に面倒です。最近、開発者が PDF からテキストを抽出し、表の情報を抽出するためのツールである Camelot を提供しました。Camelot は、ほとんどの表を Pandas のデータフレームに変換することができます。

プロジェクトのリンク:https://github.com/camelot-dev/camelot

Camelot とは何ですか

プロジェクトの説明によると、Camelot は PDF ファイルから表データを抽出するための Python ツールです。

具体的には、ユーザーは Pandas を使用して PDF ファイルを開き、このツールを使用して表データを抽出し、最後に出力形式(例:csv ファイル)を指定することができます。

コードの例

プロジェクトで提供される PDF ファイルは次の図のようになっており、ユーザーは 2-1 の表の情報を抽出する必要があるとします。

image
Camelot を使用して表データを抽出するコードは次のようになります:

>>> import camelot
>>> tables = camelot.read_pdf('foo.pdf') #PandasのCSVファイルを開くのと同じように
>>> tables[0].df # pandasのデータフレームを取得!
>>> tables.export('foo.csv', f='csv', compress=True) # json, excel, html, sqlite, 出力形式を指定できます
>>> tables[0].to_csv('foo.csv') # to_json, to_excel, to_html, to_sqlite, データをファイルにエクスポート
>>> tables
<TableList n=1>
>>> tables[0]
<Table shape=(7, 7)> # 出力形式を取得
>>> tables[0].parsing_report
{
    'accuracy': 99.02,
    'whitespace': 12.24,
    'order': 1,
    'page': 1
}

以下は出力結果であり、結合されたセルに対して、Camelot は抽出後に空行処理を行っています。これは安全な方法です。

image

インストール方法

プロジェクトの作者は 3 つのインストール方法を提供しています。まず、最も簡単な方法である Conda を使用してインストールすることができます。

conda install -c conda-forge camelot-py
最も一般的なインストール方法は、pip を使用してインストールする方法です。

pip install camelot-py[cv]
また、プロジェクトをクローンしてソースコードを使用してインストールすることもできます。

git clone https://www.github.com/camelot-dev/camelot
cd camelot
pip install ".[cv]"

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。