Skip to content

fix: keep labels of auto-numbered DOCX lists - #4508

Merged
qued merged 7 commits into
mainfrom
qued/docx-numbered-list-labels
Oct 1, 2026
Merged

qued merged 7 commits into
mainfrom
qued/docx-numbered-list-labels

Conversation

@qued

@qued qued commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

What

Word does not store the label of an auto-numbered list item (1., a), iv.) in the paragraph. It computes the label at render time from numbering.xml, so partition_docx() returned ListItem text with no label, while typed-in numbers were kept.

The label is now resolved and prefixed to the item text:

  • numId / ilvl come from the paragraph's w:numPr or its style chain (including basedOn), mapped through abstractNum with lvlOverride / startOverride.
  • Counters are kept per level in document order; a deeper level restarts when a shallower one advances, and a list continues counting after an intervening paragraph.
  • Labels follow numFmt (decimal, letters, roman numerals, ...) and lvlText (%1., %2), (%3), ...).
  • Bullets stay unprefixed. Missing or malformed numbering falls back to the previous text and never raises.
  • A paragraph that only inherits numbering from its style is now recognized as a list item, unless its style maps to another element type such as a heading.

This changes ListItem.text for every auto-numbered DOCX, so one existing expectation was updated to include the label.

🤖 Generated with Claude Code

Review in cubic

qued and others added 3 commits September 29, 2026 17:36
Word computes list labels from numbering.xml at render time, so partition_docx returned ListItem text without them. Resolve numbering from the paragraph or its style chain and prefix the rendered label. Bullets stay unprefixed.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…DOCX list labels

Advance list counters for bullets, empty paragraphs and table paragraphs, and honor
lvlRestart. Shift hyperlink offsets past the generated label. Ignore levels and start
values outside Word's range, render ordinal and circled formats, and leave the text
unlabeled for number formats that cannot be rendered.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@qued
qued marked this pull request as ready for review September 30, 2026 19:20

@cubic-dev-ai cubic-dev-ai Bot 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.

All reported issues were addressed across 4 files

Shadow auto-approve: would not auto-approve because issues were found.

Re-trigger cubic

Comment thread unstructured/partition/docx.py Resolved

@cubic-dev-ai cubic-dev-ai Bot 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.

0 issues found across 1 file (changes from recent commits).

Shadow auto-approve: would not auto-approve. Auto-approval blocked by 2 unresolved issues from previous reviews.

Re-trigger cubic

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

@cubic-dev-ai cubic-dev-ai Bot 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.

0 issues found across 2 files (changes from recent commits).

Shadow auto-approve: would not auto-approve. Auto-approval blocked by 2 unresolved issues from previous reviews.

Re-trigger cubic

@aadland6

aadland6 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Review of Unstructured-IO/unstructured PR #4508

Findings

[P1 · High] Keep counters per numId, not per abstract definition

Location: unstructured/partition/docx.py:1135

_counters is keyed by abstract_id, so two distinct w:num instances that share one w:abstractNum also share their count. A numId identifies the numbering instance applied to a paragraph; the abstract definition supplies its properties (Open XML numId reference). This corrupts labels in ordinary documents with two separate lists using the same format, and a start override in one list can affect the other. Failure case: for paragraphs using numIds 1, 1, 2, 2, 1, with both instances based on the same abstract definition, the new renderer returns 1., 2., 3., 4., 5.; the independent sequences should be 1., 2., 1., 2., 3.. I reproduced the returned values by running the PR's _ListLabels implementation against a generated python-docx document.

[P2 · Medium] Resolve a style-linked level before using the style's ilvl

Location: unstructured/partition/docx.py:1126

The w:pStyle lookup runs only when ilvl is absent. For numbering supplied by a paragraph style, Open XML says the level in that style's w:numPr is ignored and the abstract level associated through w:pStyle is used (Open XML pStyle reference). Failure case: with Child linked to level 1 (lowerLetter, %2)) and its style numPr containing ilvl=0, the renderer returns 1. instead of a). I reproduced this with the PR's _ListLabels implementation and a generated python-docx document. Styled multilevel lists will receive the wrong label and counter level.

