如何使用python从pdf文件中提取表名以及使用camelot的表?
我试图使用python中的camelot从pdf文件中提取表和表名。虽然我知道如何使用camelot提取表(这非常简单),但我还是很难找到任何关于如何提取表名的帮助。其目的是提取此信息并显示表及其名称的可视化视图,供用户从列表中选择相关表 我尝试过提取表格,然后从PDF中提取文本。我在这两方面都很成功,但在将表名连接到表时却不成功如何使用python从pdf文件中提取表名以及使用camelot的表?,python,python-3.x,python-camelot,Python,Python 3.x,Python Camelot,我试图使用python中的camelot从pdf文件中提取表和表名。虽然我知道如何使用camelot提取表(这非常简单),但我还是很难找到任何关于如何提取表名的帮助。其目的是提取此信息并显示表及其名称的可视化视图,供用户从列表中选择相关表 我尝试过提取表格,然后从PDF中提取文本。我在这两方面都很成功,但在将表名连接到表时却不成功 def tables_from_pdfs(filespath): pdffiles = glob.glob(os.path.join(filespath, "
def tables_from_pdfs(filespath):
pdffiles = glob.glob(os.path.join(filespath, "*.pdf"))
print(pdffiles)
dictionary = {}
keys = []
for file in pdffiles:
print(file)
n = PyPDF2.PdfFileReader(open(file, 'rb')).getNumPages()
print(n)
tables_dict = {}
for i in range(n):
tables = camelot.read_pdf(file, pages = str(i))
tables_dict[i] = tables
head, tail = os.path.split(file)
tail = tail.replace(".pdf", "")
keys.append(tail)
dictionary[tail] = tables_dict
return dictionary, keys
预期结果是一个表以及pdf文件中所述的表名。例如:
pdf第x页的表格名称:表1。废话废话
“'Table'”表格与此处的camelot API中的TableList和Table函数一起列出:
从显示以下内容的网页开始:
下层阶级
Camelot没有表名的引用,只有单元格数据描述。 它确实使用了python的panda数据库API,但其中可能包含表名
结合使用Camelot和Pandas来获得表名
答案的附加更新
从
您发布的代码并不表示您试图获取表名的任何内容。不要给你想要的东西。我建议使用pdfminer或PyPDF2读取带有位置绑定的PDF对象并提取表名。请阅读以下内容:没有通用解决方案。这是否回答了您的问题?我们正在搜索的名称不属于表,因此它不是数据帧的一部分。我认为你的回答不能解决问题。嗨,乔,谢谢你的回答。我查阅了文档,仍然找不到答案。我对文本相关软件包(主要是camelot)比较陌生。你能不能再多给我一些指导,让我看看可以使用的功能?谢谢,VijayYes,完成了。小心,您必须将'name'属性添加到df中,但是使用它的某些场景将丢失该数据。谢谢Joe。我认为代码是分配名称,而不是从pdf中提取名称。Anakin87建议该名称不属于该表,因此我们提取的内容将不包含该名称。我正试图以作者编写的方式从pdf文件中获取表名:)
import camelot
tables = camelot.read_pdf('foo.pdf')
tables
<TableList n=1>
tables.export('foo.csv', f='csv', compress=True) # json, excel, html
tables[0]
<Table shape=(7, 7)>
tables[0].parsing_report
{
'accuracy': 99.02,
'whitespace': 12.24,
'order': 1,
'page': 1
}
tables[0].to_csv('foo.csv') # to_json, to_excel, to_html
df_table = tables[0].df # get a pandas DataFrame!
#add
df_table.name = 'name here'
#from https://stackoverflow.com/questions/31727333/get-the-name-of-a-pandas-dataframe
import pandas as pd
df = pd.DataFrame( data=np.ones([4,4]) )
df.name = 'Ones'
print df.name
Getting values
>>> df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
... index=['cobra', 'viper', 'sidewinder'],
... columns=['max_speed', 'shield'])
>>> df
max_speed shield
cobra 1 2
viper 4 5
sidewinder 7 8
Single label. Note this returns the row as a Series.
>>> df.loc['viper']
max_speed 4
shield 5
Name: viper, dtype: int64