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
9 changes: 7 additions & 2 deletions haystack/components/converters/xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,15 @@ def _extract_tables(self, bytestream: ByteStream) -> tuple[list[str], list[dict]
"index": True,
"headers": value.columns,
"tablefmt": "pipe",
"missingval": "",
**self.table_format_kwargs,
}
# to_markdown uses tabulate
tables.append(value.to_markdown(**resolved_kwargs))
# to_markdown uses tabulate, whose missingval only covers None: a NaN
# reaches the formatter as a number and is written out as "nan". Replace
# the empty cells with None so an empty cell reads as empty, the way
# to_csv already writes it, and so missingval keeps working.
filled = value.astype(object).where(value.notna(), None)
tables.append(filled.to_markdown(**resolved_kwargs))
# add sheet_name to metadata
metadata.append({"xlsx": {"sheet_name": key}})
return tables, metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
fixes:
- |
Fixed `XLSXToDocument` writing the string `nan` into an empty cell when
`table_format="markdown"`. The same cell is written as an empty field by
`table_format="csv"`, so an empty cell now reads as empty in both formats.
A different placeholder can be set with
`table_format_kwargs={"missingval": "N/A"}`.
13 changes: 12 additions & 1 deletion test/components/converters/test_xlsx_to_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,25 @@ def test_run_markdown(self, test_files_path: Path) -> None:
}
assert (
documents[1].content
== "| | A | B |\n|---:|:------|:------|\n| 1 | col_c | col_d |\n| 2 | True | nan |"
# The empty cell reads as empty, the way the CSV format already writes it.
== "| | A | B |\n|---:|:------|:------|\n| 1 | col_c | col_d |\n| 2 | True | |"
)
assert documents[1].meta == {
"date_added": "2022-01-01T00:00:00",
"file_path": str(test_files_path / "xlsx" / "basic_tables_two_sheets.xlsx"),
"xlsx": {"sheet_name": "Table Missing Value"},
}

def test_run_markdown_missing_value(self, test_files_path: Path) -> None:
"""table_format_kwargs["missingval"] reaches tabulate for an empty cell."""
converter = XLSXToDocument(table_format="markdown", table_format_kwargs={"missingval": "N/A"})
paths: list[str | Path | ByteStream] = [test_files_path / "xlsx" / "basic_tables_two_sheets.xlsx"]
results = converter.run(sources=paths)
assert (
results["documents"][1].content
== "| | A | B |\n|---:|:------|:------|\n| 1 | col_c | col_d |\n| 2 | True | N/A |"
)

@pytest.mark.parametrize(
"sheet_name, expected_sheet_name, expected_content",
[
Expand Down