主要内容

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

mlreportgen.dom.HTMLFile 类

命名空间: mlreportgen.dom
超类: mlreportgen.dom.HTML

将 HTML 文件转换为 DOM 文档

描述

将 HTML 文件的内容转换为包含具有相同内容和格式的 DOM 对象的 mlreportgen.dom.HTMLFile 对象。您可以将 HTMLFile 对象追加到任何类型的 DOM 文档,包括 Word 和 PDF 文档。

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

创建对象

描述

htmlFileObj = HTMLFile(htmlFile) 将 HTML 文件转换为包含具有相同内容和格式的 DOM 对象的 HTMLFile 对象。

HTMLFile 对象支持这些 HTML 元素和属性。此外,HTMLFile 对象接受包含自定义 CSS 属性的 HTML,这些属性以连字符开头。HTML、Microsoft® Word 和 PDF 输出支持自定义 CSS 属性。

示例

HTML 元素属性
aclass, style, href, name
addressclass, style
bclass, style
bigclass, style
blockquoteclass, style
bodyclass, style
br不适用
centerclass, style
citeclass, style
codeclass, style
ddclass, style
delclass, style
dfnclass, style
divclass, style
dlclass, style
dtclass, style
emclass, style
fontclass, style, color, face, size
h1, h2, h3, h4, h5, h6class, style, align
hrclass, style, align
iclass, style
insclass, style
imgclass, style, src, height, width
kbdclass, style
liclass, style
markclass, style
nobrclass, style
olclass, style
pclass, style, align
preclass, style
sclass, style
sampclass, style
smallclass, style
spanclass, style
strikeclass, style
strongclass, style
subclass, style
supclass, style
tableclass, style, align, bgcolor, border, cellspacing, cellpadding, frame, rules, width
tbodyclass, style, align, valign
tfootclass, style, align, valign
theadclass, style, align, valign
tdclass, style, bgcolor, height, width, colspan, rowspan,align, valign, nowrap
thclass, style, bgcolor, height, width, colspan, rowspan,align, valign, nowrap
trclass, style, align,bgcolor, valign
ttclass, style
uclass, style
ulclass, style
varclass, style

有关这些元素的信息,请参阅 https://developer.mozilla.org/en-US/docs/Web/HTML/Element

支持以下 CSS 格式:

  • background-color

  • border

  • border-bottom

  • border-bottom-color

  • border-bottom-style

  • boder-bottom-width

  • border-color

  • border-left

  • border-left-color

  • border-left-style

  • boder-left-width

  • border-right

  • border-right-color

  • border-rigtht-style

  • border-right-width

  • border-style

  • border-top

  • border-top-color

  • border-top-style

  • border-top-width

  • border-width

  • color

  • counter-increment

  • counter-reset

  • display

  • font-family

  • font-size

  • font-style

  • font-weight

  • height

  • line-height

  • list-style-type

  • margin

  • margin-bottom

  • margin-left

  • margin-right

  • margin-top

  • padding

  • padding-bottom

  • padding-left

  • padding-right

  • padding-top

  • text-align

  • text-decoration

  • text-indent

  • vertical-align

  • white-space

  • width

有关这些格式的信息,请参阅 https://developer.mozilla.org/en-US/docs/Web/CSS/Reference

输入参量

全部展开

HTML 文件路径,指定为字符向量或字符串标量。

属性

全部展开

注意

为了使 HTML 标记在您的报告中正确显示,您必须为空元素包含结束标记,并将属性值括在引号中。如果要将保留的 XML 标记字符显示为文本,则必须使用其等效的命名或数字 XML 字符。

保留字符描述等效字符
>大于>
<小于&lt;
&“&”&amp;
"双引号&quot;
'单引号&apos;
%百分号&#37;

容器的 HTML 标记名称,指定为字符向量或字符串标量。该名称必须是 HTML 元素,例如 "div""section""article"

注意

Microsoft Word 输出忽略 HTML 标记名称。

属性:

GetAccess
public
SetAccess
public

数据类型: char | string

样式表中定义的样式名称,指定为字符向量或字符串标量。样式名称是此元素所追加到的文档或文档部件的样式表中指定的样式的名称。指定的样式定义了该元素在输出文档中的外观,除非该元素的 Style 属性指定的格式覆盖了该样式。要了解有关使用样式表的更多信息,请参阅 使用样式表样式

注意

Microsoft Word 报告会忽略文档模板中未定义的样式名称。有关 Microsoft Word 模板的详细信息,请参阅DOM API 报告程序模板

