User`s guide
Create a Table of Contents
13-83
Use Template-Defined Styles to Set Outline Levels
You can use styles defined in the report’s template to set the outline level of a paragraph.
For example, by default Word documents include a set of styles, Heading 1, Heading 2,
and so on, that define outline levels for paragraphs you want to appear in a TOC. Your
program can use these built-in styles to specify that paragraphs that serve as section
heads appear in the TOC. The following example illustrates the use of template-defined
styles to set the outline levels of section heads. This example assumes that the template
MyTemplate includes a TOC reference.
import mlreportgen.dom.*;
d = Document('MyReport','docx','MyTemplate');
append(d, Paragraph('Chapter 1','Heading 1'));
append(d, Paragraph('Section 1','Heading 2'));
close(d);
rptview(d.OutputPath); % Updates the TOC
You can also use Word or an HTML editor to define your own heading styles and then
use them to generate a report.
Use Format Objects to Set Outline Levels
You can use format objects to set outline levels. This example assumes that the template
MyTemplate includes a TOC reference.
import mlreportgen.dom.*;
d = Document('MyReport','docx','MyTemplate');
h1 = {FontFamily('Arial'),FontSize('16pt'),OutlineLevel(1)};
h2 = {FontFamily('Arial'),FontSize('14pt'),OutlineLevel(2)};
p = append(d,Paragraph('Chapter 1'));
p.style = h1;
p = append(d, Paragraph('Section 1'));
p.style = h2;
close(d);
rptview(d.OutputPath); % Updates the TOC
Use Heading Objects to Set Outline Levels
You can use mlreportgen.dom.Heading objects to specify outline levels. A Heading
object is a paragraph whose constructor specifies its outline level. You can use a Heading