Insert template to existing document

Hi,

Need some help.
We have templates from Microsoft office available at "C:\Program Files\Microsoft Office\Templates"
under this particular path we have folders for each template(.dotm) - macro enabled template

when user open these templates which contain macro instructions, document picks the user details who has opened the document template and also system date and insert them into template where needed
These template also contain logos present under same templates folder

we are currently using the below code, following items are missing when we use

  1. User who is openning the document, say document author, date, footer notes/ endnotes if present
  2. Company logo, date present in header section of macro enabled template
  3. styles present in template, like font name, font size, font color, font shading for each style

Help us inserting a complete template to document(target) with all information from template intact

import aspose.words as aw

# Load the target DOCX document
doc = aw.Document(TARGET_PATH)

# Load the DOTM template document
template = aw.Document(TEMPLATE_PATH)


for i in range(template.sections.count - 1, -1, -1):
    section = template.sections[i]
    imported_section = doc.import_node(section, True)  # True to import children nodes
    doc.sections.insert(0, imported_section)

doc.update_fields()
doc.update_page_layout()
# Save the modified document
doc.save(OUTPUT_PATH)

after insert

template details

@Prakruth

To insert a complete template into an existing document while preserving all necessary information such as user details, logos, and styles, follow these steps:

  1. Load the Target Document and Template: Ensure you load both the target DOCX document and the DOTM template correctly.

  2. Import Sections: When importing sections from the template, include all child nodes to retain formatting and styles.

  3. Update Fields: After merging, update the fields to reflect any dynamic content like dates and user information.

  4. Handle Logos and Styles: Ensure that logos are correctly referenced and that styles are applied as per the template.

Here’s an updated version of your code that addresses these points:

import aspose.words as aw

# Load the target DOCX document
doc = aw.Document(TARGET_PATH)

# Load the DOTM template document
template = aw.Document(TEMPLATE_PATH)

# Import sections from the template
for i in range(template.sections.count - 1, -1, -1):
    section = template.sections[i]
    imported_section = doc.import_node(section, True)  # True to import child nodes
    doc.sections.insert(0, imported_section)

# Update fields to reflect user details and dates
doc.update_fields()

# Update page layout to ensure everything is rendered correctly
doc.update_page_layout()

# Save the modified document
doc.save(OUTPUT_PATH)

Additional Considerations:

  • User Details: If the template uses macros to fetch user details, ensure that the macros are enabled when the document is opened.
  • Logos: Verify that the logos are correctly linked in the template. If they are not appearing, check the paths or references in the template.
  • Styles: If styles are not being applied, confirm that the styles in the template are correctly defined and that the document is set to use them.

This approach should help you retain all necessary information from the template when merging it into the target document. If you encounter specific issues with logos or styles, please provide more details for further assistance.

@Prakruth Aspose.Words does not run macros when load documents and there is no way to run macros using Aspose.Words at all.

In your case, you can use either Mail Merge or LINQ Reporting Engine to fill template with data. For example, see the attached template and code:

ds = aw.reporting.JsonDataSource("C:\\Temp\\data.json")
doc = aw.Document("C:\\Temp\\in.docx")
engine = aw.reporting.ReportingEngine()
engine.build_report(doc, ds, "info")
doc.save("C:\\Temp\\out.docx")

data.zip (1.2 KB)
in.docx (16.6 KB)
out.docx (13.2 KB)

here for reporting, should we load template/macros as json?
can you please elaborate?

@Prakruth You should modify your template to use either mail merge or LINQ Reporting syntax as in the code example above and pass the data that should be inserted into your template in the form, which is convenient for you. In the above code example JSON data was used for demonstration purposes.