Trailing whitespace-only run strips bold/italic formatting from the whole DOCX paragraph
Bug
In the DOCX backend, a paragraph whose final run is whitespace-only loses the formatting (bold, italic, underline, strikethrough) of all the text before it.
Word routinely emits a trailing run holding nothing but spaces, so this affects a large share of real-world .docx files: a fully-bold heading or sentence is exported with bold=False.
In MsWordDocumentBackend._get_paragraph_elements (docling/backend/msword_backend.py), runs are grouped by format. Two variables track formatting:
previous_format— the format of the run that opened the current group. Guarded bylen(text.strip()), so whitespace-only runs never change it.last_format— reassigned unconditionally on every run, including whitespace-only ones.
The intermediate groups are flushed with previous_format (correct), but the final group is flushed with last_format. When the last run is whitespace-only these two disagree, and the final flush takes the whitespace run's format instead of the group's.
The same path drops italic/underline/strikethrough, and it isn't limited to whitespace — any final run failing the len(text.strip()) guard triggers it. It also works in reverse: a bold whitespace-only trailing run makes plain text come out bold=True.
Steps to reproduce
from docx import Document
from docling.backend.msword_backend import MsWordDocumentBackend
from docling.datamodel.base_models import InputFormat
from docling.datamodel.document import InputDocument
doc = Document()
# Bold text followed by a plain whitespace-only run
para = doc.add_paragraph()
para.add_run("All bold text").bold = True
para.add_run(" ")
# Control: same text, no trailing run
para = doc.add_paragraph()
para.add_run("All bold text").bold = True
doc.save("bold_ws.docx")
in_doc = InputDocument(
path_or_stream="bold_ws.docx",
format=InputFormat.DOCX,
backend=MsWordDocumentBackend,
)
result = MsWordDocumentBackend(in_doc=in_doc, path_or_stream="bold_ws.docx").convert()
for item in result.texts:
print(repr(item.text), "->", item.formatting.bold)
Actual output:
'All bold text' -> False # paragraph WITH the trailing " " run (wrong)
'All bold text' -> True # control, no trailing run (correct)
Expected: both report bold=True. The whitespace-only run carries no visible text and must not redefine the formatting of the text preceding it.
Docling version
2.112.0
Python version
Python 3.13.6
Thanks for the detailed write-up, @DanielNg0729. I can confirm the bug exists exactly as you described.
The issue is on line 1216 of msword_backend.py — the final group flush uses last_format (which gets updated unconditionally on every run, including whitespace-only ones) instead of previous_format (which correctly tracks the format of the run that opened the current group) [1].
The fix is straightforward — change the final flush to use previous_format:
# Current (buggy):
if len(group_text.strip()) > 0:
paragraph_elements.append((group_text.strip(), last_format, None))
# Fixed:
if len(group_text.strip()) > 0:
paragraph_elements.append((group_text.strip(), previous_format, None))
This was inadvertently introduced in PR #3280 [2], which added the last_format variable as part of a larger refactoring to support SDT content extraction. I don't see any existing PR or issue tracking this fix yet.
To reply, just mention @dosu.
Docs are dead. Just use Dosu.