Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(rag): preserve paragraph boundaries inside Word text boxes (#3415)
12kkqq0d5ea1ad5ee8
diff --git a/src/agentscope/rag/_parser/_word.py b/src/agentscope/rag/_parser/_word.pyindex 762e76d59..2ec9dbc23 100644--- a/src/agentscope/rag/_parser/_word.py+++ b/src/agentscope/rag/_parser/_word.py@@ -51,7 +51,8 @@ def _extract_text_from_paragraph(para: DocxParagraph) -> str:Each ``mc:AlternateContent`` containing a text box uses the first branchwith readable Word text, retaining the fallback when its choices cannotsupply any text. Run-level tabs and soft line breaks are preserved in- both the paragraph and its text boxes.+ both the paragraph and its text boxes. Paragraph boundaries within each+ text box are retained.Args:para (`DocxParagraph`):@@ -64,6 +65,7 @@ def _extract_text_from_paragraph(para: DocxParagraph) -> str:from docx.oxml.ns import qntext_tag = qn("w:t")+ paragraph_tag = qn("w:p")text_box_tag = qn("w:txbxContent")deleted_tag = qn("w:del")run_tag = qn("w:r")@@ -101,7 +103,7 @@ def _extract_text_from_paragraph(para: DocxParagraph) -> str:yield from iter_text(child)own_parts: list[str] = []- text_boxes: dict[Any, list[str]] = {}+ text_boxes: dict[Any, dict[Any, list[str]]] = {}for element in iter_text(root):if element.tag == text_tag:value = element.text or ""@@ -117,21 +119,35 @@ def _extract_text_from_paragraph(para: DocxParagraph) -> str:if not value:continueparts = own_parts+ paragraph = rootfor ancestor in element.iterancestors():if ancestor is root:break+ if ancestor.tag == paragraph_tag:+ paragraph = ancestorif ancestor.tag == text_box_tag:- parts = text_boxes.setdefault(ancestor, [])+ parts = text_boxes.setdefault(ancestor, {}).setdefault(+ paragraph,+ [],+ )breakparts.append(value)own_text = "".join(own_parts)if not own_text.strip() and not any(- "".join(parts).strip() for parts in text_boxes.values()+ "".join(parts).strip()+ for paragraphs in text_boxes.values()+ for parts in paragraphs.values()):# Separator-only extraction must not hide w:noBreakHyphen, etc.own_text = para.text- blocks = [own_text, *("".join(parts) for parts in text_boxes.values())]+ blocks = [+ own_text,+ *(+ "\n".join("".join(parts) for parts in paragraphs.values())+ for paragraphs in text_boxes.values()+ ),+ ]return "\n".join(block.strip() for block in blocks if block.strip())diff --git a/tests/rag_parser_test.py b/tests/rag_parser_test.pyindex e787b0ea1..5aff9d447 100644--- a/tests/rag_parser_test.py+++ b/tests/rag_parser_test.py@@ -3119,6 +3119,67 @@ class WordParserParagraphTest(IsolatedAsyncioTestCase):_expected_word_sections("Paragraph text\nCallout\nAfter"),)+ async def test_text_box_paragraph_boundaries_are_preserved(self) -> None:+ """Separate text-box paragraphs while joining runs within each."""+ paragraphs = (+ "<w:p><w:r><w:t>First </w:t></w:r>"+ "<w:r><w:t>paragraph</w:t></w:r></w:p>"+ "<w:p><w:r><w:t>Second paragraph</w:t></w:r></w:p>"+ )+ for name, template in (+ ("modern", _DOCX_MODERN_TEXT_BOX),+ ("with_fallback", _DOCX_TEXT_BOX_WITH_VML_FALLBACK),+ (+ "fallback_only",+ _DOCX_TEXT_BOX_WITH_VML_FALLBACK.replace(+ "<w:t>{inside}</w:t>",+ "<w:t/>",+ 1,+ ),+ ),+ ):+ with self.subTest(shape=name):+ template = template.replace(+ "<w:p><w:r><w:t>{inside}</w:t></w:r></w:p>",+ paragraphs,+ )+ sections = await WordParser(include_image=False).parse(+ _make_docx_with_text_box(template),+ "demo.docx",+ )+ self.assertEqual(+ [section.model_dump() for section in sections],+ _expected_word_sections(+ "Paragraph text\nFirst paragraph\n"+ "Second paragraph\nAfter",+ ),+ )++ async def test_text_box_paragraphs_keep_inline_separators(self) -> None:+ """Keep paragraph and inline boundaries with no surrounding text."""+ paragraphs = (+ "<w:p><w:r><w:t>A</w:t><w:tab/><w:t>B</w:t></w:r></w:p>"+ "<w:p><w:r><w:t>C</w:t><w:br/><w:t>D</w:t>"+ "<w:cr/><w:t>E</w:t></w:r></w:p>"+ )+ for name, template in (+ ("modern", _DOCX_MODERN_TEXT_BOX),+ ("with_fallback", _DOCX_TEXT_BOX_WITH_VML_FALLBACK),+ ):+ with self.subTest(shape=name):+ template = template.replace(+ "<w:p><w:r><w:t>{inside}</w:t></w:r></w:p>",+ paragraphs,+ )+ sections = await WordParser(include_image=False).parse(+ _make_docx_with_text_box(template, before=""),+ "demo.docx",+ )+ self.assertEqual(+ [section.model_dump() for section in sections],+ _expected_word_sections("A\tB\nC\nD\nE\nAfter"),+ )+async def test_inline_separators_with_text_box_branches(self) -> None:"""Preserve inline separators while reading each text box once."""for name, template in (