You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enhance Subdoc Functionality to Support In-Memory docx.Document as Initialization Method for Subdoc Objects #601
Read the subdoc_template template, then call render().
Save the rendered result to a temporary file or BytesIO.
Use main_template.new_subdoc(temporary_file_or_byteio_object).
Desired Workflow:
Read the subdoc_template template, then call render().
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.
Reacted by Clyde-la, aiprogramming, Zewei and Srinivas Chippagiri
I reproduced the current behavior with a python-docxDocument 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?
Migrate from python-docx-template to DocForm, as DocForm offers significantly better performance and the original library is no longer actively maintained.
@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.
@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.
@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.
Activity
janmarsino98 commentedon Jul 14, 2026
I reproduced the current behavior with a
python-docxDocumentcreated in memory. Passing it tonew_subdoc()currently reachesDocument(docpath)and fails with:A backward-compatible approach would be to import
docx.document.Documentas the concrete document class and, inSubdoc.__init__, use the passed object directly only when it is an instance of that class. Existing paths and file-like streams would still go throughDocument(docpath)unchanged;new_subdoc()without an argument would keep its current behavior.I can add a focused test using
BytesIOfor the main template and output, plus aDocument()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?cvas22 commentedon Nov 23, 2026
@aiprogramming @janmarsino98 fyi, I'm adding this to Docform (the maintained fork at https://github.com/cvas22/docform,
docformon PyPI) in cvas22/docform#195.new_subdoc()takes an in-memoryDocumentor a renderedDocxTemplatethere, so you can switch over once it's released.cvas22 commentedon Nov 23, 2026
@aiprogramming @janmarsino98 this is merged in Docform now (cvas22/docform#195) and will be in the next
docformrelease.aiprogramming commentedon Nov 23, 2026
Migrate from python-docx-template to DocForm, as DocForm offers significantly better performance and the original library is no longer actively maintained.
cvas22 commentedon Nov 23, 2026
@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:
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.
cvas22 commentedon Nov 23, 2026
@aiprogramming I opened cvas22/docform#202 to speed up exactly that path. Inserting a rendered sub-template or an in-memory
Documentgoes from about 23ms to 4ms per subdoc, so your 30-subdoc case drops to roughly half the total time.cvas22 commentedon Nov 23, 2026
@aiprogramming the speedup is merged in Docform now (cvas22/docform#202). Inserting a rendered sub-template or an in-memory
Documentwithnew_subdoc()went from about 23ms to 4ms per subdoc. Same benchmark as before, main vs the fix:DocumentsThe subdoc rows include building the subdocs themselves, so most of what's left is your own rendering. It'll be in the next
docformrelease.