pdfplumber、pandas读取多个表格中分析某个表格中指定行列的值
- 使用pdfplumber打开pdf文档并获取表格数据
import pdfplumber
with pdfplumber.open("example.pdf") as pdf:
pages = pdf.pages
for page in pages:
tables = page.extract_tables()
# 获取第一个表格数据
table = tables[0]
- 使用pandas将表格数据转换为DataFrame对象
import pandas as pd
df = pd.DataFrame(table[1:], columns=table[0])
- 分析指定行列的值
# 获取第2列数据
col_data = df.iloc[:, 1]
# 获取第3行第2列的值
value = df.iloc[2, 1]
完整代码如下:
import pdfplumber
import pandas as pd
with pdfplumber.open("example.pdf") as pdf:
pages = pdf.pages
for page in pages:
tables = page.extract_tables()
# 获取第一个表格数据
table = tables[0]
# 将表格数据转换为DataFrame对象
df = pd.DataFrame(table[1:], columns=table[0])
# 分析指定行列的值
col_data = df.iloc[:, 1]
value = df.iloc[2, 1]
print(col_data)
print(value)
原文地址: https://www.cveoy.top/t/topic/MpG 著作权归作者所有。请勿转载和采集!