Skip to content

fix: apply Heading style from BASE_STYLE_MAP level in Word output#5174

Open
Shengshenlan wants to merge 1 commit into
PaddlePaddle:release/3.7from
Shengshenlan:fix/word-heading-level
Open

fix: apply Heading style from BASE_STYLE_MAP level in Word output#5174
Shengshenlan wants to merge 1 commit into
PaddlePaddle:release/3.7from
Shengshenlan:fix/word-heading-level

Conversation

@Shengshenlan

Copy link
Copy Markdown

Problem

BASE_STYLE_MAP in word_converter.py already defines a level field for title-type blocks:

BASE_STYLE_MAP = {
    "doc_title":       {"level": 0, "size": 20, "bold": True, "align": CENTER},
    "content_title":   {"level": 1, "size": 14, "bold": True, "align": LEFT},
    "reference_title": {"level": 1, "size": 14, "bold": True, "align": LEFT},
    "paragraph_title": {"level": 2, "size": 14, "bold": True, "align": LEFT},
    ...
}

However, _set_paragraph_style() never reads level — it only applies size, bold, align, and indent. As a result, all paragraphs in the generated .docx are Normal style, including detected titles. Word's navigation pane, outline view, and auto-generated TOC are all empty.

The level field is defined but never consumed — this looks like a half-finished feature.

Fix

Wire up the level field in _set_paragraph_style:

  • Added a doc parameter (defaults to None, so existing callers are unaffected)
  • When config["level"] is present and doc is available, assign the corresponding Word style:
    • level == 0Title
    • level == NHeading N
  • Updated the 3 call sites in _write_block to pass doc

Before / After

Tested on a real 3-page tender document (中文招标文件):

Before (all Normal):

Normal: 11   ← no headings at all

After (headings applied):

第1页: Title: 1    ← "电子化货物采购招标文件"
第2页: Heading 2: 6  ← "第一章招标公告", "1、招标条件", "2、项目基本情况"...
第3页: Heading 2: 2  ← "3.1、投标人应依法设立...", "3.2、本次招标不接受联合体"

Word navigation pane now shows the heading tree, and auto-TOC works.

Backward compatibility

  • doc parameter defaults to None — if not passed, behavior is unchanged (same as before)
  • Style assignment is wrapped in try/except — never breaks if a style is missing

Resolves #4207

BASE_STYLE_MAP already defines a 'level' field for title-type blocks
(doc_title=0, content_title=1, paragraph_title=2), but _set_paragraph_style
never read it - all paragraphs were written as Normal style, so the
generated .docx had no heading hierarchy (empty navigation pane, no TOC).

This wires up the level field: when present and a doc reference is
available, the paragraph is assigned the corresponding Word Heading style
(level 0 -> Title, level N -> Heading N).

Verified on a 3-page real tender document: before patch all paragraphs
were Normal; after patch doc_title -> Title, paragraph_title -> Heading 2.

Resolves PaddlePaddle#4207
@paddle-bot

paddle-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown

Thanks for your contribution!

@paddle-bot paddle-bot Bot added the contributor External developers label Jul 16, 2026
@CLAassistant

CLAassistant commented Jul 16, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor External developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to distiguish between Heading hiarchy?

2 participants