pip install pdfplumber
pip install openpyxl
path = r"C:\Users\lenovo\Desktop\论文和面试\以客户为中心.pdf"
pdf_mt = pdfplumber.open(path)
pdf_mt
# 获取到数据所在页 list --> [第一页的对象,第二页的对象,...第n页的对象]
all_pages = pdf_mt.pages
all_pages
for pdf_pg in all_pages[0:40]:
print(pdf_pg.extract_text())
for pdf_pg in all_pages[0:40]:
print(pdf_pg.extract_tables())
# 创建workbook对象
wb = Workbook()
# 激活工作表
ws = wb.active
for pdf_pg in need_pages:
# print(pdf_pg)
# 获取每页的文本内容
# print(pdf_pg.extract_text())
# 获取表格内容 表格:二维 [[],[]]
# print(pdf_pg.extract_tables())
# 表格有行有列的二维数据,获取二维的列表
for pdf_tb in pdf_pg.extract_tables():
# print(pdf_tb)
# 将数据一行一行的写入工作表
for row in pdf_tb:
ws.append(row)
wb.save("demo3.xlsx")