pdfplumber 提取 PDF 文本及表格
pdfplumber-0.5.12,适用于python 2.7、3.5、3.6. 官网链接在此:https://github.com/jsvine/pdfplumber
一、安装
pip install pdfplumber
二、使用
提取文本:pdf.page[0].extract_text() 提取表格:pdf.page[0].extract_tables()
提取文字与表格(去除空格),分别写入txt文档,代码如下:
import pdfplumber
path = path/to/file.pdf
fileNames = os.path.splitext(path)
pdf = pdfplumber.open(path)
for page in pdf.pages:
# 获取当前页面的全部文本信息,包括表格中的文字,写入txt文档
# 直接得到字符串,包括了换行符【与PDF上的换行位置一致,而不是实际的“段落”】
# print(page.extract_text())
with open(fileNames[0] + _txt.txt, a+) as m:
m.write(page.extract_text())
m.close()
# 提取本页全部表格内容,删除空格等,写入txt文档
# 得到的table是嵌套list类型
for table in page.extract_tables():
for row_old in table: # 表中每一行
row_new = []
for i in range(len(row_old)):
if row_old[i]:
row_new.append(row_old[i])
# print(str(row_new).replace(\n, ).decode(unicode-escape))
if len(row_new) != 0:
with open(fileNames[0] + _table.txt, a+) as f:
f.write(str(row_new).replace(\n, )+
)
f.close()
pdf.close()
模块相关介绍:
https://github.com/jsvine/pdfplumber#pdfplumber-v0512
pdf.pdf 类
pdf.page 类
关于方法和描述请见:https://github.com/jsvine/pdfplumber#the-pdfplumberpage-class
对象
每个实例pdfplumber.PDF和pdfplumber.Page访问四种类型的PDF对象。以下属性各自返回匹配对象的Python列表:
关于属性和描述请见:https://github.com/jsvine/pdfplumber#objects
