Skip to content

Your lightweight open-source personal agent.

fix(documents): preserve empty PPTX table columns

Liu Jiedb804ef94a71
diff --git a/nanobot/utils/document.py b/nanobot/utils/document.py
index efbbbfd7c..574c732a7 100644
--- a/nanobot/utils/document.py
+++ b/nanobot/utils/document.py
@@ -574,9 +574,8 @@ def _collect_pptx_shape_text(shape: Any, out: list[str]) -> None:
if getattr(shape, "has_table", False):
for row in shape.table.rows:
cells = [cell.text.strip() for cell in row.cells]
- line = "\t".join(cell for cell in cells if cell)
- if line:
- out.append(line)
+ if any(cells):
+ out.append("\t".join(cells))
return
text = getattr(shape, "text", "")
diff --git a/tests/test_document_parsing.py b/tests/test_document_parsing.py
index 2e5b3d10d..e0dcea33c 100644
--- a/tests/test_document_parsing.py
+++ b/tests/test_document_parsing.py
@@ -385,6 +385,40 @@ class TestExtractText:
assert "Alice" in result
assert "Bob" in result
+ @pytest.mark.parametrize(
+ ("cells", "expected"),
+ [
+ (("Alice", "", "100"), "Alice\t\t100"),
+ (("", "10", "100"), "\t10\t100"),
+ (("Alice", "10", ""), "Alice\t10\t"),
+ (("", " ", ""), None),
+ ],
+ )
+ def test_extract_text_pptx_table_preserves_empty_columns(
+ self, tmp_path: Path, cells: tuple[str, ...], expected: str | None
+ ):
+ """Blank cells keep values aligned with their table headers."""
+ from pptx import Presentation
+ from pptx.util import Inches
+
+ pptx_file = tmp_path / "empty-columns.pptx"
+ prs = Presentation()
+ slide = prs.slides.add_slide(prs.slide_layouts[6])
+ table = slide.shapes.add_table(
+ 2, 3, Inches(1), Inches(1), Inches(4), Inches(1)
+ ).table
+ for column, text in enumerate(("Name", "Q1", "Q2")):
+ table.cell(0, column).text = text
+ for column, text in enumerate(cells):
+ table.cell(1, column).text = text
+ prs.save(pptx_file)
+
+ result = extract_text(pptx_file)
+ expected_lines = ["--- Slide 1 ---", "Name\tQ1\tQ2"]
+ if expected is not None:
+ expected_lines.append(expected)
+ assert result == "\n".join(expected_lines)
+
def test_extract_text_pptx_grouped_shapes(self, tmp_path: Path):
"""Text inside grouped shapes must be extracted recursively."""
from pptx import Presentation