Skip to content

Build and run agents you can see, understand and trust.

fix(rag): read the text inside grouped shapes in PPTParser (#2660)

Lukas1766072aa4fb
diff --git a/src/agentscope/rag/_parser/_ppt.py b/src/agentscope/rag/_parser/_ppt.py
index 8d1011874..e3bae199e 100644
--- a/src/agentscope/rag/_parser/_ppt.py
+++ b/src/agentscope/rag/_parser/_ppt.py
@@ -15,6 +15,7 @@ long text stays intact inside a section and is split downstream by a
"""
import base64
import io
+from collections.abc import Iterator
from typing import Any, Literal
from ..._logging import logger
@@ -28,6 +29,28 @@ from ._utils import (
)
+def _iter_shapes(shapes: Any) -> Iterator[Any]:
+ """Yield every shape on a slide, descending into groups.
+
+ A group carries no picture, table or text frame of its own; its
+ children do. Iterating ``slide.shapes`` alone therefore never reaches
+ what is inside one, and the text is dropped without an error.
+
+ Args:
+ shapes (`Any`):
+ A python-pptx shape collection.
+
+ Yields:
+ `Any`: Each leaf shape, in depth-first shape-tree order.
+ """
+ for shape in shapes:
+ # A group is the only shape with its own ``shapes`` collection.
+ if hasattr(shape, "shapes"):
+ yield from _iter_shapes(shape.shapes)
+ else:
+ yield shape
+
+
def _extract_table_rows(table: Any) -> list[list[str]]:
"""Read a python-pptx table into a 2-D ``list[list[str]]``.
@@ -260,7 +283,7 @@ class PPTParser(ParserBase):
if prefix:
text_buffer.append(prefix)
- for shape in slide.shapes:
+ for shape in _iter_shapes(slide.shapes):
# 1. Pictures — flush running text, emit a DataBlock section.
if self.include_image:
image_bytes = _extract_image_bytes(shape)
diff --git a/tests/rag_parser_test.py b/tests/rag_parser_test.py
index f5d8c7ae3..100cdb2b5 100644
--- a/tests/rag_parser_test.py
+++ b/tests/rag_parser_test.py
@@ -111,6 +111,85 @@ def _make_pptx_rich() -> bytes:
return buffer.getvalue()
+def _make_pptx_with_group() -> bytes:
+ """Build a PPTX whose slide holds a text box and a group of two boxes.
+
+ A group carries no text frame of its own, so a loop over ``slide.shapes``
+ that only looks at text frames, tables and pictures never reaches it.
+ """
+ from pptx import Presentation
+ from pptx.util import Inches
+
+ prs = Presentation()
+ slide = prs.slides.add_slide(prs.slide_layouts[6]) # blank
+
+ standalone = slide.shapes.add_textbox(
+ Inches(0.5),
+ Inches(0.2),
+ Inches(4),
+ Inches(0.5),
+ )
+ standalone.text_frame.text = "Standalone"
+
+ first = slide.shapes.add_textbox(
+ Inches(0.5),
+ Inches(2.0),
+ Inches(2),
+ Inches(0.5),
+ )
+ first.text_frame.text = "Grouped one"
+ second = slide.shapes.add_textbox(
+ Inches(3.0),
+ Inches(2.0),
+ Inches(2),
+ Inches(0.5),
+ )
+ second.text_frame.text = "Grouped two"
+ slide.shapes.add_group_shape([first, second])
+
+ buffer = io.BytesIO()
+ prs.save(buffer)
+ return buffer.getvalue()
+
+
+def _make_pptx_with_nested_group() -> bytes:
+ """Build a PPTX whose slide holds a group inside a group."""
+ from pptx import Presentation
+ from pptx.util import Inches
+
+ prs = Presentation()
+ slide = prs.slides.add_slide(prs.slide_layouts[6])
+
+ inner_a = slide.shapes.add_textbox(
+ Inches(0.5),
+ Inches(0.5),
+ Inches(2),
+ Inches(0.5),
+ )
+ inner_a.text_frame.text = "Innermost"
+ inner_b = slide.shapes.add_textbox(
+ Inches(3.0),
+ Inches(0.5),
+ Inches(2),
+ Inches(0.5),
+ )
+ inner_b.text_frame.text = "Sibling"
+ inner_group = slide.shapes.add_group_shape([inner_a, inner_b])
+
+ outer = slide.shapes.add_textbox(
+ Inches(0.5),
+ Inches(2.0),
+ Inches(2),
+ Inches(0.5),
+ )
+ outer.text_frame.text = "Outer"
+ slide.shapes.add_group_shape([inner_group, outer])
+
+ buffer = io.BytesIO()
+ prs.save(buffer)
+ return buffer.getvalue()
+
+
def _make_pptx_with_special_table_cells() -> bytes:
"""Build a PPTX table with pipes and a multi-line cell."""
from pptx import Presentation
@@ -877,6 +956,68 @@ class PPTParserTest(IsolatedAsyncioTestCase):
with self.assertRaises(FileNotFoundError):
await parser.parse("/no/such/file.pptx", "x.pptx")
+ async def test_group_shape_text_is_read(self) -> None:
+ """Text inside a group reaches the Sections, in shape-tree order."""
+ parser = PPTParser(include_image=False)
+ sections = await parser.parse(_make_pptx_with_group(), "demo.pptx")
+
+ # Only "Standalone" was read before; the group held the other two.
+ self.assertEqual(
+ [s.model_dump() for s in sections],
+ [
+ {
+ "content": {
+ "type": "text",
+ "text": (
+ "<slide index=1>\n"
+ "Standalone\n"
+ "Grouped one\n"
+ "Grouped two\n"
+ "</slide>"
+ ),
+ "id": AnyString(),
+ "created_at": AnyString(),
+ "finished_at": None,
+ },
+ "source": "demo.pptx",
+ "metadata": {"slide": 1},
+ },
+ ],
+ )
+
+ async def test_nested_group_shape_text_is_read(self) -> None:
+ """A group can hold a group, so the descent has to recurse."""
+ parser = PPTParser(include_image=False)
+ sections = await parser.parse(
+ _make_pptx_with_nested_group(),
+ "demo.pptx",
+ )
+
+ # Depth first: the inner group is emptied before its sibling, and
+ # both come before the shape that follows the outer group.
+ self.assertEqual(
+ [s.model_dump() for s in sections],
+ [
+ {
+ "content": {
+ "type": "text",
+ "text": (
+ "<slide index=1>\n"
+ "Innermost\n"
+ "Sibling\n"
+ "Outer\n"
+ "</slide>"
+ ),
+ "id": AnyString(),
+ "created_at": AnyString(),
+ "finished_at": None,
+ },
+ "source": "demo.pptx",
+ "metadata": {"slide": 1},
+ },
+ ],
+ )
+
class ExcelParserTest(IsolatedAsyncioTestCase):
"""Behavioural coverage for :class:`ExcelParser`."""