Python tool for converting files and office documents to Markdown.
fix(xlsx): tolerate legacy showZeroes sheet views (#2064)
Yufeng He0f26ef9fbce9
diff --git a/packages/markitdown/src/markitdown/converters/_xlsx_converter.py b/packages/markitdown/src/markitdown/converters/_xlsx_converter.pyindex 4186ec77..355dd8f8 100644--- a/packages/markitdown/src/markitdown/converters/_xlsx_converter.py+++ b/packages/markitdown/src/markitdown/converters/_xlsx_converter.py@@ -1,4 +1,7 @@+import io+import reimport sys+import zipfilefrom typing import BinaryIO, Anyfrom ._html_converter import HtmlConverterfrom .._base_converter import DocumentConverter, DocumentConverterResult@@ -33,6 +36,54 @@ ACCEPTED_XLS_MIME_TYPE_PREFIXES = [ACCEPTED_XLS_FILE_EXTENSIONS = [".xls"]+# Some producers write the legacy attribute "showZeroes" on <sheetView>, where the+# schema calls it "showZeros". openpyxl rejects the unknown attribute outright, so the+# workbook is repaired by renaming it. The rename is confined to <sheetView> start tags.+_SHEET_VIEW_START_TAG = re.compile(rb"<sheetView(?=[\s/>])[^>]*>")+_SHOW_ZEROES_ATTRIBUTE = re.compile(rb"(?<=[\s])showZeroes(\s*=)")+++def _read_xlsx_sheets(file_stream: BinaryIO) -> dict[str, Any]:+ start_pos = file_stream.tell()+ try:+ return pd.read_excel(file_stream, sheet_name=None, engine="openpyxl")+ except TypeError as exc:+ if "showZeroes" not in str(exc):+ raise++ repaired_stream = _repair_sheetview_show_zeroes(file_stream, start_pos)+ return pd.read_excel(repaired_stream, sheet_name=None, engine="openpyxl")+++def _rename_show_zeroes_attribute(data: bytes) -> bytes:+ return _SHEET_VIEW_START_TAG.sub(+ lambda match: _SHOW_ZEROES_ATTRIBUTE.sub(rb"showZeros\1", match.group(0)),+ data,+ )+++def _repair_sheetview_show_zeroes(+ file_stream: BinaryIO, start_pos: int = 0+) -> io.BytesIO:+ file_stream.seek(start_pos)+ repaired_stream = io.BytesIO()++ with zipfile.ZipFile(file_stream) as source:+ with zipfile.ZipFile(repaired_stream, "w", zipfile.ZIP_DEFLATED) as target:+ for item in source.infolist():+ data = source.read(item.filename)+ if (+ item.filename.startswith("xl/worksheets/")+ and item.filename.endswith(".xml")+ and b"showZeroes" in data+ ):+ data = _rename_show_zeroes_attribute(data)+ target.writestr(item, data)++ repaired_stream.seek(0)+ return repaired_stream++class XlsxConverter(DocumentConverter):"""Converts XLSX files to Markdown, with each sheet presented as a separate Markdown table.@@ -80,7 +131,7 @@ class XlsxConverter(DocumentConverter):_xlsx_dependency_exc_info[2])- sheets = pd.read_excel(file_stream, sheet_name=None, engine="openpyxl")+ sheets = _read_xlsx_sheets(file_stream)md_content = ""for s in sheets:md_content += f"## {s}\n"diff --git a/packages/markitdown/tests/test_module_misc.py b/packages/markitdown/tests/test_module_misc.pyindex 55684ae1..6f39a174 100644--- a/packages/markitdown/tests/test_module_misc.py+++ b/packages/markitdown/tests/test_module_misc.py@@ -6,8 +6,8 @@ import osimport reimport shutilimport zipfile-from types import SimpleNamespaceimport pytest+from types import SimpleNamespacefrom unittest.mock import MagicMockimport markitdown._uri_utils as uri_utils@@ -374,6 +374,77 @@ def test_docx_equations() -> None:assert block_equations, "No block equations found in the document."+def test_xlsx_legacy_show_zeroes_sheetview(tmp_path) -> None:+ from openpyxl import Workbook++ base_path = tmp_path / "base.xlsx"+ xlsx_path = tmp_path / "legacy_show_zeroes.xlsx"++ workbook = Workbook()+ sheet = workbook.active+ sheet.title = "Data"+ sheet["A1"] = "hello"+ sheet["B1"] = "world"+ # A cell whose text is byte-identical to the malformed attribute. openpyxl stores it+ # as an inline string, so it lands in the very worksheet part that gets repaired.+ sheet["A2"] = ' showZeroes="0" '+ workbook.save(base_path)++ with zipfile.ZipFile(base_path) as source:+ with zipfile.ZipFile(xlsx_path, "w", zipfile.ZIP_DEFLATED) as target:+ for item in source.infolist():+ data = source.read(item.filename)+ if item.filename == "xl/worksheets/sheet1.xml":+ data = data.replace(+ b"<sheetView ", b'<sheetView showZeroes="0" ', 1+ )+ # Guard the fixture: the sheet view attribute and the cell text must+ # both live here, or this test stops exercising the repair.+ assert data.count(b'showZeroes="0"') == 2+ target.writestr(item, data)++ result = MarkItDown().convert(str(xlsx_path))++ assert "## Data" in result.markdown+ assert "hello" in result.markdown+ assert "world" in result.markdown++ # The repair must not reach into the worksheet's data: the cell keeps its text ...+ assert 'showZeroes="0"' in result.markdown+ # ... and no part of the document was silently renamed on the way through.+ assert "showZeros" not in result.markdown+++def test_xlsx_show_zeroes_rename_is_scoped_to_sheet_view_tags() -> None:+ from markitdown.converters._xlsx_converter import _rename_show_zeroes_attribute++ worksheet_xml = (+ b"<worksheet><sheetViews>"+ b'<sheetView showZeroes="0" workbookViewId="0"/>'+ b'<sheetView\n\tshowZeroes="1"\ttabSelected="1"/>'+ b"</sheetViews>"+ # openpyxl ignores custom sheet views entirely, so they need no repair+ b'<customSheetViews><customSheetView showZeroes="0"/></customSheetViews>'+ b'<sheetData><row r="1">'+ b'<c r="A1" t="inlineStr"><is><t xml:space="preserve"> showZeroes="0" '+ b"</t></is></c>"+ b'<c r="B1"><f>IF(A1=" showZeroes=","x","y")</f><v>y</v></c>'+ b"</row></sheetData></worksheet>"+ )++ repaired = _rename_show_zeroes_attribute(worksheet_xml)++ # Every <sheetView> start tag is repaired, whatever separates its attributes ...+ assert repaired.count(b"showZeros=") == 2+ assert b'<sheetView showZeros="0" workbookViewId="0"/>' in repaired+ assert b'<sheetView\n\tshowZeros="1"\ttabSelected="1"/>' in repaired++ # ... and nothing outside those start tags is rewritten.+ assert b'<t xml:space="preserve"> showZeroes="0" </t>' in repaired+ assert b'<f>IF(A1=" showZeroes=","x","y")</f>' in repaired+ assert b'<customSheetView showZeroes="0"/>' in repaired++def test_input_as_strings() -> None:markitdown = MarkItDown()