GroupDocs.Assembly for Python via .NET allows you to generate output documents in a different format than the template. For example, you can create a DOCX template but output a PDF, or create an XLSX template but output a PDF. With file paths, the output format is determined by the file extension of the output path; with streams, LoadSaveOptions names it.
The main class involved is:
DocumentAssembler - assembles documents and converts to output format
Here are the steps to generate output in a different format:
Create a template document in one format (e.g., DOCX)
Specify an output path with a different file extension (e.g., .pdf, .html)
Assemble the document using DocumentAssembler
The output will be automatically converted to the specified format
Note
Format conversion is automatic based on the output file extension. Supported conversions depend on the template format - Word templates can convert to PDF, HTML, and more, while spreadsheet templates have more limited conversion options.
Convert DOCX Template to PDF
Generate a PDF from a Word template:
fromdatetimeimportdatetimefromdecimalimportDecimalfromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembler# DOCX template, PDF outputtemplate_path="customer-invoice.docx"output_path="customer-invoice-report.pdf"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("PDF generated from DOCX template.")
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembler# DOCX template, XPS outputtemplate_path="price-notice.docx"output_path="price-notice-report.xps"document_data={"Title":"Price Notice","Content":"From 1 March 2015, Chestnut honey is 7.80 per jar.",}assembler=DocumentAssembler()assembler.assemble_document(template_path,output_path,DataSourceInfo(document_data,"document"))print("XPS generated from DOCX template.")
The following sample file is used in this example: price-notice.docx
Generate a PDF from an email template (an EML message here; an MSG template works the same way):
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembler# EML template, PDF outputtemplate_path="invoice-notice.eml"output_path="invoice-notice-report.pdf"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("PDF generated from email template.")
A stream has no file extension, so the report keeps the template’s format unless LoadSaveOptions names another one. Pass it before the data sources:
importiofromdatetimeimportdatetimefromdecimalimportDecimalfromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembler,FileFormat,LoadSaveOptionsinvoice_data={"CustomerName":"Lantern Street Café","Amount":Decimal("522.00"),"Date":datetime(2015,2,3),}withopen("customer-invoice.docx","rb")astemplate_stream,io.BytesIO()asoutput_stream:assembler=DocumentAssembler()assembler.assemble_document(template_stream,output_stream,LoadSaveOptions(FileFormat.PDF),DataSourceInfo(invoice_data,"invoice"))withopen("customer-invoice-from-stream.pdf","wb")asf:f.write(output_stream.getvalue())print("PDF generated from a DOCX template stream.")
Not all format combinations are supported. Word processing templates have the most conversion options, while spreadsheet and presentation templates have more limited output formats.
Advanced Usage Topics
To learn more about format-specific options, conversion settings, and advanced format 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: