Skip to main content

Process & Manipulate HTML via Python API

banner

Product Page | Docs | Demos | API Reference | Examples | Blog | Search | Free Support

Aspose.HTML for Python via .NET is a powerful API for Python that provides headless browser functionality, allowing you to work with HTML documents. With this API, you can easily create new HTML documents or open existing ones from different sources. Once you have the document, you can perform various manipulation operations, such as removing and replacing HTML nodes, rendering, and converting HTML to other popular formats, etc.

HTML API Features

The following are some popular features of Aspose.HTML for Python via .NET:

General Features

  • Create, Load, and Read Documents. Create, load, and modify HTML, XHTML, Markdown, or SVG documents with full control over elements, attributes, and structure using a powerful DOM-based API.
  • Load EPUB and MHTML file Formats. Open, read, and convert EPUB and MHTML documents with full support for their internal structure and linked resources.
  • Edit Documents. Insert, remove, clone, or replace HTML elements at any level of the DOM tree for granular control over content.
  • Save HTML Documents. Save documents along with all linked resources like CSS, fonts, and images using customizable saving options.
  • Navigate HTML. Navigate through documents using either NodeIterator or TreeWalker.
  • Sandboxing. Configure a Sandbox environment that is independent of the execution machine, ensuring a secure and isolated environment for running and testing.

Data Extraction

  • DOM Traversal. Navigate and manipulate the DOM tree using W3C-compliant traversal interfaces to inspect and retrieve content from HTML documents.
  • XPath Queries. Perform high-performance XPath queries to find and extract target content from large HTML documents.
  • CSS Selector and JavaScript. Use CSS selector queries and JavaScript execution to dynamically locate and extract specific elements.
  • Extract CSS Styling Information. Retrieve and analyze inline styles, embedded <style> blocks, and external stylesheets within HTML documents.
  • Extract any Data from HTML Documents. Text, attributes, form values, metadata, tables, links, or media elements: Aspose.HTML for Python via .NET enables the accurate and efficient extraction of any content for processing, analysis, or editing.

Conversion and Rendering

  • Convert Documents. Convert HTML, XHTML, SVG, MHTML, MD, and EPUB files to a wide range of formats, including PDF, XPS, DOCX, and different image formats (PNG, JPEG, BMP, TIFF, and GIF).
  • Custom Conversion Settings. Adjust page size, resolution, stylesheets, resource management, script execution, and other settings during conversion to fine-tune the output.
  • Markdown Support. Convert HTML to Markdown or vice versa for content migration and Markdown-based workflows.
  • Timeout Control. Set and control the timeout for the rendering process.

Advanced HTML Features

  • Monitor DOM Changes. Use MutationObserver to monitor DOM modifications.
  • HTML Templates. Populate HTML documents with external data sources such as XML and JSON.
  • Output Streams. Support for both single (PDF, XPS) and multiple (image formats) output file streams.
  • Check Web Accessibility. Check web documents against WCAG standards using built-in validators and accessibility rule sets.

Supported File Formats

Format Description Load Save
HTML HyperText Markup Language format ✔️ ✔️
XHTML eXtensible HyperText Markup Language format ✔️ ✔️
MHTML MIME HTML format ✔️ ✔️
EPUB E-book file format ✔️
SVG Scalable Vector Graphics format ✔️ ✔️
MD Markdown markup language format ✔️ ✔️
PDF Portable Document Format ✔️
XPS XML Paper Specification format ✔️
DOCX Microsoft Word Open XML document format ✔️
TIFF Tagged Image File Format ✔️
JPEG Joint Photographic Experts Group format ✔️
PNG Portable Network Graphics format ✔️
BMP Bitmap Picture format ✔️
GIF Graphics Interchange Format ✔️
WEBP Modern image format providing both lossy and lossless compression ✔️

Platform Independence

Aspose.HTML for Python via .NET can be used to develop applications for a vast range of operating systems, such as Windows, where Python 3.5 or later is installed. You can build both 32-bit and 64-bit Python applications.

