0
0

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 1 year has passed since last update.

あaaa

0
Last updated at Posted at 2025-05-22

import streamlit as st
import pdfplumber
import pandas as pd

def extract_tables_with_titles(pdf):
results = []

for page_num, page in enumerate(pdf.pages):
    text = page.extract_text()
    tables = page.extract_tables()

    for i, table in enumerate(tables):
        # テーブルをDataFrame化(ただし今回は中身表示しない)
        if len(table) < 2:
            continue  # ヘッダーと1行以上必要

        headers = table[0]
        table_df = pd.DataFrame(table[1:], columns=headers)

        # ページ内のテキストを行ごとに分割
        lines = text.split('\n') if text else []
        table_title = f"Table_{page_num+1}_{i+1}"
        description = "No description found."

        # タイトルと説明候補を前後の行から推定
        for j, line in enumerate(lines):
            if all(h in line for h in headers[:1]):  # ヘッダーの一部が含まれる行を検出
                if j > 0:
                    table_title = lines[j-1].strip()
                if j+1 < len(lines):
                    description = lines[j+1].strip()
                break

        results.append({
            "table_title": table_title,
            "description": description
        })

return results

def main():
st.title("PDF内の表と説明抽出アプリ(PDFPlumber使用)")

uploaded_file = st.file_uploader("PDFファイルをアップロード", type=["pdf"])

if uploaded_file:
    with pdfplumber.open(uploaded_file) as pdf:
        extracted_info = extract_tables_with_titles(pdf)

    if extracted_info:
        df = pd.DataFrame(extracted_info)
        st.subheader("抽出されたテーブル情報")
        st.dataframe(df)
    else:
        st.warning("表が見つかりませんでした。")

if name == "main":
main()

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?