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文件