Skip to content

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

fix(rag): normalise powerpoint soft line breaks in text frames (#2940)

ANIRUDDHA ADAK86d8cd089586
diff --git a/src/agentscope/rag/_parser/_ppt.py b/src/agentscope/rag/_parser/_ppt.py
index 4ad37b5f6..c37802b55 100644
--- a/src/agentscope/rag/_parser/_ppt.py
+++ b/src/agentscope/rag/_parser/_ppt.py
@@ -51,6 +51,28 @@ def _iter_shapes(shapes: Any) -> Iterator[Any]:
yield shape
+def _normalise_breaks(text: str) -> str:
+ """Normalise PowerPoint line-break characters to ``\n``.
+
+ ``python-pptx`` renders an ``<a:br/>`` element - what the
+ PowerPoint UI produces for Shift+Enter - as a vertical tab, and
+ ``paragraph.text`` / ``cell.text`` hand that character straight
+ through. Left in place it both embeds a control character in the
+ extracted text and glues the two lines together, so they are
+ embedded as one token run.
+
+ Args:
+ text (`str`):
+ The raw text of a paragraph or a table cell.
+
+ Returns:
+ `str`:
+ The text with ``\r\n``, ``\r`` and ``\v``
+ replaced by ``\n``.
+ """
+ return text.replace("\r\n", "\n").replace("\r", "\n").replace("\v", "\n")
+
+
def _extract_table_rows(table: Any) -> list[list[str]]:
"""Read a python-pptx table into a 2-D ``list[list[str]]``.
@@ -67,13 +89,7 @@ def _extract_table_rows(table: Any) -> list[list[str]]:
for row in table.rows:
cells: list[str] = []
for cell in row.cells:
- text = cell.text.strip()
- text = (
- text.replace("\r\n", "\n")
- .replace("\r", "\n")
- .replace("\v", "\n")
- )
- cells.append(text)
+ cells.append(_normalise_breaks(cell.text.strip()))
rows.append(cells)
return rows
@@ -338,7 +354,7 @@ class PPTParser(ParserBase):
if getattr(shape, "has_text_frame", False):
try:
parts = [
- para.text.strip()
+ _normalise_breaks(para.text).strip()
for para in shape.text_frame.paragraphs
if para.text.strip()
]
diff --git a/tests/rag_parser_test.py b/tests/rag_parser_test.py
index f8660e963..c5e5083c9 100644
--- a/tests/rag_parser_test.py
+++ b/tests/rag_parser_test.py
@@ -624,6 +624,95 @@ class ImageParserTest(IsolatedAsyncioTestCase):
class PPTParserTest(IsolatedAsyncioTestCase):
"""Behavioural coverage for :class:`PPTParser`."""
+ @staticmethod
+ def _deck_with_soft_break() -> bytes:
+ """Build a deck whose text frame has one soft line break."""
+ from pptx import Presentation
+ from pptx.util import Inches
+
+ prs = Presentation()
+ slide = prs.slides.add_slide(prs.slide_layouts[6]) # blank
+ box = slide.shapes.add_textbox(
+ Inches(0.5),
+ Inches(0.5),
+ Inches(4),
+ Inches(1),
+ )
+ paragraph = box.text_frame.paragraphs[0]
+ paragraph.add_run().text = "one"
+ paragraph.add_line_break()
+ paragraph.add_run().text = "two"
+
+ buffer = io.BytesIO()
+ prs.save(buffer)
+ return buffer.getvalue()
+
+ async def test_soft_line_break_becomes_a_newline(self) -> None:
+ """``add_line_break()`` shows up as ``\n``, not a control char."""
+ parser = PPTParser(include_image=False)
+ sections = await parser.parse(
+ self._deck_with_soft_break(),
+ "soft_break.pptx",
+ )
+
+ self.assertEqual(
+ [section.model_dump() for section in sections],
+ [
+ {
+ "content": {
+ "type": "text",
+ "text": "<slide index=1>\none\ntwo\n</slide>",
+ "id": AnyString(),
+ "created_at": AnyString(),
+ "finished_at": None,
+ },
+ "source": "soft_break.pptx",
+ "metadata": {"slide": 1},
+ },
+ ],
+ )
+
+ async def test_ordinary_newlines_are_preserved(self) -> None:
+ """A paragraph per line still yields newline-separated text."""
+ from pptx import Presentation
+ from pptx.util import Inches
+
+ prs = Presentation()
+ slide = prs.slides.add_slide(prs.slide_layouts[6]) # blank
+ box = slide.shapes.add_textbox(
+ Inches(0.5),
+ Inches(0.5),
+ Inches(4),
+ Inches(1),
+ )
+ frame = box.text_frame
+ frame.text = "one"
+ for line in ("two", "three"):
+ paragraph = frame.add_paragraph()
+ paragraph.text = line
+
+ buffer = io.BytesIO()
+ prs.save(buffer)
+ parser = PPTParser(include_image=False)
+ sections = await parser.parse(buffer.getvalue(), "paras.pptx")
+
+ self.assertEqual(
+ [section.model_dump() for section in sections],
+ [
+ {
+ "content": {
+ "type": "text",
+ "text": ("<slide index=1>\none\ntwo\nthree\n</slide>"),
+ "id": AnyString(),
+ "created_at": AnyString(),
+ "finished_at": None,
+ },
+ "source": "paras.pptx",
+ "metadata": {"slide": 1},
+ },
+ ],
+ )
+
async def test_simple_deck_text_only(self) -> None:
"""A simple text-only deck round-trips through wrapping tags."""
pptx_bytes = _make_pptx_simple(["Alpha", "Beta"])