Can list contents be styled (color, italic, etc) programmatically (as opposed to interactively) for the .docx type specifically?
MATLAB: How to style Report Generator lists programmatically
boldcolorfontgeneratoritaliclistMATLABreportstyle
Related Solutions
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);
To add custom styles to a PDF template you can use the following workflow:
1. <https://www.mathworks.com/help/rptgen/ug/create-an-html-template.html Create a PDF template>, for example:
>> mlreportgen.dom.Document.createTemplate('mytemplate','pdf');
2. <https://www.mathworks.com/help/rptgen/ug/create-an-html-template.html Unzip the template>, for example:
>> unzipTemplate('mytemplate.pdftx')
3.<https://www.mathworks.com/help/rptgen/ug/modify-styles-in-pdf-templates.html#bu7zyyw-1 Modify the styles>, for example:
Add the following text to the "root.css" file in ../mytemplate/stylesheets
span.RedItalic { font-style: italic; color:Tomato;} span.Strong{ font-weight: bold;}
4. Zip the template back up, for example:
>> zipTemplate('mytemplate.pdftx')
5. Use the defined styles in the report, for example:
import mlreportgen.report.*;import mlreportgen.dom.*; doc = Report('newreport','pdf', '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);
Similar workflow applies to HTML templates as well.
Related Question
- How to add a footer in PDF report using only Report API with chapters
- How to change the chapter title font in Report Generator
- How to apply font options when using the LaTeX interpreter in MATLAB
- How to convert Words-based report generator script to HTML report
- How to add subscript text in a formal table
- How to add custom text to a footer in a generated report
- How to set the text font style of a Data Cursor object
Best Answer