Load Template Documents from File or Stream
Leave feedback
Overview
GroupDocs.Assembly for Python via .NET supports loading template documents from both file paths and streams. This flexibility allows you to work with templates stored on disk, in memory, or retrieved from remote sources.
Load a template from a file path by passing the path as a string
Load a template from a stream by passing a binary file object or an io.BytesIO
Choose the appropriate form based on your use case (file system vs memory/remote)
Save the assembled document to a file path or to a stream accordingly
Note
assemble_document takes the template and the report either both as paths or both as streams. A stream is any binary file object: a file opened with open(path, "rb") or an io.BytesIO. The assembler leaves the streams you pass open, so manage their lifetime yourself, preferably with a with block.
Load Template from File Path
The simplest way to load a template is from a file path:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembler# Template and output file pathstemplate_path="invoice-summary.docx"output_path="invoice-summary-report.docx"# Sample datainvoice_data={"CustomerName":"Quayside Bistro","Amount":552.00}# Assemble document from file pathassembler=DocumentAssembler()assembler.assemble_document(template_path,output_path,DataSourceInfo(invoice_data,"invoice"))print("Document assembled successfully.")
When working with templates in memory or from remote sources, use streams:
importiofromdatetimeimportdatetimefromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembler# Load template into a memory streamwithopen("report-cover.docx","rb")asf:template_bytes=f.read()data={"Title":"Quarterly stock report","Date":datetime(2015,3,31)}# The report goes to a file opened for writing in binary modewithio.BytesIO(template_bytes)astemplate_stream,open("report-cover-report.docx","wb")asoutput_stream:assembler=DocumentAssembler()assembler.assemble_document(template_stream,output_stream,DataSourceInfo(data,"report"))print("Document assembled from stream successfully.")
The following sample file is used in this example: report-cover.docx
You can load templates from URLs or other remote sources: read the response’s bytes into an io.BytesIO. To keep the example runnable, it opens the template’s own file: URL; pass your https:// address the same way:
importioimportpathlibfromurllib.requestimporturlopenfromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembler# Any URL urllib can open: https://... on your server, here the template's file: URLtemplate_url=pathlib.Path("company-letter.docx").resolve().as_uri()# Download template from the URLresponse=urlopen(template_url)try:template_bytes=response.read()finally:response.close()withio.BytesIO(template_bytes)astemplate_stream,io.BytesIO()asoutput_stream:data={"Company":"Harbor Lane Provisions","Year":2015}assembler=DocumentAssembler()assembler.assemble_document(template_stream,output_stream,DataSourceInfo(data,"company"))withopen("company-letter-report.docx","wb")asf:f.write(output_stream.getvalue())print("Document assembled from remote template.")
To read the template from disk and keep the report in memory, open the template file as a binary stream:
importiofromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembler# Template from a file, opened as a streamwithopen("item-label.docx","rb")astemplate_stream,io.BytesIO()asoutput_stream:data={"Name":"Smoked sea salt","Value":100}assembler=DocumentAssembler()assembler.assemble_document(template_stream,output_stream,DataSourceInfo(data,"item"))# Use the output stream (e.g., send via HTTP, save to database)output_bytes=output_stream.getvalue()withopen("item-label-report.docx","wb")asf:f.write(output_bytes)print("Document assembled with mixed file/stream usage.")
The following sample file is used in this example: item-label.docx
assemble_document has no form that takes a path for one side and a stream for the other: assemble_document("item-label.docx", io.BytesIO(), ...) raises TypeError (“no overload matched”). Open the file as a stream instead, as above. After the call, the output BytesIO is positioned at 0, and getvalue() returns the whole document.
Advanced Usage Topics
To learn more about working with different template formats, custom document loading, and advanced stream handling, please refer to the advanced usage section.
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: