There are several ways to achieve this result.
1. Applying formatting by using properties of the "Text" object and using "append" to add text to Paragraph.
Example with a Document:
import mlreportgen.dom.*;
doc = Document('mydoc','docx');
para = Paragraph('A ');
para.WhiteSpace = 'pre';
append(doc,para);
boldText = Text('magic square');
boldText.Bold = 1;
append(para, boldText);
append(para, ' is an N-by-N ');
colorText = Text('matrix');
colorText.Color = 'red';
colorText.Italic = 1;
append(para, colorText);
append(para, [' constructed from the integers 1 through N^2 '...
'with equal row, column, and diagonal sums.']);
close(doc);
rptview('mydoc','docx');
Example with a Report:
import mlreportgen.report.*;
import mlreportgen.dom.*;
doc = Report('myrpt','docx');
para = Paragraph('A ');
para.WhiteSpace = 'pre';
add(doc, para);
boldText = Text('magic square');
boldText.Bold = 1;
append(para, boldText);
append(para, ' is an N-by-N ');
colorText = Text('matrix');
colorText.Color = 'red';
colorText.Italic = 1;
append(para, colorText);
append(para, [' constructed from the integers 1 through N^2 '...
'with equal row, column, and diagonal sums.']);
close(doc);
rptview('myrpt','docx');
2. Applying formatting by using the "HTML" object and HTML tags, including creating a list.
Example with a Document:
import mlreportgen.dom.*;
doc = Document('mydoc_html','docx');
para = HTML( ...
['<p style="white-space:pre">A <b>magic square</b> is an N-by-N ' ...
'<span style="color:red;font-style:italic">matrix</span>' ...
' constructed from the integers 1 through N^2 ' ...
'with equal row, column, and diagonal sums.</p>']);
append(doc,para);
ol = HTML(['<ol>', ...
'<li> <b> bold item </b> </li>',...
'<li> normal item </li>',...
'<li style="font-style:italic"> italic item </li>',...
'<li style="color:red"> colorful item </li>'...
'</ol>']);
append(doc,ol);
close(doc);
rptview('mydoc_html','docx');
Example with a Report:
import mlreportgen.report.*;
import mlreportgen.dom.*;
doc = Report('myrpt_html','docx');
para = HTML( ...
['<p style="white-space:pre">A <b>magic square</b> is an N-by-N ' ...
'<span style="color:red;font-style:italic">matrix</span>' ...
' constructed from the integers 1 through N^2 ' ...
'with equal row, column, and diagonal sums.</p>']);
add(doc,para);
ol = HTML(['<ol>', ...
'<li> <b> bold item </b> </li>',...
'<li> normal item </li>',...
'<li style="font-style:italic"> italic item </li>',...
'<li style="color:red"> colorful item </li>'...
'</ol>']);
add(doc,ol);
close(doc);
rptview('myrpt_html','docx');
3. Applying formatting by using Word styles from a template.
You would first need to create a Word template, and add the styles you would like to be able to apply to your text. You need to supply the template as an argument when creating a Document or a Report object. Then you can specify a style from that template for any Text objects you create. Example with a Document:
(mytemplate.dotx attached)
import mlreportgen.dom.*;
doc = Document('newdocument','docx', 'mytemplate');
list = OrderedList({Text('This is normal text'),...
Text('This is bold text', 'Strong'),...
Text('This is red italic text', 'RedItalic')});
append(doc, list);
close(doc);
Example with a Report:
(mytemplate.dotx attached)
import mlreportgen.report.*;
import mlreportgen.dom.*;
doc = Report('newreport','docx', 'mytemplate');
list = OrderedList({Text('This is normal text'),...
Text('This is bold text', 'Strong'),...
Text('This is red italic text', 'RedItalic')});
add(doc, list)
close(doc);
Best Answer