Main Content

mlreportgen.dom.TemplateDOCXStyle Class

Namespace: mlreportgen.dom

Parse existing styles within DOCX template

Since R2024a

Description

This class represents a DOCX style defined by a DOCX template (.dotx) file. Opening a DOCX template creates an array containing an instance of this class for each style defined by the source template file. You can access the styles via the TemplateStyles object of the template's Stylesheet property.

This class indicates only that a style exists in the source DOCX template and that the style is copied to generated templates. You cannot view or modify the style formats for objects of this class, only replace the object or remove it from a style sheet. To view the formats for an object of this class, open the source DOCX template in Microsoft® Word and inspect the style. If you determine the style does not meet your requirements, create new text, paragraph, linked, table, or list styles programmatically and set the formats as needed.

The mlreportgen.dom.TemplateDOCXStyle class is a handle class.

Class Attributes

HandleCompatible
true

For information on class attributes, see Class Attributes.

Properties

expand all

Name of the style, specified as a character vector or string scalar.

Attributes:

SetAccess
private
NonCopyable
true

Data Types: char | string

Type of content that this style formats, specified as a character vector or string scalar.

Attributes:

SetAccess
private
NonCopyable
true

Data Types: char | string

The class ignores this property.

Tag for mlreportgen.dom.TemplateDOCXStyle object, specified as a character vector or string scalar. The DOM API generates a session-unique tag as part of the creation of this object. The generated tag has the form CLASS:ID, where CLASS is the object class and ID is the value of the Id property of the object. Specify your own tag value to help you identify where to look when an issue occurs during document generation.

Attributes:

NonCopyable
true

Data Types: char | string

Object identifier for mlreportgen.dom.TemplateDOCXStyle object, specified as a character vector or string scalar. The DOM API generates a session-unique identifier when it creates the document element object. You can specify your own value for Id.

Attributes:

NonCopyable
true

Data Types: char | string

Examples

collapse all

View an existing style in a DOCX template, create a new style with modifications to that style, and replace the existing style with the modified style.

Create a DOCX Template and Examine the Default Style

Import the DOM API namespace so you do not have to use fully qualified names.

import mlreportgen.dom.*

Create a template using the default DOCX template.

t = Template("myTemplate","docx");

Open the template and check if the "rgMATLABTABLE" style exists.

open(t);
stylesheet = t.Stylesheet;
tableStyle = getStyle(stylesheet,"rgMATLABTABLE")
tableStyle = 
  TemplateDOCXStyle with properties:

       Name: 'rgMATLABTABLE'
       Type: 'table'
    Formats: []
        Tag: 'dom.TemplateDOCXStyle:1738'
         Id: '1738'

Open the source template in Microsoft® Word.

rptview(t.TemplatePath);

Open the source template in Word. In Word, inspect the "Title" style in the source DOCX template.

Create a Modified Style and Replace the Default Style

Create a new table style named "rgMATLABTABLE".

newTableStyle = TemplateTableStyle("rgMATLABTABLE");

Define formats similar to the style from the source template by using the same font, line spacing, and widow and orphan handling.

oldFormats = [LineSpacing(1),FontFamily("Calibri"),WidowOrphanControl];

Leave a 15-point space after the table.

om = OuterMargin;
om.Bottom = "15pt";
oldFormats(end + 1) = om;

Define a format that gives the table a solid border.

newFormat = Border("solid","blue");

Set the formats of the new style.

newTableStyle.Formats = [oldFormats,newFormat];

Replace the old style with the new style.

replaceStyle(stylesheet,newTableStyle);

Close the template.

close(t);
rptview(t);

Version History

Introduced in R2024a