用 Python 将 HTML 转换为 MHTML

MHTML 是一种网页归档格式,它将 HTML 代码和相关资源(如图像、样式表和脚本)合并为一个文件。这对于在单一文件中归档或共享网页特别有用。HTML 到 MHTML 的转换保留了网页在浏览器中显示的整个结构和格式,确保打开 MHTML 文件时的一致性。此外,MHTML 文件还可以离线访问网页,为在没有互联网连接的情况下查看内容提供了方便。

本文将介绍如何将 HTML 转换为 MHTML 以及如何使用 MHTMLSaveOptions

要继续学习本教程,请在您的 Python 项目中 安装并配置 Aspose.HTML for Python via .NET。我们的代码示例将帮助您使用 Python 库将 HTML 转换为 MHTML 并生成 MHTML 文件。

在线 HTML 转换器

您可以使用 Aspose.HTML for Python 通过 .NET API 实时将 HTML 转换为 MHTML。首先,从本地硬盘或 URL 加载 HTML 文件并运行示例。本示例使用默认保存选项。您将立即以单独文件的形式收到转换结果。

                
            

将 HTML 转换为 MHTML

使用 “convert_html() “方法将文件转换为另一种格式是一系列操作,其中包括文档加载和保存。下一个示例将解释如何将 HTML 转换为 MHTML:

  1. 使用 HTMLDocument 类加载 HTML 文件。
  2. 创建 MHTMLSaveOptions 类的实例,以控制 HTML 到 MHTML 的转换选项。
  3. 使用 Converter 类的 convert_html() 方法将 HTML 文档保存为 MHTML 文件。该方法接收 document, options, 输出文件路径 save_path 并执行转换操作。

只需一行代码即可将 HTML 转换为 MHTML

Converter 类的方法主要用于将 HTML 代码转换成各种格式的最简单方法。只需一行代码,您就可以在 Python 应用程序中将 HTML 转换为 MHTML!

1# Convert HTML to MHTML using Python
2
3import aspose.html.converters as conv
4import aspose.html.saving as sav
5
6# Convert HTML to MHTML
7conv.Converter.convert_html("document.html", sav.MHTMLSaveOptions(), "document.mht")

使用 MHTMLSaveOptions 将 HTML 转换为 MHTML

使用 Aspose.HTML for Python 通过 .NET 将 HTML 转换为 MHTML 时,可以使用 MHTMLSaveOptions自定义转换过程。下面的 Python 代码示例展示了如何创建带有自定义保存选项的 MHTML 文件:

 1# Convert HTML to MHTML using Python
 2
 3import os
 4import aspose.html.converters as conv
 5import aspose.html.saving as sav
 6
 7# Prepare directories and paths
 8output_dir = "output/"
 9if not os.path.exists(output_dir):
10    os.makedirs(output_dir)
11
12# Prepare HTML code with a link to another file and save it to "document1.html"
13code = "<span>Hello, World!!</span> <a href='document2.html'>click</a>"
14with open("document1.html", "w") as file:
15    file.write(code)
16
17# Prepare HTML code and save it to "document2.html"
18code = "<span>Hello, World!!</span>"
19with open("document2.html", "w") as file:
20    file.write(code)
21
22save_path = os.path.join(output_dir, "output-options.mht")
23
24# Change the value of the resource linking depth to 1 in order to convert document with directly linked resources
25options = sav.MHTMLSaveOptions()
26options.resource_handling_options.max_handling_depth = 1
27
28# Convert HTML to MHTML
29conv.Converter.convert_html("document.html", options, save_path)

在上面的示例中,我们使用的属性 max_handling_depth = 1 表示只处理保存文档中直接引用的页面。

保存选项 – MHTMLSaveOptions 类

使用 MHTMLSaveOptions 属性可以自定义渲染过程。它的 ResourceHandlingOptions 属性对于控制在转换过程中如何管理 HTML 文档中引用的外部资源至关重要。通过该属性,您可以指定 “resource_url_restriction”、“page_url_restriction”、“max_handling_depth “等选项。

PropertyDescription
page_url_restrictionThis property gets or sets restrictions applied to URLs of handled pages. The default value is ROOT_AND_SUB_FOLDERS.
resource_url_restrictionGets or sets restrictions applied to URLs of handled resources such as CSS, js, images, etc. The default is SAME_HOST.
max_handling_depthDetermines the maximum depth for handling linked resources. This is useful for ensuring that all necessary resources are embedded within the MHTML file, maintaining the integrity and appearance of the original HTML content.

Aspose.HTML 提供免费的在线 HTML 到 MHTML 转换器,可将 HTML 高质量、方便快捷地转换为 MHTML。只需上传、转换您的文件,几秒钟就能得到结果!

文本 “HTML 到 MHTML 转换器”

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.