Skip to content

fix(odf): keep ODT comments out of the body text - #4436

Merged
PeterStaar-IBM merged 3 commits into
docling-project:mainfrom
devYRPauli:fix/odt-annotations-as-comments
Oct 7, 2026
Merged

PeterStaar-IBM merged 3 commits into
docling-project:mainfrom
devYRPauli:fix/odt-annotations-as-comments

Conversation

@devYRPauli

@devYRPauli devYRPauli commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

An ODT comment (office:annotation) sits inline in the element it annotates. Every text path in OdtDocumentBackend reads the author, date and text of the comment as inline text. The comment is spliced into the paragraph, heading, list item or table cell, and no comment reaches the NOTES layer.

The script in the issue, on main and on this branch:

element main this branch
heading ## BudgetAnn Reviewer2024-01-15T10:00:00ARRename this heading ## Budget
paragraph The total is Ann Reviewer2024-01-15T10:00:00ARSource?42 EUR. The total is 42 EUR.
list item - First itemAnn Reviewer2024-01-15T10:00:00ARToo vague - First item
table cell ValCell remark Ann Reviewer 2024-01-15 10:00:00 Val
comment groups 0 4

The fix:

  • _detach_annotations() removes every office:annotation from the parsed body before the walk. It records the paragraph or heading that holds each comment. The tail text stays in place. One change at the parse step covers every text path.
  • An office:annotation-end marker holds no text, so it stays in the body and adds nothing to the output.
  • The code does not call odfdo Annotation.delete(). That method searches the whole body for the matching office:annotation-end, once per comment.
  • A comment inside a footnote body no longer leaks into the footnote text.
  • A comment on text deleted in a tracked change is dropped with that text, the same as on main.
  • Each comment thread becomes a COMMENT_SECTION group comment-<n> in the NOTES layer. The text uses the DOCX form [author: Name (initials), time: date]: text. The time is dc:date as written, as in the PPTX backend.
  • The group is a child of the item made from the paragraph of the comment that starts the thread. That item gets a FineRef to the group in item.comments, as in the DOCX backend. This covers a heading, a paragraph, a list item, a footnote and a paragraph in a rich table cell.
  • A plain table cell is not a node, so its group is a child of the table. A paragraph with a link or mixed formatting makes an InlineGroup. InlineGroup has no comments field, so it gets the child group but no FineRef.
  • When the paragraph of a comment makes no item, the group is a child of the document root. An empty paragraph and a paragraph in a text box make no item. No comment is lost.
  • A reply carries loext:parent-name. It joins the group of the comment it answers, also when the reply comes before that comment. As in the XLSX backend, each comment comes before its replies. Replies to the same comment keep document order. A loext:parent-name cycle still gives one group, and no comment is dropped.
  • This change does not cover ODP slide comments (officeooo:annotation in draw:page).

Tests:

  • New test_odt_annotation_becomes_comment_not_body_text builds the ODT in code, so tests/data does not change. It checks the markdown, the footnote text, the comment groups and their NOTES layer. It checks the parent of each of the 13 comment groups and the FineRef on each text item and table.
  • The test ODT has one reply after the comment it answers and one reply before the comment it answers. Each pair is one group. It also has a reply to a reply, saved before the comments it answers, and a second reply to the first comment. It has a loext:parent-name cycle and two comments with no office:name. It also has a comment inside a footnote and a comment on deleted tracked-change text, which is not emitted. One reply writes its initials in loext:sender-initials.
  • The test ODT has a comment in a heading, a paragraph, a list item, a footnote, a rich table cell and a plain table cell. It also has a comment in a paragraph with a link, in an empty paragraph and in a text box.
  • With the backend of main, the new test fails with The total is Ann Reviewer2024-01-15T10:00:00ARSource?Bo Writer2024-01-15T10:00:00BWAdded below.42 EUR this year.
  • With the backend of the first commit 769ee38f, the new test also fails. That backend puts all 13 comment groups under the document root, with no FineRef.
  • tests/test_backend_opendocument.py: 48 passed on main at 38b6fa0a, 49 passed on a local merge of this branch into that main. The ground truth of the ODT, ODS and ODP samples does not change.
  • The prek hooks pass on both files. ty reports 39 warnings on opendocument_backend.py, and 37 on main. The 2 new warnings are on FineRef(cref=...). ty reports the same 2 warnings on the same call in msword_backend.py.

