Skip to content

fix(xlsx): keep cell text like N/A and None instead of emptying it - #4505

Merged
cragwolfe merged 4 commits into
Unstructured-IO:mainfrom
wenqiw777:wenqiw777/xlsx-keep-na-cell-text
Oct 24, 2026
Merged

cragwolfe merged 4 commits into
Unstructured-IO:mainfrom
wenqiw777:wenqiw777/xlsx-keep-na-cell-text

Conversation

@wenqiw777

@wenqiw777 wenqiw777 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Problem

partition_xlsx() reads each worksheet with pd.read_excel() and the pandas default NA strings, so a cell whose text is N/A, NA, null, None, NaN, #N/A or similar is read as missing and comes out empty. The missing cells also change how the worksheet is split into subtables, so a whole table can fall apart:

import io, openpyxl
from unstructured.partition.xlsx import partition_xlsx

wb = openpyxl.Workbook()
for row in (["Item", "Status"], ["Pear", "N/A"], ["Fig", "NA"], ["Plum", "null"], ["Kiwi", "None"]):
    wb.active.append(row)
f = io.BytesIO()
wb.save(f)

[(type(e).__name__, e.text) for e in partition_xlsx(file=f)]
# before: [('Table', 'Item Status'), ('Title', 'Pear'), ('Title', 'Fig'), ('Title', 'Plum'), ('Title', 'Kiwi')]
# after:  [('Table', 'Item Status Pear N/A Fig NA Plum null Kiwi None')]

Fix

Worksheets are read with keep_default_na=False, na_values=[""], so only empty cells are missing. Empty cells still come back as NaN, which keeps subtable detection and the empty cells in text_as_html unchanged (test_partition_xlsx_subtables covers the splitting). None of the XLSX files in example-docs contain NA-like text, so their outputs do not change.

#4373 makes the equivalent change for CSV and TSV; this PR only touches the XLSX reader.

Testing

pytest test_unstructured/partition/test_xlsx.py, including a new regression test built from the example above.

Review in cubic

partition_xlsx() read worksheets with the pandas default NA strings, so a cell
reading N/A, NA, null, None, NaN or similar came out empty, which could also
break a table apart into Title elements. Only empty cells are treated as
missing now.

@chrikrah chrikrah left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wenqiw777 I ran this at a0ee8b7 and falsified it: with both read_excel arguments reverted and your test
kept, 1 of the 75 fails, on the tuple unpack, which is the second half of the defect you describe. I would
merge it.

The change recovers more than the missing text. On a sheet whose second column holds 1, N/A, 3, main
renders Item 1.0 Fig Plum 3.0: the N/A is gone and the integers have become floats, because pandas made
that column float64 once the string was read as missing. With your arguments the same sheet gives
Item 1 Fig N/A Plum 3. Two defects, one fix.

na_values=[""] is already pinned, so nobody should trim it as redundant: drop it alone and 8 of the 75
fail, test_partition_xlsx_subtables among them, because a blank row stops counting as missing and the sheet
stops splitting.

non-blocking: the integer rendering is a silent output change for every existing document with an NA-like
string in a numeric column, since 1.0 becomes 1 in text_as_html and in text. Your changelog entry
covers the dropped text and not that, and somebody with golden files will meet it first.

Evidence, Python 3.13, editable install plus pandas, openpyxl, msoffcrypto-tool and pytest-mock, at
a0ee8b7:

$ python -m pytest test_unstructured/partition/test_xlsx.py -q
75 passed in 5.41s

# both arguments reverted, your test kept
$ python -m pytest test_unstructured/partition/test_xlsx.py -q
1 failed, 74 passed in 2.89s
FAILED ...::test_partition_xlsx_keeps_cell_text_that_pandas_reads_as_missing_by_default

# na_values=[""] dropped alone
$ python -m pytest test_unstructured/partition/test_xlsx.py -q
8 failed, 67 passed in 2.86s
FAILED ...::test_partition_xlsx_subtables

