用 Python 保存 HTML 文档

如何用 Python 保存 HTML

本文详细介绍了如何使用 Aspose.HTML for Python 通过 .NET API 保存 HTML 文档。使用 HTML 文档后,您可以使用 HTMLDocument.save() 方法之一保存更改。有一些方法可以将文档保存到文件或 URL。

请注意,Aspose.HTML for Python via .NET 为创建输出文件提供了两种不同的方法:

  1. 基于 HTML 的方法:这种方法的输出是类似 HTML 的文件。它使用 SaveOptions 类,该类是管理脚本、样式和图像等相关资源保存过程的基础。ResourceHandler 类负责处理这些资源。它的开发目的是将 HTML 内容和相关资源保存到数据流中,并提供一些方法让您控制资源的处理过程。

  2. 可视化表示法:这种方法侧重于创建 HTML 的可视化表示。它基于 RenderingOptions 类,该类提供了定义页面大小、页边距、分辨率、用户样式等的专门方法。

本文将介绍 SaveOptions 和 ResourceHandler 类的使用。

保存 HTML

完成对 HTML 文档的修改后,您可能想保存它。您可以使用 HTMLDocument 类提供的 save() 方法之一来实现。下面是一个保存 HTML 文件的简单 Python 示例:

 1# Save HTML to a file in Python
 2
 3import os
 4import aspose.html as ah
 5
 6# Prepare the output path
 7output_dir = "output"
 8os.makedirs(output_dir, exist_ok=True)
 9
10document_path = os.path.join(output_dir, "save-html-document.html")
11
12# Create an empty HTML document
13with ah.HTMLDocument() as document:
14    # Create a text node and add it to the document
15    text = document.create_text_node("Hello, World!")
16    document.body.append_child(text)
17
18    # Save the HTML document
19    document.save(document_path)

在上面的示例中,我们使用 HTMLDocument() 构造函数来初始化一个空的 HTML 文档。HTMLDocument 类的 create_text_node(data) 方法会根据指定的字符串创建一个文本节点。调用 document.save(document_path) 时,该方法会将 document 对象的 HTML 内容写入由 document_path 指定的文件。

上面的示例非常简单。不过,在实际应用中,您往往需要对保存过程进行额外的控制。接下来的几节将介绍如何使用资源处理选项或将文档保存为不同格式。

保存选项和资源处理选项

SaveOptions 是一个基类,允许为保存操作指定附加选项,并有助于管理链接的资源。SaveOptions 类的 resource_handling_options 属性用于配置资源处理。ResourceHandlingOptions 类定义了管理 HTML 文档关联资源的选项。它提供了多个属性,用于控制在保存或处理文档时如何处理各种类型的资源:

将 HTML 保存到文件

下面的 Python 代码片段展示了如何使用 SaveOptions 类的 resource_handling_options 属性来管理与文档文件的链接。

 1# Save HTML with linked resources in Python
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.saving as sav
 6
 7# Prepare output paths
 8output_dir = "output"
 9os.makedirs(output_dir, exist_ok=True)
10
11document_path = os.path.join(output_dir, "save-with-linked-file.html")
12
13# Create an HTML file with a linked page
14with open(document_path, "w", encoding="utf-8") as file:
15    file.write("<p>Hello, World!</p><a href='linked.html'>linked file</a>")
16
17# Create the linked HTML file
18with open(os.path.join(output_dir, "linked.html"), "w", encoding="utf-8") as file:
19    file.write("<p>Hello, linked file!</p>")
20
21# Load and save the HTML document with directly linked pages
22output_path = os.path.join(output_dir, "save-with-linked-file_out.html")
23with ah.HTMLDocument(document_path) as document:
24    options = sav.HTMLSaveOptions()
25    options.resource_handling_options.max_handling_depth = 1
26    document.save(output_path, options)

如果需要将 HTML 保存为图像或 PDF 等具有固定布局的文档,可以将文档转换为所需格式。请参阅 HTML 转换器 部分了解更多信息。

将 HTML 保存为 MHTML

