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/3] 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/3] 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") From fa913441cc3bb72cde8836b543807da62b7635e3 Mon Sep 17 00:00:00 2001 From: "youchang.kim" Date: Mon, 12 Oct 2026 17:21:16 +0900 Subject: [PATCH 3/3] Document TableFinder __len__, __bool__ and __iter__ and add a changelog entry --- changes.txt | 1 + docs/page.rst | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/changes.txt b/changes.txt index 76e79047f..c51d835ac 100644 --- a/changes.txt +++ b/changes.txt @@ -34,6 +34,7 @@ Other: * Retrospectively marked #4918 fixed in 1.28.0. * Retrospectively marked #4158 fixed in 1.27.2.2 * Warning for use of deprecated `fitz` API now uses `warnings.warn()` with `FutureWarning`. +* Added `__len__`, `__bool__` and `__iter__` to `TableFinder`. `len(tabs)`, `bool(tabs)` and `for t in tabs` now work directly. Note that a `TableFinder` with no tables is now falsy. **Changes in version 1.28.2** (2026-08-06) diff --git a/docs/page.rst b/docs/page.rst index 0bc029f44..ba0f4408c 100644 --- a/docs/page.rst +++ b/docs/page.rst @@ -2428,6 +2428,18 @@ The `TableFinder` class is returned by :meth:`Page.find_tables` and has related :type: :ref:`Page` + .. method:: __len__() + + The number of tables found. Same as ``len(tables)``. + + .. method:: __bool__() + + *True* if at least one table was found. This means a `TableFinder` without tables is falsy, so ``if page.find_tables():`` skips pages without tables. + + .. method:: __iter__() + + Iterate over the :class:`Table` objects, same as iterating over :attr:`tables`. + .. class:: Table