安装
使用pdfplumber第三方库直接安装
pip install pdfplumber
一些常用的方法
.extract_text() 用来提页面中的文本,将页面的所有字符对象整理为的那个字符串
.extract_words() 返回的是所有的单词及其相关信息
.extract_tables() 提取页面的表格
.to_image() 用于可视化调试时,返回PageImage类的一个实例
import pdfplumber
import pandas as pd
with pdfplumber.open("表格.pdf") as pdf:
page = pdf.pages[0] # 第一页的信息
table = page.extract_tables()
for t in table:
# 得到的table是嵌套list类型,转化成DataFrame
df = pd.DataFrame(t[1:], columns=t[0])
print(df)