Python を使用して PDF 内のテーブルからデータを抽出する

プログラムで PDF から表を抽出

使用 テーブルアブソーバー aの各ページにあるテーブルを検出します 文書。ページにアクセスした後、繰り返し処理する table_list次に、各行とセルを確認して、テーブルの内容を読みやすいテキスト形式で再構成します。

  1. PDF をとして開く Document.
  2. 内のページを繰り返し処理する document.pages.
  3. を作成 TableAbsorber 各ページと呼び出しについて visit(page).
  4. 検出されたテーブル、行、およびセルをループ処理します。
  5. 各セルからテキストフラグメントを読み取り、抽出された行出力をアセンブルします。
import aspose.pdf as apdf
from os import path

path_infile = path.join(self.dataDir, infile)

# Open PDF document
document = apdf.Document(path_infile)

# Iterate through each page in the document
for page in document.pages:
    absorber = apdf.text.TableAbsorber()
    absorber.visit(page)

    for table in absorber.table_list:
        print("Table")
        for row in table.row_list:
            row_text = []
            for cell in row.cell_list:
                cell_text = []
                for fragment in cell.text_fragments:
                    cell_text.append("".join(seg.text for seg in fragment.segments))
                row_text.append("|".join(cell_text))
            print("|".join(row_text))

PDF ページの特定の領域にテーブルを抽出

マークされた領域内にあるテーブルのみを抽出する必要がある場合は、組み合わせてください テーブルアブソーバースクエア・アノテーション。この例では、注釈長方形が境界線として使用され、その領域内に完全に含まれているテーブルのみが処理されます。

  1. PDF をとして開く Document.
  2. ターゲットページを選択します。
  3. 関心領域を示す四角形の注釈を探します。
  4. を作成 TableAbsorber そしてページにアクセスしてください。
  5. 検出された各テーブル長方形を注釈長方形と比較します。
  6. マークされた領域に完全に収まるテーブルのみを処理します。
import aspose.pdf as apdf
from os import path

# The path to the documents directory
path_infile = path.join(self.dataDir, infile)

# Open PDF document
document = apdf.Document(path_infile)

# Get the first page (index starts from 1 in Aspose.PDF)
page = document.pages[1]

# Find the first square annotation
square_annotation = next(
    (
        ann
        for ann in page.annotations
        if ann.annotation_type == apdf.annotations.AnnotationType.SQUARE
    ),
    None,
)

if square_annotation is None:
    print("No square annotation found.")
    return

# Initialize the TableAbsorber
absorber = apdf.text.TableAbsorber()
absorber.visit(page)

# Iterate through tables on the page
for table in absorber.table_list:
    table_rect = table.rectangle
    annotation_rect = square_annotation.rect

    # Check if the table is inside the annotation region
    is_in_region = (
        annotation_rect.llx < table_rect.llx
        and annotation_rect.lly < table_rect.lly
        and annotation_rect.urx > table_rect.urx
        and annotation_rect.ury > table_rect.ury
    )

    if is_in_region:
        for row in table.row_list:
            row_text = []
            for cell in row.cell_list:
                cell_text = []
                for fragment in cell.text_fragments:
                    cell_text.append("".join(seg.text for seg in fragment.segments))
                row_text.append("|".join(cell_text))
            print("|".join(row_text))

テーブルデータを PDF から CSV にエクスポート

抽出したデータをスプレッドシートに適した形式で保存する必要がある場合は、次の方法でPDFを保存します。 Excel 保存オプション 出力形式を CSV に設定します。生成されたファイルは Excel や Google スプレッドシートで開くことも、アナリティクスのワークフローにインポートすることもできます。

  1. ソース PDF をとして開きます 文書.
  2. を作成 ExcelSaveOptions インスタンス。
  3. セット excel_save.formatExcelSaveOptions.ExcelFormat.CSV.
  4. 文書をターゲット CSV パスに保存します。
import aspose.pdf as apdf
from os import path

path_infile = path.join(self.dataDir, infile)
path_outfile = path.join(self.dataDir, outfile)

document = apdf.Document(path_infile)
excel_save = apdf.ExcelSaveOptions()
excel_save.format = apdf.ExcelSaveOptions.ExcelFormat.CSV
document.save(path_outfile, excel_save)