diff --git a/src/table.py b/src/table.py index e86e174c5..3e2f5cc97 100644 --- a/src/table.py +++ b/src/table.py @@ -2359,6 +2359,9 @@ def get_edges(self) -> list: return filter_edges(edges, min_length=settings.edge_min_length) + def __bool__(self): + return bool(self.tables) + def __getitem__(self, i): tcount = len(self.tables) if i >= tcount: @@ -2367,6 +2370,12 @@ def __getitem__(self, i): i += tcount return self.tables[i] + def __iter__(self): + return iter(self.tables) + + def __len__(self): + return len(self.tables) + """ Start of PyMuPDF interface code. diff --git a/tests/test_tables.py b/tests/test_tables.py index df2344b15..063e80cc1 100644 --- a/tests/test_tables.py +++ b/tests/test_tables.py @@ -750,6 +750,18 @@ def test_battery_file(): assert len(tabs.tables) == 0 +def test_table_finder_len_bool_iter(): + """TableFinder supports len(), truth testing and iteration over its tables.""" + for name, count in (("test_3179.pdf", 3), ("battery-file-22.pdf", 0)): + doc = pymupdf.open(os.path.join(scriptdir, "resources", name)) + tabs = doc[0].find_tables() + assert len(tabs) == count + assert bool(tabs) is (count > 0) + items = list(tabs) + assert len(items) == count + assert all(a is b for a, b in zip(items, tabs.tables)) + + def test_markdown(): """Confirm correct markdown output.""" filename = os.path.join(scriptdir, "resources", "strict-yes-no.pdf")