Get Started

Are you ready to give Aspose.HTML for Python via .NET a try?

Simply run pip install aspose-html-net from the Console to fetch the package. If you already have Aspose.HTML for Python via .NET and want to upgrade the version, please run pip install --upgrade aspose-html-net to get the latest version.

You can run the following snippets in your environment to see how Aspose.HTML works, or check out the GitHub Repository or Aspose.HTML for Python via .NET Documentation for other common use cases.

Create a New HTML Document

If you want to create an HTML document programmatically from scratch, use the parameterless constructor:

import aspose.html as ah

# Initialize an empty HTML document
with ah.HTMLDocument() as document:
    # Create a text node and add it to the document
    text = document.create_text_node("Hello, World!")
    document.body.append_child(text)

    # Save the document to a file
    document.save("create-new-document.html")

Source - Create a Document in Python

Extract Images from Website

Here is an example of how to use Aspose.HTML for Python via .NET to find images specified by the <img> element:

import os
import aspose.html as ah
import aspose.html.net as ahnet

# Prepare output directory
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)

# Open HTML document from URL
with ah.HTMLDocument("https://docs.aspose.com/svg/net/drawing-basics/svg-color/") as doc:
    # Collect all <img> elements
    images = doc.get_elements_by_tag_name("img")

    # Get distinct relative image URLs
    urls = set(img.get_attribute("src") for img in images)

    # Create absolute image URLs
    abs_urls = [ah.Url(url, doc.base_uri) for url in urls]

    for url in abs_urls:
        # Create a network request
        request = ahnet.RequestMessage(url)

        # Send request
        response = doc.context.network.send(request)

        # Check whether a response is successful
        if response.is_success:
            # Parse the URL to get the file name
            file_name = os.path.basename(url.pathname)

            # Save image to the local file system
            with open(os.path.join(output_dir, file_name), "wb") as f:
                f.write(response.content.read_as_byte_array())

Source - Extract Images From Website in Python

HTML to PDF in one line of code

Aspose.HTML for Python via .NET allows you to convert HTML to PDF, XPS, Markdown, MHTML, PNG, JPEG, and other file formats. The following snippet demonstrates the conversion from HTML to PDF literally with a single line of code!

import aspose.html.converters as conv
import aspose.html.saving as sav

# Convert HTML to PDF
conv.Converter.convert_html("document.html", sav.PdfSaveOptions(), "document.pdf")

Source - Convert HTML to PDF in Python

Convert HTML to Markdown (MD)

The following snippet demonstrates the conversion from HTML to GIT-based Markdown (MD) Format:

import aspose.html.converters as conv
import aspose.html.saving as sav

# Prepare HTML code and save it to a file
code = "<h1>Header 1</h1>" \
         "<h2>Header 2</h2>" \
         "<p>Hello, World!!</p>"
with open("document.html", "w", encoding="utf-8") as f:
         f.write(code)
         f.close()
         # Call the convert_html() method to convert HTML to Markdown
         conv.Converter.convert_html("document.html", sav.MarkdownSaveOptions.git, "output.md")

Source - Creating an HTML Document

Convert EPUB to PDF using SaveOptions

The PdfSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting EPUB to PDF format. In the example, we use the page_setup, jpeg_quality, and css.media_type properties:

import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr

# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
os.makedirs(output_dir, exist_ok=True)

document_path = os.path.join(input_dir, "input.epub")
save_path = os.path.join(output_dir, "epub-to-pdf.pdf")

# Open an existing EPUB file for reading
with open(document_path, "rb") as stream:

    # Create an instance of PdfSaveOptions
    options = sav.PdfSaveOptions()
    options.page_setup.any_page = dr.Page(dr.Size(800, 600), dr.Margin(10, 10, 10, 10))
    options.css.media_type.PRINT

    # Convert EPUB to PDF
    conv.Converter.convert_epub(stream, options, save_path)