在某些情况下,您需要将网页保存为单个文件。MHTML文档可以很方便地实现这一目的,因为它是一个网页归档文件,可以将所有内容都保存在其中。HTMLSaveFormat 枚举 指定了文档的保存格式,可以是 HTML、MHTML 和 Markdown 格式。下面的示例展示了如何使用 save(path, save_format) 方法将 HTML 保存为 MHTML 格式。

 1# Save HTML as MHTML in Python
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.saving as sav
 6
 7# Prepare the output path
 8output_dir = "output"
 9document_path = os.path.join(output_dir, "save-html-to-mhtml.mht")
10os.makedirs(output_dir, exist_ok=True)
11
12# Create an HTML file with a linked page
13with open("document.html", "w", encoding="utf-8") as file:
14    file.write("<p>Hello, World!</p><a href='linked-file.html'>linked file</a>")
15
16# Create the linked HTML file
17with open("linked-file.html", "w", encoding="utf-8") as file:
18    file.write("<p>Hello, linked file!</p>")
19
20# Load HTML and save it as MHTML
21with ah.HTMLDocument("document.html") as document:
22    document.save(document_path, sav.HTMLSaveFormat.MHTML)

保存的 “save-to-MTHML.mht “文件存储了 “document.html “和 “linked-file.html “文件的 HTML。

将 HTML 保存为 Markdown

Markdown 是一种使用纯文本语法的标记语言。除了 HTML 转 MHTML 的示例外,您还可以使用 HTMLSaveFormat 保存 HTML 到 Markdown。请看下面的 Python 示例:

 1# Save HTML as Markdown in Python
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.saving as sav
 6
 7# Prepare input and output paths
 8data_dir = "data"
 9output_dir = "output"
10os.makedirs(output_dir, exist_ok=True)
11
12input_path = os.path.join(data_dir, "document.html")
13output_path = os.path.join(output_dir, "html-to-markdown.md")
14
15# Load HTML and save it as Markdown
16with ah.HTMLDocument(input_path) as document:
17    document.save(output_path, sav.HTMLSaveFormat.MARKDOWN)

保存 SVG

通常,SVG 被嵌入 HTML 文件中,用于表示图像、图标、表格等矢量图形。不过,SVG 也可以从网页中提取出来,像 HTML 文档一样进行独立操作。

由于 SVGDocument 和 HTMLDocument 都遵循 WHATWG DOM 标准,它们的操作(如加载、读取、编辑、转换和保存)在很大程度上是相似的。因此,任何演示操作 HTMLDocument 的示例也可用于 SVGDocument。

 1# Create and save an SVG file in Python
 2
 3import os
 4import aspose.html.dom.svg as ahsvg
 5
 6# Prepare the output path
 7output_dir = "output"
 8document_path = os.path.join(output_dir, "save-html-to-svg.svg")
 9os.makedirs(output_dir, exist_ok=True)
10
11# Prepare SVG code
12svg_code = """
13<svg xmlns='http://www.w3.org/2000/svg' height='400' width='300'>
14    <path stroke="#a06e84" stroke-width="3" fill="#74aeaf" d="  
15	M 150,50 L 150, 300
16	M 120,100 L 150,50 L 180, 100
17    M 110,150 L 150,90 L 190, 150
18	M 90,220 L 150,130 L 210, 220
19	M 70,300 L 150,190 L 230, 300
20    M 110,310 L 150,240 L 190, 310
21	" />
22</svg>
23"""
24
25# Create and save the SVG document
26with ahsvg.SVGDocument(svg_code, ".") as document:
27    document.save(document_path)

有关使用 Aspose.SVG Python API 处理和渲染 SVG 文档的更多信息,请参阅 Aspose.SVG for Python via .NET Documentation。

下载 Aspose.HTML for Python via .NET 库,即可成功、快速、轻松地处理 HTML 文档。Python 库可以创建、修改、提取数据、转换和渲染 HTML 文档,而无需外部软件。它支持 EPUB、MHTML、XML、SVG 和 Markdown 等流行文件格式,并可渲染为 PDF、DOCX、XPS 和图像文件格式。

Aspose.HTML 提供免费的在线 HTML 转换器,用于将 HTML 文档转换为各种流行格式。只需从文件或 URL 中加载 HTML,选择要转换的格式,就大功告成了。它速度快,而且完全免费!