属性:

GetAccess
public
SetAccess
public
NonCopyable
true

数据类型: char | string

应用于此文档元素对象的格式化,指定为 DOM 格式对象元胞数组。此属性指定的格式将覆盖此元素的 StyleName 属性指定的相应格式。不适用于此元素的格式将被忽略。

注意

此文档元素对象的子项将继承它们未覆盖的任何格式。

属性:

GetAccess
public
SetAccess
public
NonCopyable
true

数据类型: cell

此对象的父对象,指定为文档元素对象。一个文档元素必须只有一个父元素。

属性:

GetAccess
public
SetAccess
private
NonCopyable
true

HTMLFile 对象的子元素,指定为 mlreportgen.dom.Element 对象的数组。

属性:

GetAccess
public
SetAccess
private

数据类型: cell

标记,指定为字符向量或字符串标量。DOM API 在创建此对象的过程中生成一个会话唯一标记。生成的标记形式为 CLASS:ID,其中 CLASS 是对象类,ID 是对象的 Id 属性的值。使用此值来帮助确定在文档生成过程中出现的问题的位置。

属性:

GetAccess
public
SetAccess
public
NonCopyable
true

数据类型: char | string

会话唯一标识符是作为 HTML 对象创建的一部分生成的。您可以为 Id 指定自己的值。

属性:

GetAccess
public
SetAccess
public
NonCopyable
true

数据类型: char | string

注意

HTMLFile 忽略 KeepInterElementWhiteSpace 属性。如果要保留空白,请使用 fileread 将 HTML 文件读取为文本,然后按照 mlreportgen.dom.HTMLKeepInterElementWhiteSpace 属性所述的步骤进行操作。

方法

全部展开

示例

全部折叠

创建一个名为 myHTML.html 的文本文件并将其保存在当前文件夹中。将此文本添加到文件中:

<html>
<head>
<style>p {font-size:14pt;}</style>
</head>
<body>
<p style="white-space:pre"><b>Hello</b><i style="color:green"> World</i></p>
<p>This is <u>me</u> speaking</p>
</body>
</html>

要将 myHTML.html 文件转换为 Word 报告,请运行以下命令:

import mlreportgen.dom.*; 
rpt = Document("MyReport","docx"); 
htmlFile = HTMLFile("myHTML.html"); 
append(rpt,htmlFile); 
close(rpt); 
rptview(rpt.OutputPath);
生成的 Word 报告包含您在 HTML 文件中指定的文本。

Word report showing the text "Hello world" as the first paragraph, with Hello in bold and World in green and italic. The second paragraph reads "This is me speaking", with "me" underlined

提示

  • MATLAB® Report Generator™ mlreportgen.dom.HTML mlreportgen.dom.HTMLFile 对象通常无法接受将原生文档导出为 HTML 标记的第三方应用程序(例如 Microsoft Word)的原始 HTML 输出。在这些情况下,您的报告 API 报告生成程序可以使用 mlreportgen.utils.html2dom.prepHTMLStringmlreportgen.utils.html2dom.prepHTMLFile 函数准备原始 HTML 以供 mlreportgen.dom.HTML mlreportgen.dom.HTMLFile 对象使用。通常,您的程序必须进一步处理准备好的 HTML 以删除有效但不想要的对象,例如原始内容中的换行符。

  • Word 和 PDF 文档要求在段落中包含内联元素(例如文本和链接)。为了满足此要求,HTML 解析器创建包装段落来包含段落中尚不存在的内联元素。如果从包含不在段落中的内联元素的 HTML 创建 mlreportgen.dom.HTMLmlreportgen.dom.HTMLFile 对象并将该对象添加到 HTML 文档,则生成的 HTML 可能与输入的 HTML 不同。要生成不带添加包装段落的内联元素,请使用 mlreportgen.dom.RawText 对象将 HTML 标记插入 HTML 文档。

  • 默认情况下,DOM API 使用 12 磅的基本字体大小将 em 单位转换为实际字体大小。例如,指定为 2em 的字体大小将转换为 24 磅。要指定不同的基本字体大小,请使用 mlreportgen.dom.HTML 对象将内容添加到报告中。将对象的 EMBaseFontSize 属性设置为基本字体大小。例如,如果将 EMBaseFontSize 属性设置为 14,则字体大小 2em 将转换为 28 磅。

版本历史记录

在 R2015a 中推出