使用Python从PDF中提取所有表格

3

我有一份PDF文件,想要从中提取所有的表格。但是当我运行下面的代码时,得到空列表。

import pdftables

filepath = 'File_Set_-2_feasibility_Study/140u-td005_-en-p.pdf'
with open(filepath, 'rb') as fh:
    table = pdftables.get_tables(fh)
print(table)

你可能想要查看 https://github.com/camelot-dev/camelot - Martin Thoma
@Neeraj Sharma:请在 https://dev59.com/l7Pma4cB1Zd3GeqPurO2#72414309 的答案中尝试使用SLICEmyPDF。 - 123456
2个回答

2

我猜PDF文件有多页?这个应该可以解决:

from pdftables.pdf_document import PDFDocument
from pdftables.pdftables import page_to_tables

filepath = ...
page_number = ...
with open(filepath, 'rb') as file_object:
    pdf_doc = PDFDocument.from_fileobj(file_object)
    pdf_page = pdf_doc.get_page(pagenumber) 
    tables = page_to_tables(pdf_page)
    print(tables)

您也可以迭代多个页面:

for page_number, page in enumerate(pdf_doc.get_pages()):
    tables = page_to_tables(page)
    print(tables)

1
我忘了提到我正在使用Python3,在安装了pdftables.six的包中,pdftables.pdf_document导入不可用,相反,pdfminer.pdfdocument导入可用,但它没有“from_fileobj”。 - Neeraj Sharma
你是否在正确的目录下?听起来File_Set_-2_feasibility_Study/140u-td005_-en-p.pdf是一个绝对路径而非相对路径。这是文件存储的位置吗? - Michael Dorner
当我尝试使用pdfReader查看是否打印所有文本时,令人惊讶的是它打印了除表格数据外的所有内容。 - Neeraj Sharma

0

#安装以下库以使用PDF表格,这对我有效

> pip install pdftables.six

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接