# one sheet, second column 1 / N/A / 3, run both ways
$ python probe_dtype.py
with the change:  Item 1 Fig N/A Plum 3          column dtype object
reverted:         Item 1.0 Fig Plum 3.0          column dtype float64

Every claim above came from those runs. I did not run the rest of the suite, and nothing here exercises the
password-protected path.

Is the float-to-integer change worth a line in the changelog entry, or do you read it as part of the same
fix?

@mi7ak2020-stack

Copy link
Copy Markdown

I

L4XB added a commit to L4XB/unstructured that referenced this pull request Oct 17, 2026
Unstructured-IO#4505 already reads cell text such as N/A, NULL or None as content. This
PR now only stops Pandas from inferring a dtype per column, so whole
numbers and booleans in a column with an empty cell keep their values;
the test uses a sheet without N/A-like text.
cragwolfe pushed a commit that referenced this pull request Oct 22, 2026
## Summary

`read_delimited_text()`, which reads every CSV and TSV upload, calls
`pd.read_csv()` with Pandas' default cell interpretation. That changes
what the file says in three ways:

| cell | `main` | this PR |
|---|---|---|
| `N/A`, `NULL`, `None` (and `NA`, `nan`, `n/a`, …) | empty | `N/A`,
`NULL`, `None` |
| `12` in a column that has an empty cell, with `include_header=True` |
`12.0` | `12` |
| `007` in a column of codes that has an empty cell, with
`include_header=True` | `7.0` | `007` |

Probe file `name,qty,status,code` / `Pen,12,N/A,007` / `Ink,,NULL,010` /
`Pad,7,ok,`. On `main` with `include_header=True` it reads `name qty
status code Pen 12.0 7.0 Ink 10.0 Pad 7.0 ok`, and with
`include_header=False` it reads `name qty status code Pen 12 007 Ink 010
Pad 7 ok`. With this PR it reads `name qty status code Pen 12 N/A 007
Ink NULL 010 Pad 7 ok`. Without a header row, the header strings already
make each column `object`, so only the `N/A` / `NULL` loss shows there.

## Change

Both `pd.read_csv()` calls in `read_delimited_text()` pass `dtype=str,
keep_default_na=False`. Every cell is the text the file holds, and an
empty field is an empty string. The HTML table still writes an empty
cell as empty. Delimiter sniffing, the lone-`\r` reader and the
cell-count limit are unchanged.

Related: #4505 does the same for XLSX (`keep_default_na=False`), and
#4556 keeps XLSX whole numbers (`dtype=object`). Each PR only adds read
options to its own reader.

## Testing

- New `test_partition_csv_reads_each_cell_as_written` (both
`include_header` values) and
`test_partition_tsv_reads_each_cell_as_written` fail on `main`.
- The rest of `test_csv.py` and `test_tsv.py` passes (97 in total).
- The 12 CSV/TSV files in `example-docs` partition to exactly the same
element text and HTML as on `main`.
- ruff 0.15.10 check and format are clean, and `scripts/version-sync.sh
-c` passes with `0.27.34-dev1`.



<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/Unstructured-IO/unstructured/pull/4574?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="View guided diff"
src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->

@cragwolfe cragwolfe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 74bbf38 with GPT Pro. No actionable defects found in the reviewed change. The two pandas arguments preserve literal NA-like strings while retaining blank-cell handling for subtable detection. The earlier numeric-rendering note is now documented.

GitHub reports merge conflicts with current main. When resolving them, carry both keep_default_na=False and na_values=[""] through the active ExcelWorkbookReader.parse() path, preserving workbook validation, row compaction, cell-count guards, formatting, and cleanup. This approval covers the reviewed head; the resolved integration will need fresh CI.

All reported exact-head CI checks, including required checks, succeeded. No local tests were run.

(authored by codex)

@cragwolfe
cragwolfe enabled auto-merge October 23, 2026 23:57
@cragwolfe
cragwolfe disabled auto-merge October 23, 2026 23:57
@cragwolfe
cragwolfe merged commit c4a8077 into Unstructured-IO:main Oct 24, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants