Skip to content

fix: preserve row boundaries in Table text representation - #4279

Closed
AlonNaor22 wants to merge 1 commit into
Unstructured-IO:mainfrom
AlonNaor22:fix/html-table-text-row-boundaries
Closed

AlonNaor22 wants to merge 1 commit into
Unstructured-IO:mainfrom
AlonNaor22:fix/html-table-text-row-boundaries

Conversation

@AlonNaor22

Copy link
Copy Markdown

Summary

  • Fixes bug/XLSX Table string representation loses row boundaries after 0.16.0 causing flattened output from partition_xlsx #4235 — partition_xlsx (and CSV, TSV, PPTX) Table elements lost row boundaries in str(Table) after v0.16.1
  • Root cause: HtmlTable.text used " ".join(self._table.itertext()) which flattened all cell text across all rows into a single space-separated string, destroying row structure
  • Fix: HtmlTable.text now joins cells within a row with spaces and joins rows with newlines, restoring the ability to split table text by \n to get logical rows
  • This was a regression from commit c85f29e which replaced soupparser_fromstring().text_content() (which preserved newlines) with the flattening itertext() approach
  • Chunking code is unaffected — it already does its own text extraction independently via iter_cell_texts()

Files changed

  • unstructured/common/html_table.py — HtmlTable.text property: iterate rows via iter_rows() and join with \n instead of flattening all text with spaces
  • test_unstructured/common/test_html_table.py — updated expected text to use newline-separated rows
  • test_unstructured/partition/test_constants.py — updated EXPECTED_TEXT* constants to reflect newline-separated row format
  • test_unstructured/partition/test_xlsx.py — updated direct .text == assertions for subtable and find_subtable=False tests
  • test_unstructured/partition/test_csv.py — removed unnecessary clean_extra_whitespace wrappers and unused import
  • test_unstructured/partition/test_tsv.py — updated header test assertion

Test plan

  • All 74 XLSX tests pass
  • All 24 html_table unit tests pass
  • All CSV tests pass (1 pre-existing failure in test_partition_csv_with_encoding unrelated to this change)
  • All 18 TSV tests pass
  • Linter and formatter clean (ruff check + ruff format)

HtmlTable.text was joining all cell text with spaces, destroying row
boundaries. This was a regression from v0.16.1 (commit c85f29e) when
soupparser_fromstring().text_content() was replaced with itertext()-based
flattening. Now cells within a row are space-separated and rows are
newline-separated, restoring the ability to reconstruct row structure
from str(Table).

Closes #4235

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@AlonNaor22 AlonNaor22 closed this by deleting the head repository Mar 8, 2026
linhongyu510 pushed a commit to linhongyu510/unstructured that referenced this pull request Oct 24, 2026
…, not a literal \n (Unstructured-IO#4592)

## Summary

`partition_csv()` and `partition_tsv()` build the table with
`DataFrame.to_html()`. By default pandas writes a line break or tab
inside a cell as its escape sequence, so a quoted field that spans lines
(RFC 4180 §2.6 allows this) reaches `Table.text` and
`metadata.text_as_html` with literal backslashes:

```python
import io
from unstructured.partition.csv import partition_csv

data = b'name,address\r\nAlice,"1 Main St\r\nSpringfield"\r\n'
partition_csv(file=io.BytesIO(data), include_header=True)[0].text
# main:         'name address Alice 1 Main St\\r\\nSpringfield'
# this branch:  'name address Alice 1 Main St Springfield'
```

`pd.read_csv()` parses the field correctly; the backslashes appear only
at `to_html()`. A TSV cell with a quoted newline gives `'line one\\nline
two'` the same way.

A small `dataframe_to_html()` helper in `csv.py`, used by both
partitioners, passes a formatter for each column so pandas writes the
cell text itself. HTML escaping is unchanged (`x<y & z` becomes `x&lt;y
&amp; z`) and empty cells stay empty. `HtmlTable` then folds the real
line break into a space, as it does with other whitespace in a cell, so
`text_as_html` doesn't keep the break as a `<br>`. If you'd rather keep
it, we can change that here.

Related: open Unstructured-IO#4373 (from June, now conflicting) replaces the same
`to_html()` call with its own HTML builder, which would also avoid this;
this PR is the smaller change on current `main`. Neighbours: Unstructured-IO#4574
(cells read as text) and Unstructured-IO#4376 / Unstructured-IO#4279 (row boundaries in
`HtmlTable.text`). We found no existing issue for this (searched csv/tsv
newline, line break, multiline, escape, `to_html`).

## Validation

- New tests:
`test_partition_csv_reads_a_line_break_in_a_quoted_cell_as_whitespace_not_an_escape`
(also covers a tab and HTML escaping of `<` and `&`) and the TSV
equivalent.
- `pytest test_unstructured/partition/test_csv.py
test_unstructured/partition/test_tsv.py` on Python 3.12, pandas 2.3.3:
103 passed, 9 skipped (the skips need `tiktoken`, not installed) on this
branch. With `main`'s `csv.py` and `tsv.py`, 2 failed, 101 passed, 9
skipped; only the two new tests fail.
- `ruff check` and `ruff format --check` (0.15.10) pass on the four
changed files.
- We didn't run the full `make test` or `make check`.

## Checklist

- [x] The PR title is a user-facing release-note entry with a
conventional commit prefix.
- [ ] Apply a category label (`enhancement`, `bug`, or an appropriate
existing label), or ask a maintainer to apply it. Could a maintainer
apply `bug`? We can't set labels from a fork.
- [ ] For breaking changes, use `!` in the title and explain impact and
migration above. Not a breaking change.

Prepared by CyberNative AI LLC, an AI-run company, using AI agents.


<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/Unstructured-IO/unstructured/pull/4592?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. -->
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.

bug/XLSX Table string representation loses row boundaries after 0.16.0 causing flattened output from partition_xlsx

1 participant