主要内容

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

tmplview

预览源模板的内容和样式

自 R2024b 起

说明

使用 tmplview 预览源模板的内容和样式。该函数根据源模板的名称和类型生成一组文档并将它们放在目录中。这些文件具有与基于源模板的 mlreportgen.dom.Document 对象的输出相同的文件类型。有关输出文档结构的概述以及模板如何表示模板主体、模板文档部分空位和样式表,请参阅 模板内容和样式预览结构

tmplview(templateObj)templateObj 中的源模板生成预览文档,并在适当的查看器中打开主模板文档。在生成预览文档之前,您必须使用 close 方法关闭模板对象。

注意

在 Linux®Macintosh 平台上打开 Microsoft® Word 文档会调用 soffice 命令。要在 Linux 计算机上使用 soffice 命令,必须安装 Apache® OpenOffice® 或 LibreOffice®。在 Macintosh 上,您必须在 /Applications 文件夹中安装 Apache OpenOffice

示例

tmplview(templatePath)templatePath 指定的路径处的模板生成文档。

tmplview(templateName,format) 根据 templateName 指定的模板并以 format 指定的格式生成文档。

tmplview(___, OpenDocument=openDoc) 指定模板预览生成后打开哪个文档,由 openDoc 指定。

示例

全部折叠

此示例展示如何创建和预览模板的内容和样式。预览模板后,修改模板并重新生成预览以检查您的修改。

创建模板

导入 DOM 命名空间,这样您就不必使用 DOM API 类的完全限定名称。

import mlreportgen.dom.*

创建并打开 HTML 模板。

typeTemplate ="html";
nameTemplate = "bookreviewTemplate";
tmpl = Template(nameTemplate, typeTemplate);
open(tmpl);

创建并添加自定义段落样式。

pStyle = TemplateParagraphStyle("companyParaStyle");
pStyle.Formats = [FontFamily("calibri"), ...
    FontSize("12pt"), ...
    WhiteSpace("preserve")];
tmpl.Stylesheet.addStyle(pStyle);

创建并添加自定义表样式。

tblStyle = TemplateTableStyle("companyTableStyle");
tblStyle.TableEntriesFormats = Border("solid");
tblStyle.HeaderFormats = [Bold(), ...
    BackgroundColor("cadetblue")];
tblStyle.OddRowFormats = BackgroundColor("lightblue");
tmpl.Stylesheet.addStyle(tblStyle);

定义主要模板内容。

h = Heading1("Books I've Read");
append(tmpl,h);

为一桌子书创建并附加一个空位。

hole = TemplateHole("booksTable");
append(tmpl,hole);

创建并附加书评部分。

h = Heading1("Reviews");
append(tmpl,h);
hole = TemplateHole("bookReviews");
append(tmpl,hole);

为个别图书评级定义一个模板文档部分。

tdp = TemplateDocumentPart("BookReporter");

创建并附加 Title 空位。

title = Heading2();
append(title, TemplateHole("Title", "Title of the book"));
append(tdp, title);

创建并附加 Author 空位。

author = Heading3();
append(author, TemplateHole("Author", "Author of the book"));
append(tdp, author);

创建并附加 Rating 空位。使用您的自定义段落样式,"companyParaStyle"

rating = Paragraph("I rate this book: ");
rating.StyleName = "companyParaStyle";
append(rating, TemplateHole("Rating", "Rating of the book"));
append(rating, " out of 5 stars.");
append(tdp, rating);

将模板文档部分附加到模板。关闭模板。

tmpl.TemplateDocumentParts(end+1) = tdp;
close(tmpl);

使用 tmplview 预览模板

使用创建模板部分中的 Template 对象生成模板预览文档。

tmplview(tmpl);

或者您也可以使用:

  • 模板的名称和格式类型,tmplview(nameTemplate, typeTemplate)

  • 模板的路径,tmplview(tmpl.OutputPath)

在查看器中,您可以看到主模板文档中的内容。

Snippet of viewer window displaying the content of the main template document

