Generate Output in Same Format as Template
Leave feedback
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:
DocumentAssembler - assembles documents and determines output format
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:
fromdatetimeimportdatetimefromdecimalimportDecimalfromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembler# Template and output are both DOCXtemplate_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.")
Generate an Excel spreadsheet from an Excel template:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembler# Template and output are both XLSXtemplate_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.")
Generate a PowerPoint presentation from a PowerPoint template:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembler# Template and output are both PPTXtemplate_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.")
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:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembler# Template and output are both EMLtemplate_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
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:
importiofromdatetimeimportdatetimefromdecimalimportDecimalfromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembler# Load DOCX template into a streamwithopen("invoice-letter.docx","rb")asf:template_bytes=f.read()withio.BytesIO(template_bytes)astemplate_stream,io.BytesIO()asoutput_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"))withopen("invoice-letter-from-stream.docx","wb")asf:f.write(output_stream.getvalue())print("Document generated from stream in same format.")
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.