Load Template Documents from File or Stream

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.

The main class involved is:

Here are the steps to load and use templates:

  • 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:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

# Template and output file paths
template_path = "invoice-summary.docx"
output_path = "invoice-summary-report.docx"

# Sample data
invoice_data = {"CustomerName": "Quayside Bistro", "Amount": 552.00}

# Assemble document from file path
assembler = DocumentAssembler()
assembler.assemble_document(template_path, output_path, DataSourceInfo(invoice_data, "invoice"))

print("Document assembled successfully.")

The following sample file is used in this example: invoice-summary.docx

Binary file (DOCX, 7 KB)

Download full output

Load Template from Stream

When working with templates in memory or from remote sources, use streams:

import io
from datetime import datetime

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

# Load template into a memory stream
with open("report-cover.docx", "rb") as f:
    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 mode
with io.BytesIO(template_bytes) as template_stream, open("report-cover-report.docx", "wb") as output_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

Binary file (DOCX, 7 KB)

Download full output

Load Template from Remote Source

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:

import io
import pathlib
from urllib.request import urlopen

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

# Any URL urllib can open: https://... on your server, here the template's file: URL
template_url = pathlib.Path("company-letter.docx").resolve().as_uri()

# Download template from the URL
response = urlopen(template_url)
try:
    template_bytes = response.read()
finally:
    response.close()

with io.BytesIO(template_bytes) as template_stream, io.BytesIO() as output_stream:
    data = {"Company": "Harbor Lane Provisions", "Year": 2015}

    assembler = DocumentAssembler()
    assembler.assemble_document(template_stream, output_stream, DataSourceInfo(data, "company"))

    with open("company-letter-report.docx", "wb") as f:
        f.write(output_stream.getvalue())

print("Document assembled from remote template.")

The following sample file is used in this example: company-letter.docx

Binary file (DOCX, 7 KB)

Download full output

Mixed File and Stream Usage

To read the template from disk and keep the report in memory, open the template file as a binary stream:

import io

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

# Template from a file, opened as a stream
with open("item-label.docx", "rb") as template_stream, io.BytesIO() as output_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()

with open("item-label-report.docx", "wb") as f:
    f.write(output_bytes)

print("Document assembled with mixed file/stream usage.")

The following sample file is used in this example: item-label.docx

Binary file (DOCX, 7 KB)

Download full output

Warning
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:

Free Online Apps

Along with the full-featured library, we provide simple but powerful free online apps.

To assemble documents from templates and data sources, you can use the online apps from the GroupDocs.Assembly App Product Family.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.