Skip to content

Enhance Subdoc Functionality to Support In-Memory docx.Document as Initialization Method for Subdoc Objects #601

Closed

Description

@aiprogramming

Current Workflow:

  1. Read the subdoc_template template, then call render().
  2. Save the rendered result to a temporary file or BytesIO.
  3. Use main_template.new_subdoc(temporary_file_or_byteio_object).

Desired Workflow:

  1. Read the subdoc_template template, then call render().
  2. Use main_template.new_subdoc(subdoc_template.docx) directly.

Implementation Idea:
Modify the logic in Subdocx.init as follows (variable names could also be refined for elegance):
self.subdocx = docpath if isinstance(docpath, docx.DocumentObject) else Document(docpath)

Advantages:
Users can construct the docx.Document in memory or pass the rendered subdoc_template directly to main_template without additional I/O operations.

Activity

  1. janmarsino98 commented on Jul 14, 2026

    @janmarsino98

    I reproduced the current behavior with a python-docx Document created in memory. Passing it to new_subdoc() currently reaches Document(docpath) and fails with:

    AttributeError: 'Document' object has no attribute 'seek'
    

    A backward-compatible approach would be to import docx.document.Document as the concrete document class and, in Subdoc.__init__, use the passed object directly only when it is an instance of that class. Existing paths and file-like streams would still go through Document(docpath) unchanged; new_subdoc() without an argument would keep its current behavior.

    I can add a focused test using BytesIO for the main template and output, plus a Document() source, so the whole new path is exercised without temporary files. Would you be happy with that API extension and test before I prepare a PR?

  2. cvas22 commented on Nov 23, 2026

    @cvas22

    @aiprogramming @janmarsino98 fyi, I'm adding this to Docform (the maintained fork at https://github.com/cvas22/docform, docform on PyPI) in cvas22/docform#195. new_subdoc() takes an in-memory Document or a rendered DocxTemplate there, so you can switch over once it's released.

  3. cvas22 commented on Nov 23, 2026

    @cvas22

    @aiprogramming @janmarsino98 this is merged in Docform now (cvas22/docform#195) and will be in the next docform release.

  4. aiprogramming commented on Nov 23, 2026

    @aiprogramming
    Author

    Migrate from python-docx-template to DocForm, as DocForm offers significantly better performance and the original library is no longer actively maintained.

  5. cvas22 commented on Nov 23, 2026

    @cvas22

    @aiprogramming I ran some benchmarks comparing docform 1.0.2 and main with docxtpl 0.20.2 and master. Median of 7 runs, times in ms:

    Case docform main docform 1.0.2 docxtpl master docxtpl 0.20.2
    30 paragraphs 33 32 35 32
    1000 paragraphs 411 410 393 405
    same, sandbox off 458 401 390 476
    1000-row table loop 94 92 95 141
    30 subdocs from streams 637 570 640 697
    30 in-memory subdocs 1276 1227 1267 1329

    So docform performs about the same as python-docx-template. Plain rendering matches in all of them, and the sandbox doesn't add anything noticeable. The slow part is subdocs, which cost about 20-30ms each in both libraries, mostly python-docx parsing the subdoc XML. If that's what you're hitting I'm looking at making that path faster. If it's something else, a small script that shows it would help a lot, thx.

  6. cvas22 commented on Nov 23, 2026

    @cvas22

    @aiprogramming I opened cvas22/docform#202 to speed up exactly that path. Inserting a rendered sub-template or an in-memory Document goes from about 23ms to 4ms per subdoc, so your 30-subdoc case drops to roughly half the total time.

  7. cvas22 commented on Nov 23, 2026

    @cvas22

    @aiprogramming the speedup is merged in Docform now (cvas22/docform#202). Inserting a rendered sub-template or an in-memory Document with new_subdoc() went from about 23ms to 4ms per subdoc. Same benchmark as before, main vs the fix:

    Case before after
    1000 paragraphs 376ms 375ms
    1000-row table loop 85ms 85ms
    30 subdocs from streams 528ms 468ms
    30 in-memory Documents 1153ms 534ms
    30 rendered sub-templates 1176ms 568ms

    The subdoc rows include building the subdocs themselves, so most of what's left is your own rendering. It'll be in the next docform release.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions