Convert HTML in Python
Python library offers a seamless solution for all your HTML conversion tasks. Convert HTML to PDF, MHTML, Markdown, XPS, DOCX, and Images!
How to Convert HTML Using Python
Aspose.HTML for Python via .NET API API is a powerful solution for parsing and processing HTML documents using Python. HTML, the backbone of the Internet, is the primary language for presenting information on websites. Sometimes, you need to convert HTML into various formats such as PDF, XPS, DOCX, Markdown, MHTML, and images to use them for specific tasks. Aspose.HTML for Python via .NET API makes the conversion process easier for developers. It loads the file using HTMLDocument class, creates the relevant save options object and uses the Converter class relevant conversion method.
The Python API simplifies this conversion process, offering developers a streamlined approach through three fundamental steps:
- Load an HTML document into a Document object using the HTMLDocument class. You can load HTML from a file, HTML code, or URL.
- Depending on the target format (e.g., PDF, XPS, DOCX, MHTML, Markdown, images), create the corresponding SaveOptions object. This object allows you to specify various parameters relevant to the output format, such as quality, page setup, image format, etc. The aspose.html.saving namespace is presented by data classes for description of specific save options at conversion & saving process.
- Invoke one of the convert_html() methods and pass the required parameters to it.
Use the powerful Python API to process HTML content and convert it into the format your applications need!
Online HTML Converter
You can convert HTML with Aspose.HTML in real-time. The following Python code example demonstrates how to convert an HTML document. Please load a file from a local file system or URL, select the output format and run the example. You will immediately get the result. Also, you can try free online Converters here.
from aspose.html import * from aspose.html.converters import * from aspose.html.saving import * # Load an HTML document from a file or URL document = HTMLDocument("{{input lower}}") # Initialize saving options {{#if_output 'PDF'}} options = PdfSaveOptions() {{/if_output}} {{#if_output 'DOCX'}} options = DocSaveOptions() {{/if_output}} {{#if_output 'XPS'}} options = XpsSaveOptions() {{/if_output}} {{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF'}} options = ImageSaveOptions(ImageFormat.{{output param2 upper}}) {{/if_output}} {{#if_output 'MHTML'}} options = MHTMLSaveOptions() {{/if_output}} {{#if_output 'MD'}} options = MarkdownSaveOptions() {{/if_output}} {{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF' 'PDF' 'MHTML' 'MD' 'XPS' 'DOCX'}} # Convert HTML to {{output upper}} Converter.convert_html(document, options, "output.{{output lower}}") {{/if_output}} {{#if_output 'XHTML'}} options = HTMLSaveOptions() options.document_type = HTMLSaveOptions.XHTML # Save HTML as {{output upper}} document.save("output.xhtml", options) {{/if_output}}
Convert HTML, MHTML, EPUB, Markdown, and SVG
The aspose.html.converters namespace provides easy access to conversion methods. It offers many conversions to popular formats, such as PDF, XPS, image formats, etc. Using the Converter class, you can easily convert loaded HTML and other HTML-based documents into the desired formats:
- convert_html() methods convert HTML content to PDF, XPS, DOCX, MHTML, Markdown, or images using the specified SaveOptions to ensure format accuracy and integrity.
- convert_markdown() metods convert Markdown content to HTML. All of these methods allow for the basic Markdown to HTML conversion. Conversions from Markdown to other formats go through the Markdown to HTML conversion stage.
- convert_mhtml() methods convert MHTML content to PDF, XPS, DOCX, and images.
- convert_epub() methods convert EPUB content to PDF, XPS, DOCX, and images.
- convert_svg() methods convert SVG content to PDF, XPS, and images.
To learn more about Python API conversion functions, visit our documentation .
Installing Aspose.HTML Python library
This library supports parsing of HTML5, CSS3, SVG, and HTML Canvas to construct a Document Object Model (DOM) based on the WHATWG DOM Standard. You can install the Aspose.HTML for Python via .NET on your system running the following command:
pip install aspose-html-net
For more details about Python library installation and system requirements, please refer to Aspose.HTML Documentation .
Other Supported Conversions
You can also convert HTML, XHTML, MHTML, EPUB, Markdown, and SVG files into many other file formats including few listed below: