Skip to content

Accept .dotx and .dotm Word templates #83

Description

@toxicphreAK

Document() accepts .docx and, since f49bdd1, macro-enabled .docm. Word template files — .dotx and .dotm — are still rejected, because their main-part content types are not in _WORD_MAIN_CONTENT_TYPES in docx/api.py.

Why it matters

Templates are the natural starting point for document generation: an organisation keeps its house styles, headers and numbering in a .dotx, and a pipeline should be able to open it, fill it in, and save a .docx. Today that requires renaming the file and hoping, or opening the template in Word once to convert it.

The markup inside a template is identical to a document — only the content type differs — so this is a small change, exactly parallel to the .docm support already merged.

Upstream python-openxml#363 (16 comments, open since 2017) and python-openxml#1532, with unmerged PR python-openxml#1537.

Scope

  • Add the template content types to _WORD_MAIN_CONTENT_TYPES in docx/api.py:
    application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml and the macro-enabled template equivalent
  • Decide save behaviour, which is the only real design question: saving a .dotx unchanged keeps the template content type, so the output is still a template. Generating a document from a template means rewriting the main part content type on save. Both are legitimate; the API should let the caller choose rather than picking silently
  • Check whether the [Content_Types].xml override and the docProps need adjusting when converting template → document
  • Follow the .docm commit as the model, including its test approach
  • Unit tests for open, save-as-template and save-as-document, plus fixture files
  • Update the Document() docstring, which currently names only .docx and .docm

Notes

Related: #23 (Word default template) concerns the bundled default.docx, which is a different problem — this issue is about opening user-supplied templates.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions