From 084921d68b52ce4ae2526c4038fd85c1f7d95186 Mon Sep 17 00:00:00 2001 From: felipdsa21 <93343201+felipdsa21@users.noreply.github.com> Date: Wed, 26 Aug 2026 18:10:50 -0300 Subject: [PATCH 1/2] src/table.py: add `__bool__`, `__iter__` and `__len__` methods to `TableFinder` --- src/table.py | 9 +++++++++ 1 file changed, 9 insertions(+) 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. From 30bd9da33053cb989495f91f27c96bafa04f2bf1 Mon Sep 17 00:00:00 2001 From: felipdsa21 <93343201+felipdsa21@users.noreply.github.com> Date: Fri, 2 Oct 2026 15:33:32 -0300 Subject: [PATCH 2/2] tests/test_tables.py: add test for new `TableFinder` methods. --- tests/test_tables.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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")