Main Content

Format Inheritance

You can use the Document Object Model (DOM) API to use template-based styles to specify the appearance of a document element object and format object-based styles or their equivalent format properties. If you set the StyleName and the Style properties of a document element object, the formats in the Style property override the corresponding formats specified by the template-based style of the StyleName property. In this example, suppose that the Warning style defines the color of a warning as yellow. The setting of the Style property on the paragraph overrides the color specified by the StyleName setting.

import mlreportgen.dom.*;
d = Document("MyDoc","docx","MyTemplate");
p = Paragraph("Danger!");
p.StyleName = "Warning";
p.Style = {Color("red")};
append(d,p);
close(d);

You can use a single statement to assign a format for all the document element objects contained by a container. For example, this code uses a single Style property to assign a color to all the entries in a table.

import mlreportgen.dom.*;
d = Document("MyDoc");
tableArray = {"a","b";"c","d"};
table = append(d,tableArray);
table.Style = {Color("blue")};
close(d);
rptview(d.OutputPath);

When you generate an HTML or PDF report, a document element object inherit formats it does not specify from its container, regardless of whether the document element object specifies a value for StyleName. The container inherits any formats that it does not specify from its container, and so on, to the top of the container hierarchy.

When you generate a DOCX report and you specify a value for StyleName for a document element object, the object does not inherit formats it does not specify from its container. In this example, the document element object text specifies a value for StyleName and does not inherit the bold format specified for the document element object para.

import mlreportgen.dom.*;

d = Document("MyDoc","docx");

text = Text("My Paragraph");
text.Underline = "single";
text.StyleName = "paraTitle";

para = Paragraph(text);
para.Bold = true;

append(d,para);
close(d);
rptview(d);

The text "My Paragraph" without bold formatting

If you do not specify a value for StyleName, then the document element object text inherits the bold format specified for the document element object para.

import mlreportgen.dom.*;
d = Document("MyDoc","docx");

text = Text("My Paragraph");
text.Underline = "single";

para = Paragraph(text);
para.Bold = true;

append(d,para);
close(d);
rptview(d);

The text "My Paragraph" with bold formatting

See Also

Topics