操作实现
要使用camelot-py从PDF文件中提取表格,首先需要保证在你的项目中安装了camelot-py库。如果没有按照可以按照如下的命令进行安装。
pip install camelot-py[cv]
安装完成之后就可以使用camelot-py来进行表格的提取了。如下所示。
import camelot
import pandas as pd
# 提取PDF中的表格
tables = camelot.read_pdf('your_pdf_file.pdf')
# 将每个表格保存到Excel文件中
for idx, table in enumerate(tables):
df = table.df
# 指定保存的Excel文件名,可以根据需要更改
excel_file = f'table_{idx + 1}.xlsx'
df.to_excel(excel_file, index=False)
这段代码将提取的每个表格保存到以"table_1.xlsx"、"table_2.xlsx"等命名的Excel文件中。首先提取到PDF中的表格,然后通过Python的pandas库将表格数据转换为DataFrame对象,使用DataFrame的to_excel()方法将数据保存为Excel文件。
当然,你也可以根据需求来调整保存文件的路径以及名称,如下,如果你希望将所有表格保存到同一个Excel文件中的不同工作表中,可以通过如下的方式来实现。
import camelot
import pandas as pd
# 提取PDF中的表格
tables = camelot.read_pdf('your_pdf_file.pdf')
# 创建一个Excel写入对象
excel_writer = pd.ExcelWriter('output.xlsx')
# 将每个表格保存到Excel文件的不同工作表中
for idx, table in enumerate(tables):
df = table.df
# 指定工作表的名称,可以根据需要更改
sheet_name = f'Table_{idx + 1}'
df.to_excel(excel_writer, sheet_name=sheet_name, index=False)
# 保存Excel文件
excel_writer.save()
上面这段代码就实现了将所有提取到的表格都保存到一个Excel文件的不同Sheet页中,这样我们就可以在一个Excel文件中去浏览全部的数据了。
解决问题
运行代码之后会出现如下的报错信息
OSError: Ghostscript is not installed. You can install it using the instructions here:
https://camelot-py.readthedocs.io/en/master/user/install-deps.html
这个错误表明你的系统缺少Ghostscript,而Camelot依赖于Ghostscript来处理PDF文件。所以需要在系统中安装Ghostscript来解决这个问题。
手动安装Ghostscript
访问Ghostscript的官方网站(https://www.ghostscript.com/download/gsdnld.html)下载适用于你操作系统的安装程序。
安装Ghostscript,并确保在安装过程中将其添加到系统的PATH环境变量中
使用包管理器安装Ghostscript
如果你使用的是Linux系统,可以使用包管理器安装Ghostscript。例如,在Ubuntu上,你可以运行以下命令安装Ghostscript。
sudo apt-get install ghostscript
如果你使用的是macOS,你可以使用Homebrew安装Ghostscript:
brew install ghostscript
如果你使用的是Windows系统,你可以使用Chocolatey来安装Ghostscript。首先确保已安装Chocolatey,然后运行以下命令:
choco install ghostscript
这里我们不具体介绍choco的安装,有兴趣的读者可以自己查找相关资料进行安装。
运行程序并调整
安装完成之后,我们继续运行上面的代码,会发现既没有输出也没有报错,这可能是因为我们的文件格式、提取参数、Camelot出现了问题,我们可以一一的进行排查。排查完成之后运行程序会会出现一个如下的报错信息。
excel_writer.save()
AttributeError: 'XlsxWriter' object has no attribute 'save'
所以,这里我们需要将将 excel_writer.save() 替换为 excel_writer.close(),代码如下所示。
import camelot
import pandas as pd
# 提取PDF中的表格
tables = camelot.read_pdf('your_pdf_file.pdf')
# 创建一个Excel写入对象
excel_writer = pd.ExcelWriter('output.xlsx')
# 将每个表格保存到Excel文件的不同工作表中
for idx, table in enumerate(tables):
df = table.df
# 指定工作表的名称,可以根据需要更改
sheet_name = f'Table_{idx + 1}'
df.to_excel(excel_writer, sheet_name=sheet_name, index=False)
# 保存并关闭Excel文件
excel_writer.close()
会发现还是没有输出结果,这里我们就需要注意识别参数问题了。如下所示。
tables = camelot.read_pdf('your_pdf_file.pdf', flavor='stream', pages='1-end', split_text=False)
使用 lattice 模式:该模式适用于边界明确的表格,在提取过程中会使用 PDF 文件中的线条信息来识别表格,例如有线条或框框的表格。
使用 stream 模式:该模式适用于没有明显边界的表格,使用文本对齐和空白来识别表格,例如只有文本对齐的表格。
最终代码
import camelot
import pandas as pd
# 提取PDF中的表格
tables = camelot.read_pdf('2020.pdf', flavor='stream', pages='1-end', split_text=False)
# 输出提取的表格数量
print("Total tables extracted:", len(tables))
# 输出每个表格的内容
for idx, table in enumerate(tables):
print("Table", idx + 1)
print(table.df)
# 创建一个Excel写入对象
excel_writer = pd.ExcelWriter('2020.xlsx')
# 将每个表格保存到Excel文件的不同工作表中
for idx, table in enumerate(tables):
df = table.df
if df.shape[1]>1:
# 指定工作表的名称,可以根据需要更改
sheet_name = f'Table_{idx + 1}'
df.to_excel(excel_writer, sheet_name=sheet_name, index=False)
# 保存并关闭Excel文件
excel_writer.close()
最终调整完成的代码结构就是上面这个了,上面这个代码就可以完成对于PDF中无边框的表格提取。但是由于是代码提取,所以在很多表格缩进不清楚的地方,可能会出现表格串行的问题。在使用的时候需要注意检查生成的表格是否存在问题。
本文暂时没有评论,来添加一个吧(●'◡'●)