Repository navigation
Conversation
|
✅ DCO Check Passed Thanks @loickouao, all your commits are properly signed off. 🎉 |
Merge Protections🟢 Merge protection satisfied — ready to merge. Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
| return etree.tostring(root, xml_declaration=True, encoding="UTF-8", standalone=True) | ||
|
|
||
|
|
||
| def _remove_xml_comments(docx_obj: DocxDocument) -> None: |
There was a problem hiding this comment.
Comments should not be ignored but can be stored with a ContentLayer attribute (eg NOTES)
There was a problem hiding this comment.
Thanks for the review. The title was ambiguous, my fault: this PR removes XML comment nodes (<!-- ... -->, lxml etree._Comment) and processing instructions, not Word comments. I've retitled the PR and renamed the helper to _remove_xml_comment_nodes to make that clear.
Word comments (w:comment in word/comments.xml, anchored by w:commentRangeStart / w:commentReference) are elements, not comment nodes, so they are untouched: _add_comments still stores them in the NOTES layer with their links, and test_comments_extraction passes. I also checked tests/data/docx/sources/docx_comments.docx with <!-- Created by docx4j 8.2.8 (Apache licensed) --> injected into word/document.xml and inside a w:comment in word/comments.xml:
- on main, the conversion fails with
ValueError: Invalid input tag; - with this PR, the 3 reviewer comments are in NOTES, linked to the same 3 items, and the whole output (markdown, NOTES, links) matches main on the unmodified file.
The XML comment nodes themselves are producer metadata, not document content: LibreOffice doesn't show them and drops them on re-save, while it keeps the reviewer comments. Storing them in NOTES would add the docx4j banner to every file docx4j produces. If you'd still like them kept, I can add each one as a text item in the NOTES layer instead.
There was a problem hiding this comment.
Update: I saw your comment on #4564 ("not a fan of omitting text, unless it is format related"), so I went ahead. The text of XML comments in the main document part now goes to the NOTES layer, after the body. Processing instructions are still dropped, since they are format related, and so are comments in the other parts (styles, settings, …). test_xml_comment_text_goes_to_notes covers it.
|
@chriscoveries @loickouao @PeterStaar-IBM: #4564 and #4584 fix the same crash. XML comment and processing-instruction nodes in a DOCX part are not elements, so
Proposal: continue with #4584 as the single fix, and close #4564, with credit to @chriscoveries for the first fix and the test case. Before #4584 can merge, it still needs:
@chriscoveries: if the body-level test in #4564 covers a case that #4584 does not, please say so here, and it can move into #4584. |
docx4j writes an XML comment at the start of the document body. The backend reads the tag of every child it walks, which a comment does not have, so lxml raised "ValueError: Invalid input tag" and the whole conversion failed. A comment inside a table cell or row dropped the table's text instead. Remove comments and processing instructions from every XML part once the document is loaded, keeping any text that follows them. Signed-off-by: loickouao <diegoloic@hotmail.fr>
The helper only removes <!-- --> nodes and processing instructions. Word comments (w:comment) are elements and still go to the NOTES layer. Signed-off-by: loickouao <diegoloic@hotmail.fr>
XML comments in the main document part are no longer dropped: their text is added to the NOTES layer, after the body. Processing instructions and comments in the other parts are still removed. Signed-off-by: loickouao <diegoloic@hotmail.fr>
Use the public XmlPart.element instead of probing for the private _element attribute, as AGENTS.md asks. In python-docx only XmlPart parts have an element, so the same parts are cleaned. Signed-off-by: loickouao <diegoloic@hotmail.fr>
053210d to
543c6c2
Compare
AI triage (advisory)Checked commit The PR adds Topic labels added: CI safety: no concern found. A maintainer must still approve CI. Possible duplicates
1 related pull request(s) in the same code
The |
|
@dolfim-ibm thanks, both points are done:
The |
An XML comment or processing instruction in
word/document.xmleither fails the whole DOCX conversion withValueError: Invalid input tag, or silently drops a table's text (see #4583). docx4j writes such a comment at the start of the body in every file it produces.The backend reads the tag of every child it walks in several places, and a comment does not have one. Guarding each of those places would be fragile, so this PR takes comments and processing instructions out of every XML part (
XmlPart) once python-docx has loaded it. The text of the XML comments in the main document part is kept in the NOTES layer, after the body. Processing instructions, and comments in the other parts (styles, settings, …), are dropped. Any text that follows a removed node is kept. Word comments (w:commentelements) are not affected and still go to NOTES as before.Supersedes #4564 by @chriscoveries, which first fixed the crash by skipping non-element nodes at the top level of the body. This PR also covers comments inside paragraphs, runs, table cells and table rows: 4 of the 6 cases below still fail on #4564's branch. The test from #4564 (a comment and a processing instruction between two body paragraphs) passes unchanged on this branch.
Tests:
test_xml_comments_and_processing_instructions_keep_body_textcovers six positions:Each one checks that every text, table cells included, is in the output. All six fail on main.
test_xml_comment_text_goes_to_noteschecks that the comment text is in NOTES and not in the body, that the text after an inline comment is kept, and that processing instructions are dropped.Checks (rebased on
mainat 6fadbae):.docxfiles undertests/datagive byte-identicalexport_to_dict()onmainand on this branch.export_to_dict()is identical to that of the same files with the comments removed by hand, apart from the docx4j banner, which is in NOTES.tests/test_backend_msword*.pyandtests/test_office_image_modes.pypass.main.Issue resolved by this Pull Request:
Resolves #4583
Checklist: