Create and Load HTML Documents in Python

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.

Create an HTML Document

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.

Create an Empty HTML Document

The following example creates an empty document and saves it as output/document-empty.html:

  1. Create the output directory and destination path.
  2. Initialize HTMLDocument without constructor arguments.
  3. Call 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.

Create HTML Content with the DOM

This example creates a document, adds a Hello, World! text node to its <body>, and saves output/create-new-document.html:

  1. Open a new HTMLDocument with a Python context manager.
  2. Create a text node with create_text_node().
  3. Append the node to document.body.
  4. Save the populated document.
 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.

Load HTML from Different Sources

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.

Load HTML from a File

The following example loads data/document.html and saves it to output/document-edited.html:

  1. Build the input and output paths.
  2. Load the local file with HTMLDocument(document_path).
  3. Make any required DOM changes where indicated in the example.
  4. Save the document to the output 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.

Load HTML from a URL

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:

  1. Pass the web-page URL to HTMLDocument.
  2. Access the root element through document_element.
  3. Print the serialized HTML through 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.

Load HTML from a String

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:

  1. Defines <p>Hello, World!</p> as a Python string.
  2. Creates a document from the string and uses . as the base URI.
  3. Saves the result as 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)

Load HTML from a Stream

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:

  1. Encodes the HTML markup as a BytesIO stream.
  2. Supplies the stream and base URI to HTMLDocument.
  3. Saves the loaded document as 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)

Load an SVG File in Python

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.

Load SVG from a File and Access Its DOM

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.

  1. Save the sample SVG as data/load-svg.svg.
  2. Pass the SVG file path to the SVGDocument constructor.
  3. Find the <circle> element with get_elements_by_tag_name().
  4. Read its 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.

Load SVG from a Stream

Use a stream when SVG markup is already available in memory. The existing example:

  1. Defines SVG markup containing a circle with a radius of 40 pixels.
  2. Encodes the markup and stores it in a BytesIO stream.
  3. Creates SVGDocument with the stream and a base URI.
  4. Prints the serialized SVG and saves it as 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.

Work with MHTML and EPUB Sources

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.

Common Document Loading Issues

IssueLikely CauseRecommended Action
Relative images or styles do not loadThe 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 failsThe 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 contentHTMLDocument() 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 HTMLDocumentThese formats use source-specific conversion workflows.Use the MHTML or EPUB converter for the required output format.

Related Articles