python中pdf文件解析包pdfplumber的簡單使用


pdfplumber不僅可以解析提取pdf文件中的文本,還可以提取表格

一、安裝

pip3 install pdfplumber

二、使用

# coding:utf-8

import pdfplumber

with pdfplumber.open('./test.pdf') as pdf:
    # 遍歷每個頁面
    for page in pdf.pages:
        # 獲取當前頁面的全部文本信息,包括表格中的文字,沒有內容則打印None
        print(page.extract_text())
        # 提取當前頁面中的所有表格
        print(page.extract_tables())   #沒有表格,則返回[],有表格則返回[[[row1],[row2]...],[[row1],[row2]...]...]
        # 遍歷提取到的每個表
        for table in page.extract_tables():
            print(table) # [[row1],[row2]...]
            # 遍歷每一行數據
            for row in table:
                print(row) # ['xxx','xxx'...]
        # 每一頁打印一條分割線
        print('---------- 分割線 ----------')

# test.pdf是需要解析的pdf文件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM