Skip to content

Add guidance to avoid whitespace reformatting in docx/ooxml skill #12

Description

@thejud

Using the docx skill to add a comment to a word doc resulted in a document unreadable by word (and by soffice/libreoffice). Pandoc could extract the content. After some investigation, claude figured out that it was formatting the document incorrectly by adding additional whitespace.

It provided the following additional guidance to refine the ooxml prompt. I'm happy to include this as a PR, but suspect you are extremely particular about tuning the prompt, so I'll provide what is working for me:

For complex scenarios not covered by the library:

**CRITICAL: XML Serialization**
When writing XML back to files after manipulation with defusedxml.minidom, you MUST preserve the original formatting:
- ✅ **USE**: `doc.toxml(encoding='utf-8')` - Preserves original formatting, Word-compatible
- ❌ **NEVER USE**: `doc.toprettyxml()` - Adds extra whitespace that breaks Word compatibility

Microsoft Word is extremely sensitive to XML whitespace. Using `toprettyxml()` adds newlines and indentation that Word interprets as part of the document structure, causing documents to become unreadable. Always use `toxml()` to maintain the compact format Word expects.

and

# CRITICAL: When writing XML manually (not using Document class)
# ✅ CORRECT - preserves formatting:
with open(doc_path, 'wb') as f:
    f.write(doc.toxml(encoding='utf-8'))

# ❌ WRONG - breaks Word compatibility:
with open(doc_path, 'wb') as f:
    f.write(doc.toprettyxml(indent='  ', encoding='utf-8'))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions