Fix Page.extract_table(...) when no table found

Return None instead of crashing.

Fixes https://github.com/jsvine/pdfplumber/issues/216
This commit is contained in:
Jeremy Singer-Vine
2020-05-27 22:23:22 -04:00
parent 81104301cb
commit d64afa8c2a
2 changed files with 14 additions and 1 deletions
+4
View File
@@ -174,6 +174,10 @@ class Page(Container):
def extract_table(self, table_settings={}):
tables = self.find_tables(table_settings)
if len(tables) == 0:
return None
# Return the largest table, as measured by number of cells.
sorter = lambda x: (-len(x.cells), x.bbox[1], x.bbox[0])
largest = list(sorted(tables, key=sorter))[0]
+10 -1
View File
@@ -148,8 +148,17 @@ class Test(unittest.TestCase):
cropped_page = page.crop((0, 0, page.width, 122))
assert len(cropped_page.extract_table()) == 5
def test_issue_203(self):
path = os.path.join(HERE, "pdfs/issue-203-decimalize.pdf")
with pdfplumber.open(path) as pdf:
assert len(pdf.objects)
def test_issue_216(self):
"""
.extract_table() should return None if there's no table,
instead of crashing
"""
path = os.path.join(HERE, "pdfs/issue-140-example.pdf")
with pdfplumber.open(path) as pdf:
cropped = pdf.pages[0].crop((0, 0, 1, 1))
assert cropped.extract_table() is None