Generate Your First Document from a Python Dict

Overview

This guide walks you through creating your first document with GroupDocs.Assembly for Python via .NET. You’ll generate a simple document from an in-memory Python dict, demonstrating the basic workflow of template-based document assembly.

The main classes involved are:

Here are the steps to generate your first document:

  • Describe your data as a Python dict whose keys are the names the template reads
  • Populate the dict with sample data
  • Create a template document (DOCX) with expression tags referencing the dict’s keys
  • Use DocumentAssembler to assemble the template with your data
  • Save the assembled document to a file

Describe the Data as a Dict

In Python you do not declare a class for the data. A dict is enough: each key becomes a member the template can read, and the value’s Python type decides what the template sees (str, int, float, decimal.Decimal, bool, datetime, nested dicts and lists).

person = {
    "Name": "Amara Osei",
    "Age": 41,
    "Email": "amara.osei@harborlane.example",
}

Generate Document from a Dict

Now, pass the dict to the assembler together with the name the template uses for it:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

# Create sample data
person = {
    "Name": "Amara Osei",
    "Age": 41,
    "Email": "amara.osei@harborlane.example",
}

# Paths for template and output
template_path = "person-card.docx"
output_path = "person-card-report.docx"

# Assemble the document
assembler = DocumentAssembler()
assembler.assemble_document(template_path, output_path, DataSourceInfo(person, "person"))

print("Document generated successfully:", output_path)

The following sample file is used in this example: person-card.docx

Binary file (DOCX, 7 KB)

Download full output

Template Document Example

Your template document (person-card.docx) should contain expression tags like this:

Name: <<[person.Name]>>
Age: <<[person.Age]>>
Email: <<[person.Email]>>

After assembly, the output will be:

Name: Amara Osei
Age: 41
Email: amara.osei@harborlane.example

Using Contextual Object Member Access

You can also use contextual member access, which allows you to omit the data source name in simple scenarios:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

person = {
    "Name": "Lena Park",
    "Age": 29,
    "Email": "lena.park@harborlane.example",
}

assembler = DocumentAssembler()
# When using contextual access, you can omit the data source name
assembler.assemble_document("person-card-contextual.docx", "person-card-contextual-report.docx",
                            DataSourceInfo(person))

The following sample file is used in this example: person-card-contextual.docx

Binary file (DOCX, 7 KB)

Download full output

With contextual access, your template can use:

Name: <<[Name]>>
Age: <<[Age]>>
Email: <<[Email]>>
Note
When using contextual object member access, the template expressions can reference the dict’s keys directly without the data source name prefix. This is useful for simple, single-object scenarios. A template that still names the source (<<[person.Name]>>) fails when the source has no name: the engine looks for a person key in the dict.

Advanced Usage Topics

To learn more about working with complex data structures, multiple data sources, and advanced template syntax, 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.