两种方法:
第一种方法:需要下载camelot.py、opencv-python、ghostscript模块。最重要的是在这个网址:https://www.ghostscript.com/download/gsdnld.html。下载Ghostscrip.exe。
import camelot.io as camelot
import cv2
import pandas as pd
import ghostscript as gs
tabels = camelot.read_pdf(r'C:\Users\Administrator\Desktop\2020城投宝典.pdf',pages='1') # 提取第一页的pdf表格,可以设置为'all',提取pdf中所有表格的数据
with pd.ExcelWriter(r'C:\Users\Administrator\Desktop\05.xlsx') as writer: # 设置存储的表格
for page in range(0,221):
data = tabels[page].df
data.to_excel(writer, sheet_name=f'sheet{page}')
第二种方法:需要下载PyPDF2、pdfplumber模块。
(1)导出为csv。所有的表格加载在一个csv文件中。
import PyPDF2
import pdfplumber
import pandas as pd
file = r'C:\Users\Administrator\Desktop\2020城投宝典.pdf' # 自己的pdf路径
with pd