1.首先安装pdfplumber库:
pip install pdfplumber
2.如果安装失败,首先应该升级pip,用低版本的pip可能导致pdfplumber安装不成功:
python -m pip install --upgrade pip
# 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'...]
3. 如果不用with方法,则首先要打开一个pdf:
pdf = pdfpl