Generate Output in Same Format as Template

Overview

By default, GroupDocs.Assembly for Python via .NET saves assembled documents in the same format as the template. This means a DOCX template produces a DOCX output, an XLSX template produces an XLSX output, and so on. With file paths, the output format is determined by the file extension of the output path.

The main class involved is:

Here are the steps to generate output in the same format:

  • Create a template document in your desired format (DOCX, XLSX, PPTX, etc.)
  • Specify an output path with the same file extension as the template
  • Assemble the document using DocumentAssembler
  • The output will automatically be in the same format as the template
Note
The output format is automatically determined by the file extension of the output path. No additional configuration is needed to preserve the template format.

Generate DOCX from DOCX Template

Generate a Word document from a Word template:

from datetime import datetime
from decimal import Decimal

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

# Template and output are both DOCX
template_path = "invoice-letter.docx"
output_path = "invoice-letter-report.docx"

invoice_data = {
    "CustomerName": "Quayside Bistro",
    "Amount": Decimal("552.00"),
    "Date": datetime(2015, 1, 14),
}

assembler = DocumentAssembler()
assembler.assemble_document(template_path, output_path, DataSourceInfo(invoice_data, "invoice"))

print("DOCX document generated from DOCX template.")

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

Binary file (DOCX, 7 KB)

Download full output

Generate XLSX from XLSX Template

Generate an Excel spreadsheet from an Excel template:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

# Template and output are both XLSX
template_path = "monthly-sales.xlsx"
output_path = "monthly-sales-report.xlsx"

report_data = {
    "Title": "Monthly Sales",
    "Month": "January 2015",
}

assembler = DocumentAssembler()
assembler.assemble_document(template_path, output_path, DataSourceInfo(report_data, "report"))

print("XLSX document generated from XLSX template.")

The following sample file is used in this example: monthly-sales.xlsx

Binary file (XLSX, 7 KB)

Download full output

Generate PPTX from PPTX Template

Generate a PowerPoint presentation from a PowerPoint template:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

# Template and output are both PPTX
template_path = "quarterly-review.pptx"
output_path = "quarterly-review-report.pptx"

presentation_data = {
    "Title": "Quarterly Review",
    "Quarter": "Q1 2015",
}

assembler = DocumentAssembler()
assembler.assemble_document(template_path, output_path, DataSourceInfo(presentation_data, "presentation"))

print("PPTX document generated from PPTX template.")

The following sample file is used in this example: quarterly-review.pptx

Binary file (PPTX, 30 KB)

Download full output

Generate an Email from an Email Template

Generate an email message from an email template. The example uses an EML template, a MIME message you can write in any text editor; an Outlook MSG template is assembled the same way, into an .msg output path:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

# Template and output are both EML
template_path = "invoice-email.eml"
output_path = "invoice-email-report.eml"

email_data = {
    "Recipient": "Quayside Bistro <orders@quaysidebistro.example>",
    "Subject": "Invoice for order 10001",
    "Body": "Your invoice for order 10001 comes to 552.00 and is due by 13 February 2015.",
}

assembler = DocumentAssembler()
assembler.assemble_document(template_path, output_path, DataSourceInfo(email_data, "email"))

print("EML document generated from EML template.")

The following sample file is used in this example: invoice-email.eml

Binary file (EML, 855 bytes)

Download full output

In the template, tags work in the subject and the body. In the To: header, the tag sits in a quoted display name and is replaced by the whole recipient address:

To: "<<[email.Recipient]>>" <customer@harborlane.example>
Subject: <<[email.Subject]>>

Using Streams with Same Format

When using streams, the format is still preserved based on the template format:

import io
from datetime import datetime
from decimal import Decimal

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

# Load DOCX template into a stream
with open("invoice-letter.docx", "rb") as f:
    template_bytes = f.read()

with io.BytesIO(template_bytes) as template_stream, io.BytesIO() as output_stream:
    data = {"CustomerName": "Lantern Street Café", "Amount": Decimal("522.00"), "Date": datetime(2015, 2, 3)}

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

    with open("invoice-letter-from-stream.docx", "wb") as f:
        f.write(output_stream.getvalue())

print("Document generated from stream in same format.")

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

Binary file (DOCX, 7 KB)

Download full output

Format Compatibility

The following formats support same-format output:

  • Word Processing: DOCX, DOC, RTF, ODT, OTT
  • Spreadsheet: XLSX, XLS, XLSM, ODS
  • Presentation: PPTX, PPT, PPTM, ODP
  • Email: MSG, EML, EMLX
  • Other: HTML, TXT, XML
Warning
Some older formats (like DOC, XLS, PPT) may have limitations compared to their newer counterparts (DOCX, XLSX, PPTX). For best results, use the newer Office Open XML formats.

Advanced Usage Topics

To learn more about format conversion, saving to different formats, and format-specific options, please refer to Generate Output in Different Format and 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.