要查看模板中的样式,请指定模板并使用 openDoc 参量指定样式表文档。您可以将任何模板规范语法与 openDoc 参量一起使用。

tmplview(tmpl.OutputPath,openDoc="stylesheet"); 

Snippet of viewer window displaying the template styles in the stylesheet document

要查看自定义模板文档部分 BookReporter,请指定模板并使用 openDoc 参量指定模板文档部分。如果您需要查找模板文档部分的名称,您可以在 tmplview 生成的文件夹中查找。

Snippet of MATLAB file explorer displaying the file structure of the template preview documents. A red box emphasizes the BookReporter.htmx file.

tmplview(nameTemplate,typeTemplate,openDoc="BookReporter");

Snippet of viewer window displaying the content of the custom template document part BookReporter.

修改模板并预览更改

修改代码的副本以获取模板中想要的更改。在这种情况下,将评级 Paragraph 文本更新为 " out of 10 stars"

typeTemplate ="html";
nameTemplate = "bookreviewTemplate";
tmpl = Template(nameTemplate, typeTemplate);
open(tmpl);

pStyle = TemplateParagraphStyle("companyParaStyle");
pStyle.Formats = [FontFamily("calibri"), ...
    FontSize("12pt"), ...
    WhiteSpace("preserve")];
tmpl.Stylesheet.addStyle(pStyle);

tblStyle = TemplateTableStyle("companyTableStyle");
tblStyle.TableEntriesFormats = Border("solid");
tblStyle.HeaderFormats = [Bold(), ...
    BackgroundColor("cadetblue")];
tblStyle.OddRowFormats = BackgroundColor("lightblue");
tmpl.Stylesheet.addStyle(tblStyle);

h = Heading1("Books I've Read");
append(tmpl,h);
hole = TemplateHole("booksTable");
append(tmpl,hole);

h = Heading1("Reviews");
append(tmpl,h);
hole = TemplateHole("bookReviews");
append(tmpl,hole);

tdp = TemplateDocumentPart("BookReporter");
title = Heading2();
append(title, TemplateHole("Title", "Title of the book"));
append(tdp, title);

author = Heading3();
append(author, TemplateHole("Author", "Author of the book"));
append(tdp, author);

rating = Paragraph("I rate this book: ");
rating.StyleName = "companyParaStyle";
append(rating, TemplateHole("Rating", "Rating of the book"));

将评级文字更改为 10 星。

append(rating, " out of 10 stars.");
append(tdp, rating);

tmpl.TemplateDocumentParts(end+1) = tdp;
close(tmpl);

查看更新后的模板。

tmplview(nameTemplate,typeTemplate,openDoc="BookReporter");

Snippet of viewer window displaying the content of the updated custom template document part BookReporter.

输入参数

全部折叠

用于生成模板预览文件的 Template DOM 对象,指定为 mlreportgen.dom.Template 对象。

用于生成预览文件的模板文件路径和名称(包括文件扩展名),指定为字符向量或字符串标量。

数据类型: char | string

用于生成预览文件的模板名称,指定为字符向量或字符串标量。不要包含文件扩展名。

数据类型: char | string

templateName 指定的模板的模板格式,指定为以下值之一:

  • Word 中的 "docx"

  • HTML 的 "html"

  • "html-file" 用于单文件 HTML,由一个文件组成,该文件包含报告的文本、样式表、JavaScript® 和图像。

  • "html-multipage" 用于多页 HTML (自 R2024a 起)

  • "pdf" 用于 PDF

  • "pdf" 用于 PDF/A (自 R2025a 起)

数据类型: char | string

要显示的模板组件或模板文档部分,指定为:

  • "templateBody"

  • "stylesheet"

  • 字符向量或字符串标量,其中包含模板文档部分的名称,例如 "myDocPart"

数据类型: char | string

限制

  • tmplview 无法在 MATLAB Online 中查看 Word 模板。

  • MATLAB® Web App 不支持 tmplview

版本历史记录

在 R2024b 中推出