diff --git a/docling/backend/msexcel_backend.py b/docling/backend/msexcel_backend.py index 443e9d46..aeaed4f1 100644 --- a/docling/backend/msexcel_backend.py +++ b/docling/backend/msexcel_backend.py @@ -139,10 +139,14 @@ class MsExcelDocumentBackend(DeclarativeDocumentBackend, PaginatedDocumentBacken self.workbook = None try: if isinstance(self.path_or_stream, BytesIO): - self.workbook = load_workbook(filename=self.path_or_stream) + self.workbook = load_workbook( + filename=self.path_or_stream, data_only=True + ) elif isinstance(self.path_or_stream, Path): - self.workbook = load_workbook(filename=str(self.path_or_stream)) + self.workbook = load_workbook( + filename=str(self.path_or_stream), data_only=True + ) self.valid = self.workbook is not None except Exception as e: diff --git a/tests/data/groundtruth/docling_v2/xlsx_03_chartsheet.xlsx.itxt b/tests/data/groundtruth/docling_v2/xlsx_03_chartsheet.xlsx.itxt index 8b435a35..2bb5754e 100644 --- a/tests/data/groundtruth/docling_v2/xlsx_03_chartsheet.xlsx.itxt +++ b/tests/data/groundtruth/docling_v2/xlsx_03_chartsheet.xlsx.itxt @@ -1,4 +1,4 @@ item-0 at level 0: unspecified: group _root_ item-1 at level 1: section: group sheet: Duck Observations - item-2 at level 2: table with [7x3] + item-2 at level 2: table with [7x4] item-3 at level 1: section: group sheet: Duck Chart \ No newline at end of file diff --git a/tests/data/groundtruth/docling_v2/xlsx_03_chartsheet.xlsx.json b/tests/data/groundtruth/docling_v2/xlsx_03_chartsheet.xlsx.json index 449b5f40..66ede3e3 100644 --- a/tests/data/groundtruth/docling_v2/xlsx_03_chartsheet.xlsx.json +++ b/tests/data/groundtruth/docling_v2/xlsx_03_chartsheet.xlsx.json @@ -71,7 +71,7 @@ "bbox": { "l": 0.0, "t": 0.0, - "r": 3.0, + "r": 4.0, "b": 7.0, "coord_origin": "TOPLEFT" }, @@ -125,6 +125,19 @@ "row_section": false, "fillable": false }, + { + "row_span": 1, + "col_span": 1, + "start_row_offset_idx": 0, + "end_row_offset_idx": 1, + "start_col_offset_idx": 3, + "end_col_offset_idx": 4, + "text": "Ducks", + "column_header": true, + "row_header": false, + "row_section": false, + "fillable": false + }, { "row_span": 1, "col_span": 1, @@ -164,6 +177,19 @@ "row_section": false, "fillable": false }, + { + "row_span": 1, + "col_span": 1, + "start_row_offset_idx": 1, + "end_row_offset_idx": 2, + "start_col_offset_idx": 3, + "end_col_offset_idx": 4, + "text": "200", + "column_header": false, + "row_header": false, + "row_section": false, + "fillable": false + }, { "row_span": 1, "col_span": 1, @@ -203,6 +229,19 @@ "row_section": false, "fillable": false }, + { + "row_span": 1, + "col_span": 1, + "start_row_offset_idx": 2, + "end_row_offset_idx": 3, + "start_col_offset_idx": 3, + "end_col_offset_idx": 4, + "text": "230", + "column_header": false, + "row_header": false, + "row_section": false, + "fillable": false + }, { "row_span": 1, "col_span": 1, @@ -242,6 +281,19 @@ "row_section": false, "fillable": false }, + { + "row_span": 1, + "col_span": 1, + "start_row_offset_idx": 3, + "end_row_offset_idx": 4, + "start_col_offset_idx": 3, + "end_col_offset_idx": 4, + "text": "250", + "column_header": false, + "row_header": false, + "row_section": false, + "fillable": false + }, { "row_span": 1, "col_span": 1, @@ -281,6 +333,19 @@ "row_section": false, "fillable": false }, + { + "row_span": 1, + "col_span": 1, + "start_row_offset_idx": 4, + "end_row_offset_idx": 5, + "start_col_offset_idx": 3, + "end_col_offset_idx": 4, + "text": "280", + "column_header": false, + "row_header": false, + "row_section": false, + "fillable": false + }, { "row_span": 1, "col_span": 1, @@ -320,6 +385,19 @@ "row_section": false, "fillable": false }, + { + "row_span": 1, + "col_span": 1, + "start_row_offset_idx": 5, + "end_row_offset_idx": 6, + "start_col_offset_idx": 3, + "end_col_offset_idx": 4, + "text": "280", + "column_header": false, + "row_header": false, + "row_section": false, + "fillable": false + }, { "row_span": 1, "col_span": 1, @@ -358,10 +436,23 @@ "row_header": false, "row_section": false, "fillable": false + }, + { + "row_span": 1, + "col_span": 1, + "start_row_offset_idx": 6, + "end_row_offset_idx": 7, + "start_col_offset_idx": 3, + "end_col_offset_idx": 4, + "text": "310", + "column_header": false, + "row_header": false, + "row_section": false, + "fillable": false } ], "num_rows": 7, - "num_cols": 3, + "num_cols": 4, "grid": [ [ { @@ -402,6 +493,19 @@ "row_header": false, "row_section": false, "fillable": false + }, + { + "row_span": 1, + "col_span": 1, + "start_row_offset_idx": 0, + "end_row_offset_idx": 1, + "start_col_offset_idx": 3, + "end_col_offset_idx": 4, + "text": "Ducks", + "column_header": true, + "row_header": false, + "row_section": false, + "fillable": false } ], [ @@ -443,6 +547,19 @@ "row_header": false, "row_section": false, "fillable": false + }, + { + "row_span": 1, + "col_span": 1, + "start_row_offset_idx": 1, + "end_row_offset_idx": 2, + "start_col_offset_idx": 3, + "end_col_offset_idx": 4, + "text": "200", + "column_header": false, + "row_header": false, + "row_section": false, + "fillable": false } ], [ @@ -484,6 +601,19 @@ "row_header": false, "row_section": false, "fillable": false + }, + { + "row_span": 1, + "col_span": 1, + "start_row_offset_idx": 2, + "end_row_offset_idx": 3, + "start_col_offset_idx": 3, + "end_col_offset_idx": 4, + "text": "230", + "column_header": false, + "row_header": false, + "row_section": false, + "fillable": false } ], [ @@ -525,6 +655,19 @@ "row_header": false, "row_section": false, "fillable": false + }, + { + "row_span": 1, + "col_span": 1, + "start_row_offset_idx": 3, + "end_row_offset_idx": 4, + "start_col_offset_idx": 3, + "end_col_offset_idx": 4, + "text": "250", + "column_header": false, + "row_header": false, + "row_section": false, + "fillable": false } ], [ @@ -566,6 +709,19 @@ "row_header": false, "row_section": false, "fillable": false + }, + { + "row_span": 1, + "col_span": 1, + "start_row_offset_idx": 4, + "end_row_offset_idx": 5, + "start_col_offset_idx": 3, + "end_col_offset_idx": 4, + "text": "280", + "column_header": false, + "row_header": false, + "row_section": false, + "fillable": false } ], [ @@ -607,6 +763,19 @@ "row_header": false, "row_section": false, "fillable": false + }, + { + "row_span": 1, + "col_span": 1, + "start_row_offset_idx": 5, + "end_row_offset_idx": 6, + "start_col_offset_idx": 3, + "end_col_offset_idx": 4, + "text": "280", + "column_header": false, + "row_header": false, + "row_section": false, + "fillable": false } ], [ @@ -648,6 +817,19 @@ "row_header": false, "row_section": false, "fillable": false + }, + { + "row_span": 1, + "col_span": 1, + "start_row_offset_idx": 6, + "end_row_offset_idx": 7, + "start_col_offset_idx": 3, + "end_col_offset_idx": 4, + "text": "310", + "column_header": false, + "row_header": false, + "row_section": false, + "fillable": false } ] ] @@ -660,7 +842,7 @@ "pages": { "1": { "size": { - "width": 3.0, + "width": 4.0, "height": 7.0 }, "page_no": 1 @@ -673,4 +855,4 @@ "page_no": 2 } } -} \ No newline at end of file +} diff --git a/tests/data/groundtruth/docling_v2/xlsx_03_chartsheet.xlsx.md b/tests/data/groundtruth/docling_v2/xlsx_03_chartsheet.xlsx.md index 3638d491..e0977b8b 100644 --- a/tests/data/groundtruth/docling_v2/xlsx_03_chartsheet.xlsx.md +++ b/tests/data/groundtruth/docling_v2/xlsx_03_chartsheet.xlsx.md @@ -1,8 +1,8 @@ -| Year | Freshwater Ducks | Saltwater Ducks | -|--------|--------------------|-------------------| -| 2019 | 120 | 80 | -| 2020 | 135 | 95 | -| 2021 | 150 | 100 | -| 2022 | 170 | 110 | -| 2023 | 160 | 120 | -| 2024 | 180 | 130 | \ No newline at end of file +| Year | Freshwater Ducks | Saltwater Ducks | Ducks | +|--------|--------------------|-------------------|---------| +| 2019 | 120 | 80 | 200 | +| 2020 | 135 | 95 | 230 | +| 2021 | 150 | 100 | 250 | +| 2022 | 170 | 110 | 280 | +| 2023 | 160 | 120 | 280 | +| 2024 | 180 | 130 | 310 | \ No newline at end of file diff --git a/tests/data/xlsx/xlsx_03_chartsheet.xlsx b/tests/data/xlsx/xlsx_03_chartsheet.xlsx index 366859a2..fab68787 100644 Binary files a/tests/data/xlsx/xlsx_03_chartsheet.xlsx and b/tests/data/xlsx/xlsx_03_chartsheet.xlsx differ diff --git a/tests/test_backend_msexcel.py b/tests/test_backend_msexcel.py index e3dc8f8c..6084a4b6 100644 --- a/tests/test_backend_msexcel.py +++ b/tests/test_backend_msexcel.py @@ -1,4 +1,5 @@ import logging +from io import BytesIO from pathlib import Path import pytest @@ -116,6 +117,52 @@ def test_chartsheet(documents) -> None: assert doc.pages[2].size.width == 0 +def test_chartsheet_data_values(documents) -> None: + """Test that data values are extracted correctly from xlsx_03_chartsheet. + + This test verifies that calculated values (not formulas) are returned. + The file contains duck observations with year 2024 having a total of 310 ducks. + We need to verify that both 2024 and 310 appear in the parsed data. + + Args: + documents: The paths and converted documents. + """ + doc = next(item for path, item in documents if path.stem == "xlsx_03_chartsheet") + + # Find all tables + tables = list(doc.tables) + assert len(tables) > 0, "Should have at least one table" + + # Look for a table that has the year 2024 in it + table_with_2024 = None + row_index_of_2024 = None + + for table in tables: + for cell in table.data.table_cells: + if cell.text == "2024": + table_with_2024 = table + row_index_of_2024 = cell.start_row_offset_idx + break + if table_with_2024: + break + + assert table_with_2024 is not None, "Should find a table containing year 2024" + assert row_index_of_2024 is not None, "Should find row index for 2024" + + # Now verify that the value 310 exists in the document + # (it may be in the same table or a different table due to how the parser splits tables) + found_310 = False + for table in tables: + for cell in table.data.table_cells: + if cell.text == "310": + found_310 = True + break + if found_310: + break + + assert found_310, "Should find the value 310 (total ducks for 2024) in the document" + + def test_inflated_rows_handling(documents) -> None: """Test that files with inflated max_row are handled correctly. @@ -178,3 +225,46 @@ def test_inflated_rows_handling(documents) -> None: f"reported {reported_max_row:,} rows, " f"correctly processed as {page_count} pages with proper dimensions" ) + + +def test_bytesio_stream(): + """Test that Excel files can be loaded from BytesIO streams. + + This test verifies that the BytesIO code path in the backend is working correctly, + ensuring that data_only=True is applied when loading workbooks from streams. + """ + # Get a test Excel file + path = next(item for item in get_excel_paths() if item.stem == "xlsx_01") + + # Load the file into a BytesIO stream + buf = BytesIO(path.open("rb").read()) + + # Create an InputDocument with the BytesIO stream + in_doc = InputDocument( + path_or_stream=buf, + format=InputFormat.XLSX, + filename=path.stem, + backend=MsExcelDocumentBackend, + ) + + # Initialize the backend with the BytesIO stream + backend = MsExcelDocumentBackend(in_doc=in_doc, path_or_stream=buf) + + # Verify the backend is valid + assert backend.is_valid(), "Backend should be valid when loaded from BytesIO" + + # Verify page count matches expected value + assert backend.page_count() == 4, "Should detect 4 pages from BytesIO stream" + + # Convert the document + doc = backend.convert() + + # Verify the document was converted successfully + assert doc is not None, "Document should be converted from BytesIO stream" + assert len(doc.pages) == 4, "Document should have 4 pages" + + # Verify page sizes match expected dimensions + assert doc.pages.get(1).size.as_tuple() == (3.0, 7.0) + assert doc.pages.get(2).size.as_tuple() == (9.0, 18.0) + assert doc.pages.get(3).size.as_tuple() == (13.0, 36.0) + assert doc.pages.get(4).size.as_tuple() == (0.0, 0.0)