Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
The HTMLDocument class represents an HTML document as a Document Object Model (DOM) tree. The document-tree model and HTML document structure are defined by the WHATWG DOM and HTML Living Standard. You can create an empty document or load HTML from a local file, URL, string, or binary stream and then inspect, edit, save, or convert it.
Use HTMLDocument() to create an empty HTML document. To load existing content, pass a file path or URL to a supported constructor. When loading HTML from a string or stream, also provide a base URI so that relative CSS, images, scripts, and links can be resolved correctly.
Aspose.HTML for Python via .NET creates the standard <html>, <head>, and <body> structure for a new HTMLDocument. You can save that structure immediately or add DOM nodes before saving it.
The following example creates an empty document and saves it as output/document-empty.html:
HTMLDocument without constructor arguments.save() to write the document to an HTML file. 1# Create an empty HTML document 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)
9save_path = os.path.join(output_dir, "document-empty.html")
10
11# Create and save an empty HTML document
12with ah.HTMLDocument() as document:
13 # Work with the document here
14 document.save(save_path)The saved file contains an empty HTML document structure. Add elements to the DOM before calling save() when the file should contain page content.
This example creates a document, adds a Hello, World! text node to its <body>, and saves output/create-new-document.html:
HTMLDocument with a Python context manager.create_text_node().document.body. 1# Create an HTML document in Python using the DOM
2
3import os
4import aspose.html as ah
5
6# Prepare the output path
7output_dir = "output"
8os.makedirs(output_dir, exist_ok=True)
9document_path = os.path.join(output_dir, "create-new-document.html")
10
11# Create an empty HTML document
12with ah.HTMLDocument() as document:
13 # Create a text node and add it to the document
14 text = document.create_text_node("Hello, World!")
15 document.body.append_child(text)
16
17 # Save the HTML document
18 document.save(document_path)Continue with Edit an HTML Document for examples that create elements, set attributes, and modify CSS.
Choose the constructor that matches where the HTML content comes from. A file path or URL identifies an external source, while a string or stream supplies the markup directly.
The following example loads data/document.html and saves it to output/document-edited.html:
HTMLDocument(document_path). 1# Load an HTML file in Python
2
3import os
4import aspose.html as ah
5
6# Prepare input and output paths
7output_dir = "output"
8input_dir = "data"
9os.makedirs(output_dir, exist_ok=True)
10
11document_path = os.path.join(input_dir, "document.html")
12save_path = os.path.join(output_dir, "document-edited.html")
13
14# Load, process, and save the HTML document
15with ah.HTMLDocument(document_path) as document:
16 # Work with the document here
17 document.save(save_path)The current example does not modify the DOM; it demonstrates the load-and-save workflow. Add editing operations before document.save() when the output must differ from the source.
HTMLDocument can load a web page directly from an absolute URL. The constructor performs a synchronous load and processes linked resources required by the document.
The example performs these steps:
HTMLDocument.document_element.outer_html.1# Load HTML from a URL in Python
2
3import aspose.html as ah
4
5# Load the web page and print its HTML
6with ah.HTMLDocument("https://docs.aspose.com/html/files/aspose.html") as document:
7 print(document.document_element.outer_html)This example writes the loaded markup to the console; it does not save an output file. If the page cannot be loaded, the constructor raises a loading error, so production code should handle network and resource-loading failures.
Use an HTMLDocument constructor that accepts HTML markup and a base URI when the source already exists in memory. The base URI determines how relative resource URLs are resolved.
The following example:
<p>Hello, World!</p> as a Python string.. as the base URI.output/create-html-from-string.html. 1# Create an HTML document from a string in Python
2
3import os
4import aspose.html as ah
5
6# Define the HTML content
7html_code = "<p>Hello, World!</p>"
8
9# Prepare the output path
10output_dir = "output"
11os.makedirs(output_dir, exist_ok=True)
12save_path = os.path.join(output_dir, "create-html-from-string.html")
13
14# Create an HTML document from the string and save it
15with ah.HTMLDocument(html_code, ".") as document:
16 document.save(save_path)Use io.BytesIO when HTML is already available as bytes and should be loaded without first writing a source file to disk.
The stream example:
BytesIO stream.HTMLDocument.output/load-from-stream.html. 1# Load HTML from a stream in Python
2
3import os
4import io
5import aspose.html as ah
6
7# Prepare the output path
8output_dir = "output"
9os.makedirs(output_dir, exist_ok=True)
10save_path = os.path.join(output_dir, "load-from-stream.html")
11
12# Define the base URI for relative resources
13base_uri = "."
14
15# Load HTML from a memory stream and save it
16with io.BytesIO(b"<p>Hello, World!</p>") as content_stream:
17 with ah.HTMLDocument(content_stream, base_uri) as document:
18 document.save(save_path)Use
SVGDocument from the aspose.html.dom.svg module for standalone SVG content. Like HTML, SVG is exposed through a DOM, but SVG-specific classes and methods belong to the SVG API. The
SVG 2 specification defines the SVG document model, elements, and rendering behavior.
The following example loads the local
load-svg.svg file and reads an attribute from its <circle> element. Download the sample and place it in the data directory before running the code.
data/load-svg.svg.SVGDocument constructor.<circle> element with get_elements_by_tag_name().fill attribute through the SVG DOM.1# Load an SVG file in Python and access its DOM
2
3import aspose.html.dom.svg as ahsvg
4
5# Load the SVG file and access an element through the DOM
6with ahsvg.SVGDocument("data/flower.svg") as document:
7 circle = document.get_elements_by_tag_name("circle")[0]
8 print(circle.get_attribute("fill"))The code prints #2F80ED, the fill color defined for the circle. The same DOM can be used to inspect elements, change attributes, or save the modified SVG.
Use a stream when SVG markup is already available in memory. The existing example:
BytesIO stream.SVGDocument with the stream and a base URI.load-from-stream.svg. 1# Create an SVG document from a string in Python
2
3import io
4import aspose.html.dom.svg as ahsvg
5
6# Define the SVG content
7svg_content = "<svg xmlns='http://www.w3.org/2000/svg'><circle cx='50' cy='50' r='40'/></svg>"
8base_uri = "."
9
10# Create an SVG document from a memory stream
11with io.BytesIO(svg_content.encode("utf-8")) as content_stream:
12 with ahsvg.SVGDocument(content_stream, base_uri) as document:
13 # Print and save the SVG document
14 print(document.document_element.outer_html)
15 document.save("load-from-stream.svg")For additional SVG-specific workflows, see the Aspose.SVG for Python via .NET documentation.
MHTML stores HTML and associated resources in a web archive, while EPUB packages publication content and resources. These formats are supported as conversion sources, but they are not loaded with an HTMLDocument constructor for DOM editing.
| Issue | Likely Cause | Recommended Action |
|---|---|---|
| Relative images or styles do not load | The string or stream was created without a suitable base URI. | Pass a base URI that relative resource paths can be resolved against. |
| Loading from a URL fails | The page or one of its required resources is unavailable or blocked by network settings. | Verify the URL and network access, and handle loading errors in application code. |
| An empty document contains no visible content | HTMLDocument() creates the document structure but does not add page content. | Create and append elements or text nodes before saving. |
MHTML or EPUB cannot be edited with HTMLDocument | These formats use source-specific conversion workflows. | Use the MHTML or EPUB converter for the required output format. |
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.