[P2 · Medium] Reapply startOverride when a child level restarts

Location: unstructured/partition/docx.py:1137

_restarted permits startOverride only on the first use of a level, then a parent-level item clears the child's counter and the next child takes the abstract level's w:start. Open XML specifies that startOverride applies both initially and whenever the level restarts (Open XML startOverride reference). Failure case: for levels 0, 1, 1, 0, 1, 1 with level 1's override set to 5, the renderer returns 1., 5., 6., 2., 1., 2.; the labels should be 1., 5., 6., 2., 5., 6.. I reproduced the returned values with the PR's _ListLabels implementation and a generated python-docx document.

@cubic-dev-ai cubic-dev-ai Bot 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.

0 issues found across 2 files (changes from recent commits).

Shadow auto-approve: would not auto-approve. This PR does not meet the repository auto-approval settings.

Re-trigger cubic

@qued

qued commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Review of Unstructured-IO/unstructured PR #4508 — [P1] per-numId counters, [P2] style-linked level, [P2] startOverride on restart

@aadland6 thanks for the review. I checked each point in Word for Mac with minimal DOCX files:

  1. Counters per numId — not changed. Two w:num instances sharing one abstractNum, no override, render 1. 2. 3. 4. in Word, so one counter per abstractNum matches Word. Pinned by test_partition_docx_continues_counting_across_num_ids_sharing_a_definition.
  2. Style-linked level — not changed. A style whose numPr says ilvl=0 renders 1. 2. 3. in Word, i.e. the style's own level is used rather than the level linked via w:pStyle. I briefly implemented the linked-level behavior in 4acceb3 and reverted it in eea544d. Pinned by test_partition_docx_uses_the_level_a_style_names_over_the_one_a_definition_links.
  3. startOverride on restart — fixed in 4acceb3. Word renders 1. 5. 6. 2. 5. 6. for levels 0, 1, 1, 0, 1, 1 with a level-1 override of 5. Pinned by test_partition_docx_restarts_a_child_level_at_its_override_value_each_time.

@cubic-dev-ai cubic-dev-ai Bot 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.

0 issues found across 2 files (changes from recent commits).

Shadow auto-approve: would not auto-approve. This PR does not meet the repository auto-approval settings.

Re-trigger cubic

@aadland6
aadland6 self-requested a review October 1, 2026 20:54
@qued
qued enabled auto-merge October 1, 2026 21:03
@qued
qued added this pull request to the merge queue Oct 1, 2026
Merged via the queue into main with commit d9f976e Oct 1, 2026
55 checks passed
@qued
qued deleted the qued/docx-numbered-list-labels branch October 1, 2026 21:44
linhongyu510 pushed a commit to linhongyu510/unstructured that referenced this pull request Oct 20, 2026
… section-break paragraphs (Unstructured-IO#4538)

## What

Follow-up to Unstructured-IO#4508, bringing `partition_docx()` closer to what Word
renders for auto-numbered paragraphs:

- A numbered paragraph of any element type now carries its label in the
text, not only list items. Numbered headings come out as, for example,
`Title "4.2. Overview"`. Bullets stay unprefixed.
- A numbered heading is no longer classified as a `ListItem`, whether
its number comes from its style or from the paragraph's own numbering. A
heading is a paragraph in a title-mapped style (`Heading 1`, `Title`,
and so on) or whose style chain or own properties give it an outline
level of 0 to 8. It still advances the list counter, as it does in Word.
Paragraphs in other styles that carry their own numbering, such as `List
Paragraph` or `Caption`, stay `ListItem`s.
- An empty paragraph that only holds a section break no longer consumes
a list number. Word does not number it, so every later item in the list
was shifted by one (for example `11.3.2.` came out as `11.3.3.`).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
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.

2 participants