Source - Convert EPUB to PDF in Python

Product Page | Docs | Demos | API Reference | Examples | Blog | Search | Free Support | Temporary License

Metadata

Release files for aspose-html-net 26.10.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for aspose-html-net 26.10.0
File Actions
aspose_html_net-26.10.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for aspose_html_net-26.10.0-py3-none-win_amd64.whl
aspose_html_net-26.10.0-py3-none-win32.whl Python 3 none Windows x86-32 Details for aspose_html_net-26.10.0-py3-none-win32.whl
aspose_html_net-26.10.0-py3-none-manylinux1_x86_64.whl Python 3 none Linux glibc 2.5+ x86-64 Details for aspose_html_net-26.10.0-py3-none-manylinux1_x86_64.whl
aspose_html_net-26.10.0-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for aspose_html_net-26.10.0-py3-none-macosx_11_0_arm64.whl
aspose_html_net-26.10.0-py3-none-macosx_10_14_x86_64.whl Python 3 none macOS 10.14+ x86-64 Details for aspose_html_net-26.10.0-py3-none-macosx_10_14_x86_64.whl

Total release size: 320.8 MB

Release files / aspose_html_net-26.10.0-py3-none-win_amd64.whl

Download URL aspose_html_net-26.10.0-py3-none-win_amd64.whl
Size 58.8 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
2bc654c368ee7a7ee6ad4a8f41f35eba5d39a11a317a10558f33e80feec94d29
BLAKE2b-256 checksum
How to use checksums
aae8862746b3450f3cfe930d832bdb5df50859c2f793a9ffecb8dd62df91ffc9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.0

Release files / aspose_html_net-26.10.0-py3-none-win32.whl

Download URL aspose_html_net-26.10.0-py3-none-win32.whl
Size 51.4 MB
Tags Python 3 Windows x86-32
SHA-256 checksum
How to use checksums
bec332ea935fbf16f0fe3004b8d0ebe9b45935899fab695d7d150b137c6934ca
BLAKE2b-256 checksum
How to use checksums
85ebc508efe724f3d560ffeb20d6f2d0b1e18df1c2e3188fb4dfa3a6018b0d02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.0

Release files / aspose_html_net-26.10.0-py3-none-manylinux1_x86_64.whl

Download URL aspose_html_net-26.10.0-py3-none-manylinux1_x86_64.whl
Size 83.4 MB
Tags Linux glibc 2.5+ x86-64 Python 3
SHA-256 checksum
How to use checksums
4cf4472594060a0254579052f8453a0209b3069a4f407ede924e9fc9bc9a49c8
BLAKE2b-256 checksum
How to use checksums
3815489986f2bf385177f1fe09c63a88cd861477be8c8c8aa11faf03a3c4a468
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.0

Release files / aspose_html_net-26.10.0-py3-none-macosx_11_0_arm64.whl

Download URL aspose_html_net-26.10.0-py3-none-macosx_11_0_arm64.whl
Size 56.6 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
fea2e56e9b304e39eb3af6ad5ef57abdebb0b8a5eca269aa1927fe3d35490222
BLAKE2b-256 checksum
How to use checksums
0449a49a1180ff29c17c6c9497e8a6862d5cad8f9706dda7b41af1ebb2214dbf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.0

Release files / aspose_html_net-26.10.0-py3-none-macosx_10_14_x86_64.whl

Download URL aspose_html_net-26.10.0-py3-none-macosx_10_14_x86_64.whl
Size 70.6 MB
Tags Python 3 macOS 10.14+ x86-64
SHA-256 checksum
How to use checksums
258c2f0f9f36dfb4d83005189a1d6ee95f0369eeff91f894c7001f7369486f4d
BLAKE2b-256 checksum
How to use checksums
b280435a2ca179e13c362488a7485bfb2cff621a3d21f787cdebe4b9dd22cf15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.0

Security for aspose-html-net 26.10.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page