Issue resolved by this Pull Request:
Resolves #4435

Checklist:

  • Documentation has been updated, if necessary.
  • Examples have been added, if necessary.
  • Tests have been added, if necessary.

An ODT comment (office:annotation) sits inline in the paragraph,
heading, list item or table cell it annotates. Every text path read its
creator, date and body as inline text, so the comment was spliced into
the document text and no comment reached the NOTES layer.

Remove the annotations from the parsed body before the walk. Use the
plain Element.delete(), because odfdo Annotation.delete() searches the
whole body for the matching office:annotation-end once per comment. An
office:annotation-end marker holds no text, so it stays in the body.

Add each comment to a COMMENT_SECTION group in the NOTES layer, in the
'[author: Name (initials), time: date]: text' form of the DOCX backend.
A reply (loext:parent-name) joins the group of the comment it answers,
also when the reply comes before that comment. As in the XLSX backend,
each comment comes before its replies.

The walk drops text deleted in a tracked change, so a comment on that
text is detached but not added to a group, the same as on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Yash Raj Pandey <yashpn62@gmail.com>
@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @devYRPauli, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 Merge protection satisfied — ready to merge.

Show 1 satisfied protection

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

Comment thread docling/backend/opendocument_backend.py Resolved
Comment thread docling/backend/opendocument_backend.py Resolved
devYRPauli and others added 2 commits September 29, 2026 10:39
Each comment thread was a COMMENT_SECTION group at the document root,
so a reader could not tell which item a comment annotates.

_detach_annotations() now keys each thread by the paragraph or heading
that holds its first comment. The walk passes the threads down and adds
them where it makes the item for that paragraph: a heading, a
paragraph, a list item, a table for a plain cell, and a footnote. A
paragraph in a rich table cell is a paragraph. The thread group is a
child of the item in the NOTES layer, and the item gets a FineRef to
the group in its comments, as in the DOCX backend. An InlineGroup has
no comments field, so a paragraph with several runs gets only the child
group.

A thread whose paragraph makes no item goes to the root, so no comment
is lost.

The key is the lxml element under the odfdo wrapper, because odfdo
builds a new wrapper on every access.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Yash Raj Pandey <yashpn62@gmail.com>
The walk skips a text box, so its paragraph makes no item. The thread
of a comment in that paragraph goes to the root after the walk. No
test reached that path before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Yash Raj Pandey <yashpn62@gmail.com>
@codecov

codecov Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.10638% with 14 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
docling/backend/opendocument_backend.py 85.10% 8 Missing and 6 partials ⚠️

📢 Thoughts on this report? Let us know!

@devYRPauli

Copy link
Copy Markdown
Contributor Author

@PeterStaar-IBM The changes requested in your review are addressed in d3212ae and 9f3d88b, and the original review threads are now outdated. When you have a chance, could you please take another look?

@dolfim-ibm dolfim-ibm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Peter's points are addressed: each thread is now a COMMENT_SECTION group in the NOTES layer under the annotated item, with the FineRef back-link as in DOCX. The branch merges cleanly with current main (after the recent ODF chart/page_range changes), and the ODF suite passes on the merge.

Two non-blocking follow-ups:

  • a comment in a paragraph holding only an image lands at the document root instead of under the PictureItem, because _add_odf_paragraph returns early;
  • ODP slide comments (officeooo:annotation) are still out of scope, worth a separate issue.

lgtm

@devYRPauli

Copy link
Copy Markdown
Contributor Author

Agreed on both follow-ups.

  • Image-only paragraph: _add_odf_paragraph returns at line 653 before it attaches the threads, so attach_rest puts them at the document root. The chart branch returns early at line 655 in the same way. I will fix both in a follow-up PR after this one merges.
  • ODP slide comments: I will open a separate issue for officeooo:annotation.

@PeterStaar-IBM PeterStaar-IBM left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm!

@PeterStaar-IBM
PeterStaar-IBM merged commit 8aef089 into docling-project:main Oct 7, 2026
26 checks passed
@devYRPauli

Copy link
Copy Markdown
Contributor Author

Follow-ups: #4650 attaches the comments in image-only and chart-only paragraphs to the picture. #4648 tracks the ODP slide comments.

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.

ODT comments are spliced into the body text

3 participants