Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/markitdown-ocr/tests/test_xlsx_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def fixed_read(*args: Any, **kwargs: Any):
assert "Future shared fix" in result and "<native>" not in result
assert result.count("[Image OCR]") == 2
repair.assert_called_once()
assert calls == [{"sheet_name": None, "engine": "openpyxl"}] * 2
assert calls == [{"sheet_name": None, "engine": "openpyxl", "dtype": object}] * 2
service.extract_text.assert_called_once()


Expand Down
21 changes: 18 additions & 3 deletions packages/markitdown/src/markitdown/converters/_xlsx_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@
_SHOW_ZEROES_ATTRIBUTE = re.compile(rb"(?<=[\s])showZeroes(\s*=)")


def _read_excel_sheets(stream: BinaryIO, engine: str) -> dict[str, Any]:
"""Read every sheet, keeping text cells as text.

By default pandas parses a text cell that looks numeric into a number, so a
ZIP code "02134" becomes 2134 and "+15551234" becomes 15551234. Reading as
object keeps the value each cell holds; infer_objects() then restores the
usual column dtypes for genuinely numeric, boolean and date columns.

pandas' missing-value markers are unchanged: text such as "NA", "N/A",
"NULL" or "nan" in a data cell is still read as missing, as before.
"""
sheets = pd.read_excel(stream, sheet_name=None, engine=engine, dtype=object)
return {name: sheet.infer_objects() for name, sheet in sheets.items()}


@contextmanager
def _read_xlsx_sheets(
file_stream: BinaryIO,
Expand All @@ -52,12 +67,12 @@ def _read_xlsx_sheets(
repaired_stream = None
try:
try:
sheets = pd.read_excel(file_stream, sheet_name=None, engine="openpyxl")
sheets = _read_excel_sheets(file_stream, "openpyxl")
except TypeError as exc:
if "showZeroes" not in str(exc):
raise
repaired_stream = _repair_sheetview_show_zeroes(file_stream, start_pos)
sheets = pd.read_excel(repaired_stream, sheet_name=None, engine="openpyxl")
sheets = _read_excel_sheets(repaired_stream, "openpyxl")
yield sheets, repaired_stream if repaired_stream is not None else file_stream
finally:
if repaired_stream is not None:
Expand Down Expand Up @@ -237,7 +252,7 @@ def convert(
_xls_dependency_exc_info[2]
)

sheets = pd.read_excel(file_stream, sheet_name=None, engine="xlrd")
sheets = _read_excel_sheets(file_stream, "xlrd")
md_content = ""
for s in sheets:
md_content += f"## {s}\n"
Expand Down
66 changes: 66 additions & 0 deletions packages/markitdown/tests/test_xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,5 +541,71 @@ def test_xlsx_show_zeroes_rename_is_scoped_to_sheet_view_tags() -> None:
assert b'<customSheetView showZeroes="0"/>' in repaired


# Cell values


def _convert_rows(rows: list[list[Any]]) -> str:
workbook = openpyxl.Workbook()
for row in rows:
workbook.active.append(row)
stream = io.BytesIO()
workbook.save(stream)
stream.seek(0)
return MarkItDown().convert_stream(stream, stream_info=_INFO).markdown


def test_xlsx_text_cells_that_look_numeric_stay_text() -> None:
markdown = _convert_rows(
[
["zip", "phone", "account"],
["02134", "+15551234", "12345678901234567890"],
["00501", "0612345678", "007"],
]
)

assert markdown.splitlines()[-2:] == [
"| 02134 | +15551234 | 12345678901234567890 |",
"| 00501 | 0612345678 | 007 |",
]


def test_xlsx_missing_value_markers_are_unchanged() -> None:
# Keeping numeric-looking text does not change pandas' NA markers: these
# cells were missing before and still are, next to text that is kept.
markdown = _convert_rows(
[
["zip", "marker"],
["02134", "NA"],
["00501", "N/A"],
["00601", "NULL"],
["00701", "nan"],
["00801", "kept"],
]
)

assert markdown.splitlines()[-5:] == [
"| 02134 | NaN |",
"| 00501 | NaN |",
"| 00601 | NaN |",
"| 00701 | NaN |",
"| 00801 | kept |",
]


def test_xlsx_numeric_cells_render_as_before() -> None:
markdown = _convert_rows(
[
["int", "sparse", "float", "flag"],
[1, 2, 1.5, True],
[3, None, 0.25, False],
]
)

assert markdown.splitlines()[-2:] == [
"| 1 | 2.0 | 1.50 | True |",
"| 3 | NaN | 0.25 | False |",
]


if __name__ == "__main__":
raise SystemExit(pytest.main([__file__]))