Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(rag): preserve image-only and header-only Excel sheets (#2666)
JasonHd60d58ec7429
diff --git a/src/agentscope/rag/_parser/_excel.py b/src/agentscope/rag/_parser/_excel.pyindex 4f0d80939..3288ffb71 100644--- a/src/agentscope/rag/_parser/_excel.py+++ b/src/agentscope/rag/_parser/_excel.py@@ -134,7 +134,8 @@ class ExcelParser(ParserBase):Each sheet is scanned for tabular data and (optionally) images.Tables are rendered as Markdown pipe-tables or JSON arrays; images- are emitted as standalone :class:`DataBlock` sections.+ are emitted as standalone :class:`DataBlock` sections, including on+ sheets without cell values. Header-only tables are preserved.When ``separate_sheet=True`` each sheet becomes a batch ofsections that never intermix with other sheets, making it possible@@ -310,24 +311,23 @@ class ExcelParser(ParserBase):logger.warning("Failed to parse sheet '%s': %s", sheet_name, e)return sheet_sections- if df.empty:- return sheet_sections-- table_data = _extract_table_data(df)-- if self.table_format == "markdown":- table_text = self._table_to_markdown(table_data, sheet_name)- else:- table_text = self._table_to_json(table_data, sheet_name)+ # A header-only sheet is "empty" to pandas but still has columns+ if len(df.columns) > 0:+ table_data = _extract_table_data(df)- if table_text:- sheet_sections.append(- Section(- content=TextBlock(text=table_text),- source=filename,- metadata={"sheet": sheet_name},- ),- )+ if self.table_format == "markdown":+ table_text = self._table_to_markdown(table_data, sheet_name)+ else:+ table_text = self._table_to_json(table_data, sheet_name)++ if table_text:+ sheet_sections.append(+ Section(+ content=TextBlock(text=table_text),+ source=filename,+ metadata={"sheet": sheet_name},+ ),+ )if self.include_image and workbook is not None:try:diff --git a/tests/rag_parser_test.py b/tests/rag_parser_test.pyindex 100cdb2b5..9ef6b81c5 100644--- a/tests/rag_parser_test.py+++ b/tests/rag_parser_test.py@@ -1022,6 +1022,76 @@ class PPTParserTest(IsolatedAsyncioTestCase):class ExcelParserTest(IsolatedAsyncioTestCase):"""Behavioural coverage for :class:`ExcelParser`."""+ async def test_header_only_sheet(self) -> None:+ """A sheet with only a header row is kept as a table."""+ xlsx_bytes = _make_xlsx_simple({"Data": [["Revenue", "Year"]]})+ sections = await ExcelParser().parse(xlsx_bytes, "header.xlsx")++ self.assertListEqual(+ [s.model_dump() for s in sections],+ [+ {+ "content": {+ "type": "text",+ "text": (+ "Sheet: Data\n"+ "| Revenue | Year |\n"+ "| --- | --- |\n"+ ),+ "id": AnyString(),+ "created_at": AnyString(),+ "finished_at": None,+ },+ "source": "header.xlsx",+ "metadata": {},+ },+ ],+ )++ async def test_blank_sheet_has_no_sections(self) -> None:+ """A blank sheet produces neither a table nor images."""+ xlsx_bytes = _make_xlsx_simple({"Blank": []})+ sections = await ExcelParser().parse(xlsx_bytes, "blank.xlsx")++ self.assertListEqual(sections, [])++ async def test_image_only_sheet(self) -> None:+ """Images on a sheet without cell values are still extracted."""+ from openpyxl import Workbook+ from openpyxl.drawing.image import Image++ workbook = Workbook()+ workbook.active.title = "Images"+ workbook.active.add_image(Image(io.BytesIO(_PNG_PIXEL)), "A3")+ buffer = io.BytesIO()+ workbook.save(buffer)+ workbook.close()++ parser = ExcelParser(include_image=True)+ sections = await parser.parse(buffer.getvalue(), "images.xlsx")++ self.assertListEqual(+ [s.model_dump() for s in sections],+ [+ {+ "content": {+ "type": "data",+ "id": AnyString(),+ "source": {+ "type": "base64",+ "data": _PNG_PIXEL_B64,+ "media_type": "image/png",+ },+ "name": "images.xlsx",+ "created_at": AnyString(),+ "finished_at": None,+ },+ "source": "images.xlsx",+ "metadata": {"sheet": "Images", "media_type": "image/png"},+ },+ ],+ )+async def test_single_sheet_markdown(self) -> None:"""A single-sheet workbook produces one text Section withMarkdown table."""