天天看點

pdfplumber識别表格類格式的pdf

python -V 3.7

import pdfplumber
import pandas as pd

pd.set_option('display.max_columns',100)  # 設定同時顯示多少列

# path = r'D:/JBK/code_example/pdfplumber_pdf/1.pdf'   # 這個裡面就是表格
path = r'D:/JBK/code_example/pdfplumber_pdf/6.pdf'   # 這個裡面就是表格

# path = r'D:/JBK/code_example/untitled/pdfminer_all.pdf'
with pdfplumber.open(path) as pdf:
    # 擷取第一頁
    first_page = pdf.pages[0]
    # 解析文本
    # text = first_page.extract_text()
    # print(text)
    # 解析表格
    tables = first_page.extract_tables()

    for table in tables:
        # print(table[1:])  # [['1', 'A', '1', '1', '15.00', '40.94', '67.59', '86.38', '98.45', '99.53', '99.55', '99.49'], ['2', 'A', '2', '1', '13.36', '37.75', '60.83', '79.67', '98.70', '100.38', '100.45', '101.11'],
        # print(table[0])   # ['', 'Bath', 'Vessel', 'Injection', '5.0 min', '10.0 min', '15.0 min', '20.0 min', '30.0 min', '45.0 min', '60.0 min', '75.0 min']
        df = pd.DataFrame(table[1:], columns=table[0])
        print(df)
        # exit()