Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(docreader): avoid chart serialization during xlsx preprocessing
jiahao6635cc690bbe7f46
diff --git a/docreader/parser/xlsx_merge.py b/docreader/parser/xlsx_merge.pyindex 8fe4b2bc0..a0613bdb8 100644--- a/docreader/parser/xlsx_merge.py+++ b/docreader/parser/xlsx_merge.py@@ -36,6 +36,20 @@ def fill_merged_cells_xlsx(content: bytes) -> bytes:if not changed:return content+ # Charts are not part of the cell text extracted by DocReader. Remove+ # dedicated chart sheets through the public workbook API, then clear+ # embedded worksheet charts (openpyxl has no public removal API for them)+ # before saving this temporary workbook.+ active_sheet = wb.active+ for chart_sheet in list(wb.chartsheets):+ wb.remove(chart_sheet)+ if active_sheet in wb.worksheets:+ wb.active = active_sheet+ elif wb.worksheets:+ wb.active = wb.worksheets[0]+ for ws in wb.worksheets:+ ws._charts.clear()+out = BytesIO()wb.save(out)logger.info("Filled merged cells in XLSX before parse")diff --git a/docreader/tests/test_excel_parser.py b/docreader/tests/test_excel_parser.pyindex 5d8516e68..682613ff2 100644--- a/docreader/tests/test_excel_parser.py+++ b/docreader/tests/test_excel_parser.py@@ -5,9 +5,11 @@ import subprocessimport tempfileimport unittestimport zipfile+from unittest.mock import patchimport openpyxlimport pandas as pd+from openpyxl.chart import BarChart, Referencefrom docreader.parser.excel_convert import detect_excel_format, engine_for_formatfrom docreader.parser.excel_parser import ExcelParser@@ -136,6 +138,28 @@ class XlsxRepairTest(unittest.TestCase):class XlsxMergeFillTest(unittest.TestCase):+ @staticmethod+ def _workbook_with_merged_cells_and_chart() -> bytes:+ wb = openpyxl.Workbook()+ ws = wb.active+ ws["A1"] = "title"+ ws.merge_cells("A1:B1")+ ws.append(["category", "value"])+ ws.append(["one", 1])+ ws.append(["two", 2])++ chart = BarChart()+ chart.add_data(+ Reference(ws, min_col=2, min_row=2, max_row=4),+ titles_from_data=True,+ )+ chart.set_categories(Reference(ws, min_col=1, min_row=3, max_row=4))+ ws.add_chart(chart, "D2")++ bio = io.BytesIO()+ wb.save(bio)+ return bio.getvalue()+def test_fill_merged_cells_propagates_master_value(self):wb = openpyxl.Workbook()ws = wb.active@@ -155,6 +179,89 @@ class XlsxMergeFillTest(unittest.TestCase):self.assertEqual(out_ws["A3"].value, "left")self.assertEqual(out_ws["B3"].value, "only-b")+ def test_fill_merged_cells_does_not_serialize_charts(self):+ content = self._workbook_with_merged_cells_and_chart()++ with patch(+ "openpyxl.chart._chart.ChartBase._write",+ side_effect=AttributeError("'Typed' object has no attribute 'to_tree'"),+ ):+ filled = fill_merged_cells_xlsx(content)++ out_wb = openpyxl.load_workbook(io.BytesIO(filled), data_only=True)+ out_ws = out_wb.active+ self.assertEqual(out_ws["B1"].value, "title")+ self.assertEqual(out_ws["A3"].value, "one")+ self.assertEqual(out_ws["B4"].value, 2)+ self.assertEqual(out_ws._charts, [])++ def test_excel_parser_reads_merged_workbook_with_chart(self):+ document = ExcelParser().parse_into_text(+ self._workbook_with_merged_cells_and_chart()+ )++ self.assertIn("A: title,B: title", document.content)+ self.assertIn("A: one,B: 1", document.content)+ self.assertIn("A: two,B: 2", document.content)++ def test_chart_workbook_without_merges_passes_through_unchanged(self):+ content = self._workbook_with_merged_cells_and_chart()+ wb = openpyxl.load_workbook(io.BytesIO(content))+ wb.active.unmerge_cells("A1:B1")+ bio = io.BytesIO()+ wb.save(bio)+ content = bio.getvalue()++ self.assertEqual(fill_merged_cells_xlsx(content), content)++ def test_fill_does_not_serialize_dedicated_chart_sheets(self):+ content = self._workbook_with_merged_cells_and_chart()+ wb = openpyxl.load_workbook(io.BytesIO(content))+ ws = wb.active+ chart = BarChart()+ chart.add_data(+ Reference(ws, min_col=2, min_row=2, max_row=4),+ titles_from_data=True,+ )+ chart.set_categories(Reference(ws, min_col=1, min_row=3, max_row=4))+ chart_sheet = wb.create_chartsheet("Summary")+ chart_sheet.add_chart(chart)+ wb.active = chart_sheet+ bio = io.BytesIO()+ wb.save(bio)++ with patch(+ "openpyxl.chart._chart.ChartBase._write",+ side_effect=AttributeError("'Typed' object has no attribute 'to_tree'"),+ ):+ filled = fill_merged_cells_xlsx(bio.getvalue())++ out_wb = openpyxl.load_workbook(io.BytesIO(filled), data_only=True)+ self.assertEqual(out_wb.active._charts, [])+ self.assertEqual(out_wb.active.title, "Sheet")+ self.assertEqual(out_wb.chartsheets, [])++ def test_fill_keeps_active_worksheet_after_removing_earlier_chart_sheet(self):+ content = self._workbook_with_merged_cells_and_chart()+ wb = openpyxl.load_workbook(io.BytesIO(content))+ data_sheet = wb.active+ chart = BarChart()+ chart.add_data(+ Reference(data_sheet, min_col=2, min_row=2, max_row=4),+ titles_from_data=True,+ )+ chart_sheet = wb.create_chartsheet("Summary", 0)+ chart_sheet.add_chart(chart)+ wb.active = data_sheet+ bio = io.BytesIO()+ wb.save(bio)++ filled = fill_merged_cells_xlsx(bio.getvalue())++ out_wb = openpyxl.load_workbook(io.BytesIO(filled), data_only=True)+ self.assertEqual(out_wb.active.title, "Sheet")+ self.assertEqual(out_wb.chartsheets, [])+def test_parse_en_mergecell_workbook(self):path = os.path.join(os.path.dirname(__file__),