mirror of
https://github.com/jsvine/pdfplumber.git
synced 2026-08-29 08:34:23 +08:00
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:
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user