Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
After creating, loading, or editing an HTMLDocument, call save() to write its current DOM state to a file. Aspose.HTML for Python via .NET can save HTML, create MHTML or Markdown output, and save standalone SVG documents.
To save HTML in Python, create or load an HTMLDocument, modify it if needed, and call document.save(path). Use HTMLSaveOptions to control linked resources, or pass HTMLSaveFormat.MHTML or HTMLSaveFormat.MARKDOWN to select another HTML-based output format.
Saving and conversion solve different tasks:
HTMLDocument.save() and classes from the
aspose.html.saving module.| API | Use it to |
|---|---|
| HTMLDocument.save() | Save the current HTML document by using a supported path, format, options, or resource handler overload. |
| HTMLSaveOptions | Configure HTML output and access linked-resource handling settings. |
| ResourceHandlingOptions | Control how resources and linked pages are processed during saving. |
| HTMLSaveFormat | Select MHTML or Markdown output with a format-based save() overload. |
| SVGDocument | Create, edit, and save a standalone SVG document. |
The simplest workflow creates an HTML document, adds content to its DOM, and saves the result as a local .html file.
The following example:
output directory and the save-html-document.html destination path.HTMLDocument.Hello, World! text node and appends it to the document body.save() to write the document to the HTML file. 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)The HTMLDocument() constructor creates the document structure in memory. Changes remain in memory until save() writes the current DOM to the destination. To load and modify an existing file before saving it, see
Edit HTML Documents in Python.
For examples of saving CSS and images to a selected folder, embedding resources in one HTML file, and processing directly linked pages, see Save HTML with Resources in Python.
The
HTMLSaveFormat enumeration selects MHTML or Markdown when calling a compatible save() overload.
MHTML is a web archive format that combines the current HTML document and handled resources in one file. A linked HTML page is not included merely because it appears in an <a href> element; linked-page processing is controlled by max_handling_depth and URL restrictions.
The following example:
document.html and a page named linked-file.html.document.html into an HTMLDocument.save() with HTMLSaveFormat.MHTML.output/save-html-to-mhtml.mht containing the current document and resources handled under the active save settings. 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)With the default page depth of 0, the example does not package the target of the linked-file.html hyperlink as another archived page. Use
MHTMLSaveOptions when linked-page depth or resource handling requires additional configuration.
Markdown is a plain-text markup format suited to editable documentation and content workflows.
The following example:
data/document.html into an HTMLDocument.output/html-to-markdown.md as the destination.save() with HTMLSaveFormat.MARKDOWN. 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)For converter-based workflows and additional Markdown settings, see Convert HTML to Markdown in Python.
SVG can be embedded in HTML or processed as a standalone XML-based vector document. Use SVGDocument for a standalone SVG DOM. HTML and SVG use the node concepts defined by the WHATWG DOM Standard, while the SVG 2 specification defines SVG elements and rendering behavior.
The following example:
SVGDocument from the markup and a base URI.output/save-html-to-svg.svg. 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)This workflow creates and saves SVG source content; it does not convert an HTML page to SVG. For additional SVG workflows, see the Aspose.SVG for Python via .NET documentation.
| Issue | Cause and recommended action |
|---|---|
| Images, CSS, scripts, or fonts are missing | Check the base URL, resource paths, handling behavior, and URL restrictions configured through HTMLSaveOptions. |
| A linked HTML page is not processed | The default max_handling_depth is 0. Use 1 for directly referenced pages or another suitable depth, subject to URL restrictions. |
A PDF, DOCX, XPS, or image was expected from save() | HTMLDocument.save() handles HTML-based formats. Use the HTML conversion or rendering APIs for visual output formats. |
| MHTML or Markdown output was not created | Pass HTMLSaveFormat.MHTML or HTMLSaveFormat.MARKDOWN to the compatible save() overload. |
| SVG changes are absent from the output | Apply the changes to an SVGDocument and call its save() method afterward. |
Create or load an HTMLDocument, modify its DOM if needed, and call document.save(path) to write the current document state to an HTML file.
The save API can process linked resources. Use HTMLSaveOptions.resource_handling_options to control the handling behavior and which resource URLs may be processed.
To create a self-contained .html file, set HTMLSaveOptions.resource_handling_options.default to ResourceHandling.EMBED and configure JavaScript separately when required. To create a MIME web archive, call document.save(path, HTMLSaveFormat.MHTML). See
Save HTML with Resources in Python for a complete embedding example.
Saving writes the document to an HTML-based format such as HTML, MHTML, or Markdown. Conversion produces another representation such as PDF, DOCX, XPS, or an image.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.