Main Content

本页采用了机器翻译。点击此处可查看英文原文。

mlreportgen.dom.DocumentPart 类

命名空间: mlreportgen.dom

创建文档部件对象

描述

定义文档部件,报告中可重复的部分。文档部件通常存在一些空位,您需要在报告生成期间填充这些空位。您可以将某个部分追加到文档或相同输出类型的文档部件。

mlreportgen.dom.DocumentPart 类是一个 handle 类。

创建对象

描述

documentPartObj = DocumentPart 使用默认 HTML 模板创建 HTML 文档部件。

示例

documentPart = DocumentPart(type) 根据部件的默认模板创建指定类型的文档部件(例如,Microsoft® Word)。

documentPartObj = DocumentPart(type,templatePath) 根据指定的模板创建文档部件。

documentPartObj = DocumentPart(type,templatePath,docPartTemplateName) 根据指定模板中的指定文档部件模板创建文档部件。

documentPartObj = DocumentPart(templateSrc,docPartTemplateName) 根据存储在指定源使用的模板中的指定文档部件模板创建文档部件。源可以是文档或文档部件。

输入参量

全部展开

输出的类型,指定为下列值之一:

  • "html" - HTML 输出

  • "pdf" - 基于 PDF 模板的 PDF

  • "docx" - Word 输出

  • "html-file" - HTML 输出,使用包含报告的 CSS、JavaScript® 和图像的单个文件

  • "html-multipage" - HTML 输出包含 HTML 文档文本,分为页面、图像、样式表和 JavaScript 文件 (自 R2024a 起)

如果使用 templatePath 参量指定模板,则 type 的值必须与模板类型匹配。

此部件的模板文件或文件夹的完整路径,指定为字符向量或字符串标量。如果省略文件扩展名,则模板类型基于文档类型,例如 Word 的 .docx

数据类型: char

文档部件模板名称,指定为字符向量或字符串标量。使用 templatePathtemplateSrc 参量指定该部件的存储位置。

文档或文档部件对象,其模板包含此文档部件的模板,指定为文档的 mlreportgen.dom.Document 对象或文档部件的 mlreportgen.dom.DocumentPart 对象。

属性

全部展开

文档中当前空位的 ID,指定为字符向量或字符串标量。

属性:

SetAccess
private
Transient
true
NonCopyable
true

数据类型: char | string

当前模板空位的类型,指定为 "Inline""Block"

  • 内联空位适用于段落元素可以包含的文档元素:TextImageLinkTargetExternalLinkInternalLinkCharEntityAutoNumber

  • 块状空位可以包含 ParagraphTableOrderedListUnorderedListDocumentPartGroup 元素。

属性:

SetAccess
private
Transient
true
NonCopyable
true

数据类型: char | string

该文档的当前页面布局,指定为 mlreportgen.dom.DOCXPageLayout 对象、mlreportgen.dom.PDFPageLayout 对象或 []。此属性适用于 Word 和 PDF 文 档。对于 Word 文档,该值是一个指定当前页面布局的 DOCXPageLayout 对象。对于 PDF 文档,如果文档当前指定了页面布局,则该值为 PDFPageLayout 对象。对于 HTML 文档,该值始终为 []

属性:

SetAccess
private
Transient
true
NonCopyable
true

该文档的打开状态,指定为 'unopened''open''closed'

属性:

SetAccess
private
Transient
true
NonCopyable
true

数据类型: char | string

如果模板存储在此部件的 TemplatePath 属性指定的模板的文档部件模板库中,则此部件的模板名称指定为字符串标量或字符向量。如果此属性为 [],则使用 TemplatePath 属性指定的模板作为此部件的模板。

属性:

NonCopyable
true

数据类型: char

此部件模板的路径或模板库包含此部件模板的模板的路径,指定为字符向量或字符串标量。

属性:

NonCopyable
true

数据类型: char

输出文件类型,指定为以下值之一:

文件类型
"docx"

Microsoft Word

"html"

HTML 输出为压缩或解压后的文件夹,其中包含 HTML 文档文本、图像、样式表和 JavaScript 文件

"html-file"

HTML 输出由单个文件组成,其中包含报告的文本、样式表、JavaScript 和图像

"html-multipage" (自 R2024a 起)

HTML 输出为压缩或解压后的文件夹,其中包含分为多个页面的 HTML 文档文本、图像、样式表和 JavaScript 文件

"pdf"

PDF

如果使用 TemplatePath 属性指定模板,则该模板必须与 Type 属性一致。

属性:

NonCopyable
true

数据类型: char | string

mlreportgen.dom.DocumentPart 对象的父级,指定为文档元素对象。一个文档元素必须只有一个父元素。

属性:

SetAccess
private
NonCopyable
true

mlreportgen.dom.DocumentPart 对象的子项,指定为文档元素对象的数组。此属性包含使用 append 方法追加的文档元素对象。

属性:

SetAccess
private
NonCopyable
true

mlreportgen.dom.DocumentPart 对象的标签,指定为字符向量或字符串标量。DOM API 在创建此对象的过程中生成一个会话唯一标签。生成的标签形式为 CLASS:ID,其中 CLASS 是对象类,ID 是对象的 Id 属性的值。指定您自己的标签值,以帮助您确定在文档生成过程中出现问题时要查找的位置。

属性:

NonCopyable
true

数据类型: char | string

mlreportgen.dom.DocumentPart 对象的对象标识符,指定为字符向量或字符串标量。DOM API 在创建文档元素对象时会生成一个会话唯一标识符。您可以为 Id 指定自己的值。

属性:

NonCopyable
true

数据类型: char | string

方法

全部展开

示例

全部折叠

此示例创建一个函数 createMagicSquareReport,该函数根据空白文档部件模板定义文档部件。新文档部件有一个标题,其文本取决于输入。生成的每个文档部件都包含一个幻方表,其外观也基于输入。该示例还创建了一个本地函数 createSquareSection,该函数根据输入迭代地将文档部件追加到报告中。

创建函数。

function createMagicSquareReport(square_sizes, report_type)
%createMagicSquareReport 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:
%
%      createMagicSquareReport([5,10,15],"pdf");

import mlreportgen.dom.*;
rpt = Document("MagicSquareReport",report_type);
open(rpt);
for i = 1:length(square_sizes)
    sz = square_sizes(i);
    section = createSquareSection(rpt,sz);
    append(rpt,section);
end
close(rpt);
rptview(rpt.OutputPath);
 
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)};
append(section,h1);
% Create table to hold square
table = append(section, Table(magic(square_size)));
% Format table
table.Border = "solid";
table.ColSep = "solid";
table.RowSep = "solid";

调用该函数来生成报告。改变输入参量来改变内容或输出格式。本示例创建一个包含三个正方形的 Word 文档。

 createMagicSquareReport([5,8,12],"docx");

版本历史记录

在 R2014a 中推出