This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

mlreportgen.dom.DocumentPart class

Package: mlreportgen.dom

Create a document part object


Define a document part, a repeatable part of a report. A document part typically has holes that you fill during report generation. You can append a part to a document or to a document part of the same output type.


documentPartObj = DocumentPart() creates an HTML document part using the default HTML template.

documentPart = DocumentPart(type) creates a document part of the specified type (for example, Microsoft® Word) based on the default template for that part.

documentPartObj = DocumentPart(type,templatePath) creates a document part based on the specified template.

documentPartObj = DocumentPart(type,templatePath,docPartTemplateName) creates a document part based on the specified document part template in the specified template.

documentPartObj = DocumentPart(templateSrc,docPartTemplateName) creates a document part based on the specified document part template stored in the template used by the specified source. The source can be a document or a document part.

Input Arguments

expand all

Type of output, specified as one of these values:

  • 'html'— HTML output

  • 'pdf'— PDF based on a PDF template

  • 'docx'— Word output

  • 'html-file'— HTML output, using a single file that contains the CSS, JavaScript®, and images for the report

If you specify a template using the templatePath argument, the value for type must match the template type.

Full path of this part’s template file or folder, specified as a character vector. If you omit a file extension, the template type is based on the document type, for example, .docx for Word.

Data Types: char

Document part template name, specified as a character vector. Specify where the part is stored using the templatePath or templateSrc argument.

Document or document part object whose template contains the template for this document part, specified as an mlreportgen.dom.Document object for a document or a mlreportgen.dom.DocumentPart object for a document part.

Output Arguments

expand all

Document part, returned as an mlreportgen.dom.DocumentPart object.


expand all

This read-only property lists child elements of this object.

This read-only property is the hole ID of the current hole in this document.

Type of the current template hole, specified as 'Inline' or 'Block'.

  • An inline hole is for document elements that a paragraph element can contain: Text, Image, LinkTarget, ExternalLink, InternalLink, CharEntity, AutoNumber.

  • A block hole can contain a Paragraph, Table, OrderedList, UnorderedList, DocumentPart, or Group.

This property applies to Word and PDF documents. For Word documents, the value is a DOCXPageLayout object that specifies the current page layout. For PDF documents, the value is a PDFPageLayout object if the document currently specifies a page layout. For HTML documents, the value is always [].

ID for this document element, specified as a character vector. The DOM generates a session-unique ID when it creates the document element. You can specify your own ID.

This read-only property lists the open status of this document element.

This read-only property lists the parent of this document element.

Tag for document element, specified as a character vector.

A session-unique ID is generated as part of document element creation. The generated tag has the form CLASS:ID, where CLASS is the class of the element and ID is the value of the Id property of the object. You can specify a tag to replace the generated tag.

For example, to make it easier to identify where an issue occurred during document generation, you can specify your own tag value.

The name of this part's template if the template is stored in the document part template library of the template specified by this part's TemplatePath property. If this property is [], the template specified by the TemplatePath property is used as this part's template.

Path of this part's template or of a template whose template library contains this part's template, specified as a character vector.

Output type, specified as one of these values.

  • 'HTML' – HTML report packaged as a zipped file containing the HTML file, images, style sheet, and JavaScript files of the report.

  • 'HTML-FILE' – HTML report as a single HTML file containing the text, style sheet, JavaScript, and base64-encoded images of the report

  • 'PDF' – PDF file

  • 'DOCX'Microsoft Word document

If you specify a template using the TemplatePath property, the value for Type must match the template type.


Use DocumentPart methods like you use the corresponding Document methods.




Use DocumentPart.addHTML in a similar way to how you use Document.addHTML.

Append HTML text to document


Use DocumentPart.addHTMLFile in a similar way to how you use Document.addHTMLFile.

Append HTML file contents to document


Append document element to the document part.


Close this document part. You cannot close a document part if it has not been opened or was previously closed.


Create document part template.


Fill document hole.


Get core properties of document part.


Get full path of main part of output document.


Move to next template hole.


Open this document part. You cannot open a document part if it was previously opened or closed. You also cannot open a document part if its library source is closed.


Set core properties of document part.


collapse all

This example creates a function createMagicParts that defines a document part based on a blank document part template. The new document part has a heading whose text depends on the input. Each document part generated contains a magic square table whose appearance is also based on the input. The example creates a containing function magicparts that appends the document part to the report iteratively based on the input.

Create the function.

function magic_square_report(square_sizes, report_type)
%MAGIC_SQUARE_REPORT Report on magic squares
%    magic_square_report(square_sizes, report_type) 
%    creates a report of the specified output type 
%    (docx, pdf, or html) on the specified magic
%    squares. For example, to create a PDF report on 
%    squares of size 5, 10, and 15, enter the following 
%    line at the MATLAB command line:
%      magic_square_report([5,10,15],'pdf');

import mlreportgen.dom.*;
rpt = Document('MagicSquareReport',report_type);
for i = 1:length(square_sizes)
    sz = square_sizes(i);
    section = createSquareSection(rpt,sz);
function section = createSquareSection(rpt,square_size)
import mlreportgen.dom.*;
% Create document part to hold section
section = DocumentPart(rpt.Type);
% Create magic square heading
h1 = Heading1(sprintf('magic(%i)',square_size));
% Put each square on a separate page.
h1.Style = {PageBreakBefore(true)};
% Create table to hold square
table = append(section, Table(magic(square_size)));
% Format table
table.Border = 'solid';
table.ColSep = 'solid';
table.RowSep = 'solid';

Call the function to generate the report. Change the input arguments to change the contents or output format. This example creates a Word document that contains three squares.