我們經(jīng)常遇到一些發(fā)布的pdf文件,需要獲取其中表格中的數(shù)據(jù),比如如下的表格:
提取數(shù)據(jù)有多種方法,我們采用最簡單的python來實現(xiàn)。- 建立python項目,建立文件readpdf.py如下
import
tabula
# 檢查本地的java環(huán)境是否正確
tabula.environment_info()
#jpype.startJVM(jpype.getDefaultJVMPath())
# 從PDF文件中讀取表格數(shù)據(jù)到DataFrame列表
dfs = tabula.read_pdf("D:\\ai-hos\\doc\\my.pdf", pages='all',
multiple_tables=True,force_subprocess=True)
for i, df in enumerate(dfs):
# 將每個DataFrame保存為CSV文件
df.to_csv(f"table_{i}.csv", index=False)
pip install tabula-py
pip install jpype1 --no-cache-dir
java -version
這樣運行程序就可以解析pdf文件中的數(shù)據(jù)了。?
Python提取PDF表格數(shù)據(jù)還可以使用以下幾種方法:
1. 使用 pdfplumber 庫
安裝:pip install pdfplumber
示例代碼:
python
import pdfplumber
import pandas as pd
def extract_pdf_tables(pdf_path, start_page, end_page):
with pdfplumber.open(pdf_path) as pdf:
all_dfs = []
for page in pdf.pages[start_page-1:end_page]:
tables = page.extract_tables()
for table in tables:
if table:
df = pd.DataFrame(table[1:], columns=table[0])
all_dfs.append(df)
combined_df = pd.concat(all_dfs, ignore_index=True)
return combined_df
# 使用示例
pdf_path = "your_file.pdf"
start_page = 1 # 開始頁碼
end_page = 10 # 結(jié)束頁碼
data = extract_pdf_tables(pdf_path, start_page, end_page)
data.to_excel("output.xlsx", index=False)
2. 使用 camelot 庫
安裝:pip install camelot-py[cv]
示例代碼:
python
import camelot
def extract_pdf_tables(pdf_path):
tables = camelot.read_pdf(pdf_path)
combined_df = pd.concat([table.df for table in tables], ignore_index=True)
return combined_df
# 使用示例
pdf_path = "your_file.pdf"
data = extract_pdf_tables(pdf_path)
data.to_csv("output.csv", index=False)?
3. 使用 tabula-py 庫
安裝:pip install tabula-py jpype1
示例代碼:
python
import tabula
def extract_pdf_tables(pdf_path):
dfs = tabula.read_pdf(pdf_path, pages='all', multiple_tables=True)
combined_df = pd.concat(dfs, ignore_index=True)
return combined_df
# 使用示例
pdf_path = "your_file.pdf"
data = extract_pdf_tables(pdf_path)
data.to_excel("output.xlsx", index=False)
注意事項
文件路徑:確保PDF文件路徑正確,可使用絕對路徑或相對路徑。
表格格式:不同庫對表格格式的兼容性不同,若提取結(jié)果不理想,可嘗試更換庫或調(diào)整參數(shù)。
性能優(yōu)化:對于大型PDF文件,可分頁處理或使用多線程提高效率。
該文章在 2025/8/28 16:17:25 編輯過