Converting Between Formats – Python Examples

Welcome to the “Converting between Formats” guides! One of the key features of Aspose.HTML is its powerful conversion capability. Converting between formats is essential for various reasons: it allows you to work in a familiar, convenient format or use the unique advantages of different formats for specific tasks. This chapter provides a detailed overview of Aspose.HTML conversion functions for Python via .NET. You will learn how to convert HTML, MHTML, EPUB, Markdown, and SVG files using the Python library. With detailed examples and explanations, you will quickly master the process of converting your documents into the required formats.

A Few Ways to Convert files

Aspose.HTML for Python via .NET offers multiple ways to convert between formats: using the Converter class’s methods or the render_to() method of the HTMLDocument class.

  1. The static Converter class is a shared facade that provides HTML, MHTML, EPUB, Markdown, and SVG file conversions to popular formats and makes these operations convenient and easy. Its static methods offer a clear and straightforward approach to converting these documents to PDF, XPS, DOCX, MD, MHTML, and various image formats.

  2. The render_to() method allows you to render HTML or SVG to another format and send a document to the output device. Aspose.HTML for Python via .NET API supports several output device implementations, including PdfDevice, XpsDevice, DocDevice, and ImageDevice classes, which render documents to PDF, XPS, DOCX, and image formats respectively.

The following example demonstrates converting HTML to PDF using the Converter class:

1from aspose.html import *
2from aspose.html.converters import *
3from aspose.html.saving import *
4
5document = HTMLDocument("document.html")
6options = PdfSaveOptions()
7Converter.convert_html(document, options, "output.pdf")

To convert HTML to PDF using the render_to() method, you can use the following code snippet:

1from aspose.html import *
2from aspose.html.rendering.pdf import *
3
4with HTMLDocument("document.html") as document:
5    pdf_options = PdfRenderingOptions()
6    with PdfDevice(pdf_options, "output.pdf") as device:
7        document.render_to(device)

The current chapter covers popular converters and provides Python examples for common conversion scenarios. These examples demonstrate the simplicity and universality of the conversion process, which involves only a few necessary steps. The articles in this chapter detail the supported conversions and how to perform them using the Converter class, which groups all low-level conversion operations into a single class.

Aspose.HTML offers free online Converters for converting HTML, XHTML, MHTML, EPUB, XML, and Markdown files to a variety of popular formats. You can easily convert HTML to PDF, HTML to JPG, SVG to PDF, MHTML to PDF, or MD to HTML. Just select the file, choose the format to convert, and you’re done. It’s fast and completely free!

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.