### Load HTML Document from File using Python Source: https://docs.aspose.com/html/python-net/create-a-document Provides a Python code example for loading an existing HTML file from a specified directory, allowing for subsequent modification and saving. ```python import os import aspose.html as ah # Setup directories and define paths output_dir = "output/" input_dir = "data/" if not os.path.exists(output_dir): os.makedirs(output_dir) document_path = os.path.join(input_dir, "document.html") save_path = os.path.join(output_dir, "document-edited.html") # Initialize a document from a file document = ah.HTMLDocument(document_path) # Work with the document # Save the document to a file document.save(save_path) ``` -------------------------------- ### Create Markdown File and Convert to PDF using Python Source: https://docs.aspose.com/html/python-net/convert-markdown-to-pdf This example demonstrates creating a Markdown file programmatically and then converting it to a PDF using Aspose.HTML for Python. It includes setting up output directories, writing Markdown content to a file, and then performing the conversion. ```python import os import aspose.html.converters as conv import aspose.html.saving as sav # Setup output directory and paths output_dir = "output/" os.makedirs(output_dir, exist_ok=True) source_path = os.path.join(output_dir, "document.md") save_path = os.path.join(output_dir, "markdown-to-pdf.pdf") # Create a simple Markdown example file code = "### Hello, World!\nConvert Markdown to PDF!" with open(source_path, "w") as file: file.write(code) # Convert Markdown file to an intermediate HTMLDocument document = conv.Converter.convert_markdown(source_path) # Create an instance of PdfSaveOptions options = sav.PdfSaveOptions() # Convert HTML to PDF conv.Converter.convert_html(document, options, save_path) ``` -------------------------------- ### Install Aspose.HTML for Python via .NET Source: https://docs.aspose.com/html/python-net/faq This command installs the Aspose.HTML for Python via .NET library using pip. Ensure you have pip installed and configured in your environment. ```bash pip install aspose-html-net ``` -------------------------------- ### Create Markdown File and Convert to HTML (Python) Source: https://docs.aspose.com/html/python-net/convert-markdown-to-html This Python example demonstrates how to programmatically create a Markdown file from a string and then convert it to HTML using Aspose.HTML. It handles directory setup, file creation, and the conversion process, providing a complete workflow for generating HTML from dynamic Markdown content. ```python import os import aspose.html.converters as conv # Setup directories and define paths output_dir = "output/" os.makedirs(output_dir, exist_ok=True) source_path = os.path.join(output_dir, "document.md") # Prepare a simple Markdown example code = "### Hello, World!\nConvert Markdown to HTML!" # Create a Markdown file with open(source_path, "w", encoding="utf-8") as file: file.write(code) # Prepare a path to save the converted file save_path = os.path.join(output_dir, "document-output.html") # Convert Markdown to HTML document conv.Converter.convert_markdown(source_path, save_path) ``` -------------------------------- ### Create Markdown from String and Convert to PNG Source: https://docs.aspose.com/html/python-net/convert-markdown-to-image This example demonstrates how to create a Markdown file programmatically from a string and then convert it to a PNG image. It involves setting up output directories, writing the Markdown content to a file, converting it to an intermediate HTML document, and finally rendering the HTML to a PNG image. ```python import os import aspose.html.converters as conv import aspose.html.saving as sav # Setup output directory and paths output_dir = "output/" os.makedirs(output_dir, exist_ok=True) source_path = os.path.join(output_dir, "document.md") # Create a simple Markdown example file code = "### Hello, World!\nConvert Markdown to PNG!" with open(source_path, "w") as file: file.write(code) # Convert Markdown file to an intermediate HTMLDocument document = conv.Converter.convert_markdown(source_path) # Create ImageSaveOptions with PNG format options = sav.ImageSaveOptions() # Prepare output file path save_path = os.path.join(output_dir, "markdown-to-image.png") # Convert HTMLDocument to PNG image conv.Converter.convert_html(document, options, save_path) ``` -------------------------------- ### Convert Markdown to DOCX from a string using Python Source: https://docs.aspose.com/html/python-net/convert-markdown-to-docx This Python code example demonstrates how to create a Markdown file from a string and then convert it to a DOCX file using Aspose.HTML. It covers preparing the source Markdown file, converting it to an HTML document, and finally saving it as a DOCX file. ```python import os import aspose.html.converters as conv import aspose.html.saving as sav # Prepare a path to a source Markdown file output_dir = "output/" source_path = os.path.join(output_dir, "document.md") # Prepare a simple Markdown example code = "### Hello, World!\nConvert Markdown to DOCX!" # Create a Markdown file with open(source_path, "w") as file: file.write(code) # Prepare a path to save the converted file save_path = os.path.join(output_dir, "document-output.docx") # Convert Markdown to HTML document document = conv.Converter.convert_markdown(source_path) # Convert HTML document to DOCX file format conv.Converter.convert_html(document, sav.DocSaveOptions(), save_path) ``` -------------------------------- ### Convert EPUB to PDF with Custom Save Options (Python) Source: https://docs.aspose.com/html/python-net/convert-epub-to-pdf This Python example demonstrates how to convert an EPUB file to PDF with custom save options, including page setup and CSS media type. It utilizes the PdfSaveOptions class for detailed control over the output PDF. ```python 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) ``` -------------------------------- ### Render HTML to DOCX using Python Source: https://docs.aspose.com/html/python-net/rendering-device This Python code snippet demonstrates how to render an HTML document to DOCX format using the Aspose.HTML library. It initializes an HTML document, creates a DocDevice for output, and then calls the render_to method. Ensure the Aspose.HTML library is installed. ```python # Render HTML to DOCX using Python import os import aspose.html as ah import aspose.html.rendering.doc as rd # Prepare output directory and path for the DOCX file output_dir = "output/" os.makedirs(output_dir, exist_ok=True) save_path = os.path.join(output_dir, "document.doc") # Initialize an HTML document from URL doc = ah.HTMLDocument("https://docs.aspose.com/html/files/document.html") # Create a DocDevice and specify the output file to render device = rd.DocDevice(save_path) # Render HTML to DOCX doc.render_to(device) ``` -------------------------------- ### Convert HTML to MHTML in One Line with Aspose.HTML for Python Source: https://docs.aspose.com/html/python-net/convert-html-to-mhtml This example demonstrates the simplest method to convert HTML to MHTML using Aspose.HTML for Python. It achieves the conversion in a single line of code by directly passing the input file, save options, and output path to the `convert_html` method. ```python # Convert HTML to MHTML using Python import aspose.html.converters as conv import aspose.html.saving as sav # Convert HTML to MHTML conv.Converter.convert_html("document.html", sav.MHTMLSaveOptions(), "document.mht") ``` -------------------------------- ### Create and Save HTML Document in Python Source: https://docs.aspose.com/html/python-net/installation This Python code demonstrates how to create an HTML document from a string and save it to a file using the Aspose.HTML for Python via .NET library. It requires the 'aspose-html-net' package to be installed. ```python import os from aspose.html import * # Prepare HTML code html_code = "

Hello, World!

" # Setup output directory output_dir = "output/" if not os.path.exists(output_dir): os.makedirs(output_dir) # Initialize a document from the string variable document = HTMLDocument(html_code, ".") # Save the document to disk document.save(os.path.join(output_dir, "create-html-from-string.html")) ``` -------------------------------- ### Convert EPUB to JPG with Custom ImageSaveOptions Source: https://docs.aspose.com/html/python-net/convert-epub-to-jpg This Python code example demonstrates converting an EPUB file to JPG format with custom save options using Aspose.HTML. It allows setting horizontal and vertical resolution, background color, and page size for the output image. ```python import os import aspose.html.converters as conv import aspose.html.saving as sav import aspose.html.rendering.image as rim import aspose.html.drawing as dr import aspose.pydrawing as pd # 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-image.jpg") # Open an existing EPUB file for reading with open(document_path, "rb") as stream: # Create an instance of ImageSaveOptions options = sav.ImageSaveOptions(rim.ImageFormat.JPEG) options.horizontal_resolution = dr.Resolution.from_dots_per_inch(150.0) options.vertical_resolution = dr.Resolution.from_dots_per_inch(150.0) options.background_color = pd.Color.bisque options.page_setup.any_page.size = dr.Size(500, 1000) # Convert EPUB to JPG conv.Converter.convert_epub(stream, options, save_path) ``` -------------------------------- ### Convert HTML to MHTML with Custom Save Options in Python Source: https://docs.aspose.com/html/python-net/convert-html-to-mhtml This Python code example illustrates how to convert HTML to MHTML with customized save options using Aspose.HTML. It specifically shows how to adjust the `max_handling_depth` property within `MHTMLSaveOptions` to control the depth of resource linking during conversion. ```python # Convert HTML to MHTML using Python import os import aspose.html.converters as conv import aspose.html.saving as sav # Prepare directories and paths output_dir = "output/" if not os.path.exists(output_dir): os.makedirs(output_dir) # Prepare HTML code with a link to another file and save it to "document1.html" code = "Hello, World!! click" with open("document1.html", "w") as file: file.write(code) # Prepare HTML code and save it to "document2.html" code = "Hello, World!!" with open("document2.html", "w") as file: file.write(code) save_path = os.path.join(output_dir, "output-options.mht") # Change the value of the resource linking depth to 1 in order to convert document with directly linked resources options = sav.MHTMLSaveOptions() options.resource_handling_options.max_handling_depth = 1 # Convert HTML to MHTML conv.Converter.convert_html("document.html", options, save_path) ``` -------------------------------- ### Convert HTML to DOCX using Python Source: https://docs.aspose.com/html/python-net/convert-html-to-docx This snippet demonstrates the basic conversion of an HTML document to DOCX format using the Aspose.HTML library. It involves loading an HTML file, initializing DocSaveOptions, and then using the Converter.convert_html() method to perform the conversion. ```python import aspose.html as ah import aspose.html.converters as conv import aspose.html.saving as sav # Load an HTML document from a file or URL document = ah.HTMLDocument("document.html") # Initialize saving options options = sav.DocSaveOptions() # Convert HTML to DOCX conv.Converter.convert_html(document, options, "output.docx") ``` -------------------------------- ### Convert MHTML to DOCX with Custom DocSaveOptions Source: https://docs.aspose.com/html/python-net/convert-mhtml-to-docx This Python example demonstrates how to convert an MHTML file to DOCX format using Aspose.HTML for Python via .NET with custom save options. It allows setting page size, document format, and CSS media type using the DocSaveOptions class for more control over the conversion process. ```python # Convert MHTML to DOCX using Python with custom settings 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, "document.mht") save_path = os.path.join(output_dir, "document.docx") # Open an existing MHTML file for reading with open(document_path, "rb") as stream: # Create an instance of DocSaveOptions options = sav.DocSaveOptions() options.page_setup.any_page.size = dr.Size(1000, 800) options.document_format.DOCX options.css.media_type.SCREEN # Convert MHTML to DOCX conv.Converter.convert_mhtml(stream, options, save_path) ``` -------------------------------- ### Convert MHTML to PDF with PdfSaveOptions using Python Source: https://docs.aspose.com/html/python-net/convert-mhtml-to-pdf This Python code example demonstrates how to convert an MHTML file to PDF using Aspose.HTML for Python with custom `PdfSaveOptions`. It allows fine-grained control over page setup (size, margins), CSS media type handling, and JPEG compression quality for embedded images. ```python 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, "document.mht") save_path = os.path.join(output_dir, "document.pdf") # Open an existing MHTML 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 options.jpeg_quality = 100 # Convert MHTML to PDF conv.Converter.convert_mhtml(stream, options, save_path) ``` -------------------------------- ### Apply License from File - Python via .NET Source: https://docs.aspose.com/html/python-net/licensing Initializes a License object and sets it from a local license file. Ensure the license file is accessible at the specified path. This method is straightforward for applying a perpetual license. ```python from aspose.html import License # Initialize license object lic = License() # Set license from file lic.set_license("./license/Aspose.HTML.Python.via.NET.lic") ``` -------------------------------- ### Convert HTML to DOCX in One Line using Python Source: https://docs.aspose.com/html/python-net/convert-html-to-docx This example shows how to convert an HTML file to DOCX format with a single line of Python code. It utilizes the static convert_html() method from the converters module, providing the input HTML file path, DocSaveOptions, and the output DOCX file path directly. ```python # Convert HTML to DOCX using Python import aspose.html.saving as sav import aspose.html.converters as conv # Convert HTML to DOCX conv.Converter.convert_html("document.html", sav.DocSaveOptions(), "document.docx") ``` -------------------------------- ### Convert HTML to Various Formats using Aspose.HTML (Python) Source: https://docs.aspose.com/html/python-net/html-converter This snippet shows how to load an HTML document and convert it to different formats like PDF, DOCX, XPS, BMP, JPEG, GIF, PNG, TIFF, MHTML, and MD. It dynamically selects the appropriate save options based on the desired output format. The conversion is performed using the `aspose.html.converters.Converter.convert_html` method. ```python import aspose.html as ah import aspose.html.converters as conv import aspose.html.saving as sav import aspose.html.rendering.image as rim # Load an HTML document from a file or URL document = ah.HTMLDocument("{{input lower}}") # Initialize saving options if 'PDF' in ['PDF', 'DOCX', 'XPS', 'MHTML', 'MD']: options = sav.PdfSaveOptions() elif 'BMP' in ['BMP', 'JPEG', 'GIF', 'PNG', 'TIFF']: options = sav.ImageSaveOptions(rim.ImageFormat.{{output param2 upper}}) elif 'XHTML' in ['XHTML']: options = sav.HTMLSaveOptions() options.document_type = sav.HTMLSaveOptions.XHTML elif 'DOCX' in ['PDF', 'DOCX', 'XPS', 'MHTML', 'MD']: options = sav.DocSaveOptions() elif 'XPS' in ['PDF', 'DOCX', 'XPS', 'MHTML', 'MD']: options = sav.XpsSaveOptions() elif 'MHTML' in ['PDF', 'DOCX', 'XPS', 'MHTML', 'MD']: options = sav.MHTMLSaveOptions() elif 'MD' in ['PDF', 'DOCX', 'XPS', 'MHTML', 'MD']: options = sav.MarkdownSaveOptions() # Convert HTML to {{output upper}} if 'XHTML' in ['XHTML']: document.save("output.xhtml", options) else: conv.Converter.convert_html(document, options, "output.{{output lower}}") ``` -------------------------------- ### Create Empty HTML Document using Python Source: https://docs.aspose.com/html/python-net/create-a-document Demonstrates how to create an empty HTML document using the default HTMLDocument() constructor in Python and save it to a file. This initializes a basic HTML structure. ```python import os import aspose.html as ah # Setup an output directory and prepare a path to save the document output_dir = "output" if not os.path.exists(output_dir): os.makedirs(output_dir) save_path = os.path.join(output_dir, "document-empty.html") # Initialize an empty HTML document document = ah.HTMLDocument() # Work with the document here... # Save the document to a file document.save(save_path) ``` -------------------------------- ### Create HTML Document from String in Python Source: https://docs.aspose.com/html/python-net/create-a-document This snippet demonstrates how to create an HTML document directly from a string variable in Python. It utilizes the `HTMLDocument` constructor and saves the resulting document to a file. Ensure the `aspose-html` library is installed. ```python import os import aspose.html as ah # Prepare HTML code html_code = "

Hello, World!

" # Setup output directory output_dir = "output" if not os.path.exists(output_dir): os.makedirs(output_dir) # Initialize a document from the string variable document = ah.HTMLDocument(html_code, ".") # Save the document to disk document.save(os.path.join(output_dir, "create-html-from-string.html")) ``` -------------------------------- ### Convert SVG to PNG (Python) Source: https://docs.aspose.com/html/python-net/convert-svg-to-png This example demonstrates a straightforward conversion of an SVG file to a PNG image using Aspose.HTML. It loads a local SVG file, sets default image save options, and converts it to PNG format. ```python import aspose.html.dom.svg as ahsvg import aspose.html.converters as conv import aspose.html.saving as sav import aspose.html.rendering.image as rim # Load an SVG document from a file or URL document = ahsvg.SVGDocument("document.svg") # Initialize saving options options = sav.ImageSaveOptions(rim.ImageFormat.PNG) # Convert SVG to PNG conv.Converter.convert_svg(document, options, "output.png") ``` -------------------------------- ### Load SVG Document from Stream in Python Source: https://docs.aspose.com/html/python-net/create-a-document This Python example shows how to load an SVG document from an in-memory stream using `io.BytesIO` and the `SVGDocument` constructor. It then prints the document's outer HTML and saves it to a file. The `aspose-html` library is required. ```python import io import aspose.html.dom.svg as ahsvg # Initialize an SVG document from a string object svg_content = "" base_uri = "." content_stream = io.BytesIO(svg_content.encode('utf-8')) document = ahsvg.SVGDocument(content_stream, base_uri) # Write the document content to the output stream print(document.document_element.outer_html) # Save the document to a disk document.save("load-from-stream.svg") ``` -------------------------------- ### Convert MHTML to PDF, DOCX, or XPS using Aspose.HTML Source: https://docs.aspose.com/html/python-net/convert-mhtml-to-png This snippet demonstrates converting an MHTML file to PDF, DOCX, or XPS format using the Aspose.HTML library. It initializes the appropriate save options based on the desired output format and then uses the Converter class to perform the conversion. The input MHTML file is read from a stream. ```python import aspose.html.converters as conv import aspose.html.saving as sav # Open an MHTML file for reading with open("{{input lower}}", 'rb') as stream: # Initialize saving options {{#if_output 'PDF'}} options = sav.PdfSaveOptions() {{/if_output}} {{#if_output 'DOCX'}} options = sav.DocSaveOptions() {{/if_output}} {{#if_output 'XPS'}} options = sav.XpsSaveOptions() {{/if_output}} # Convert MHTML to {{output upper}} conv.Converter.convert_mhtml(stream, options, "output.{{output lower}}") ``` -------------------------------- ### Disable JavaScript for HTML to PDF Conversion Source: https://docs.aspose.com/html/python-net/sandboxing This example shows how to disable JavaScript execution when converting HTML to PDF. It initializes a Configuration object, sets the sandbox flag to include Sandbox.SCRIPTS to mark scripts as untrusted, and then converts the HTML document to PDF. ```python import os import aspose.html as ah import aspose.html.converters as conv import aspose.html.saving as sav # Define input and output directories data_dir = "data" output_dir = "output" os.makedirs(output_dir, exist_ok=True) # Create an instance of the Configuration class with ah.Configuration() as config: # Mark "scripts" as an untrusted resource config.security |= ah.Sandbox.SCRIPTS # Initialize an HTML document with the specified configuration html_path = os.path.join(data_dir, "document-with-scripts.html") with ah.HTMLDocument(html_path, config) as doc: # Convert HTML to PDF output_pdf = os.path.join(output_dir, "document-sandbox.pdf") conv.Converter.convert_html(doc, sav.PdfSaveOptions(), output_pdf) ``` -------------------------------- ### XPS Rendering Options Source: https://docs.aspose.com/html/python-net/rendering-options Configure common rendering options for XPS files, including page size and margins. ```APIDOC ## XPS Rendering Options The `XpsRenderingOptions` class inherits from `RenderingOptions` and allows configuration of common options for XPS file generation. ### Properties - **page_setup** (PageSetup) - Configures page size, margins, and other page-related settings. ### Example: Render HTML to XPS with custom page settings ```python import os import aspose.html as ah import aspose.html.rendering.xps as rx import aspose.html.drawing as dr # Setup input and output directories data_dir = "data/" output_dir = "output/" os.makedirs(output_dir, exist_ok=True) # Prepare a path to the source HTML file document_path = os.path.join(data_dir, "document.html") # Initialize the HTML document from the file doc = ah.HTMLDocument(document_path) # Create an instance of XPS Rendering Options options = rx.XpsRenderingOptions() # Set custom page size (5 x 2 inches) options.page_setup.any_page = dr.Page(dr.Size(dr.Length.from_inches(5.0), dr.Length.from_inches(2.0))) # Prepare a path to save the converted file save_path = os.path.join(output_dir, "document-options.xps") # Create an XpsDevice with the specified options and output file device = rx.XpsDevice(options, save_path) # Render HTML to XPS doc.render_to(device) ``` ``` -------------------------------- ### Create New HTML Document Programmatically using Python Source: https://docs.aspose.com/html/python-net/create-a-document Shows how to create a new HTML document programmatically from scratch using the HTMLDocument() constructor without parameters in Python. It includes adding a text node to the body. ```python import os import aspose.html as ah # Prepare the output path to save a document output_dir = "output/" if not os.path.exists(output_dir): os.makedirs(output_dir) document_path = os.path.join(output_dir, "create-new-document.html") # 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(document_path) ``` -------------------------------- ### Render HTML to PDF using Aspose.HTML Source: https://docs.aspose.com/html/python-net/rendering-device This snippet demonstrates how to render an HTML document to a PDF file using the PdfDevice in Aspose.HTML for Python via .NET. It initializes an HTML document, creates a PdfDevice specifying the output path, and then uses the render_to method. Default rendering options are used. ```python import os import aspose.html as ah import aspose.html.rendering.pdf as rp # Prepare output directory and path for the PDF file output_dir = "output/" os.makedirs(output_dir, exist_ok=True) save_path = os.path.join(output_dir, "document.pdf") # Prepare HTML code code = "Hello, World!!" # Initialize an HTML document from the HTML code doc = ah.HTMLDocument(code, ".") # Create a PDF Device and specify the output file to render device = rp.PdfDevice(save_path) # Render HTML to PDF doc.render_to(device) ``` -------------------------------- ### Block Untrusted Images During HTML to PDF Conversion Source: https://docs.aspose.com/html/python-net/sandboxing This example demonstrates how to disable image loading when converting HTML to PDF. It prepares HTML content with an external image, creates a Configuration instance, sets the Sandbox.IMAGES flag to mark images as untrusted, and then converts the HTML to PDF. ```python import os import aspose.html as ah import aspose.html.converters as conv import aspose.html.saving as sav # Prepare HTML code and save it to a file code = "Hello, World!! " \ "" output_dir = "output" os.makedirs(output_dir, exist_ok=True) html_path = os.path.join(output_dir, "sandboxing.html") output_pdf = os.path.join(output_dir, "sandboxing-out.pdf") with open(html_path, "w", encoding="utf-8") as file: file.write(code) # Create an instance of Configuration with ah.Configuration() as configuration: # Mark 'IMAGES' as an untrusted resource configuration.security |= ah.Sandbox.IMAGES # Initialize an HTML document with the specified configuration with ah.HTMLDocument(html_path, configuration) as document: # Convert HTML to PDF conv.Converter.convert_html(document, sav.PdfSaveOptions(), output_pdf) ``` -------------------------------- ### Convert HTML to GIF using Python with Custom Options Source: https://docs.aspose.com/html/python-net/convert-html-to-gif This Python example demonstrates converting an HTML document to a GIF image using Aspose.HTML for Python via .NET. It utilizes `ImageSaveOptions` to specify the output format as GIF and allows for custom settings like resolution and CSS media type. The code loads an HTML file, configures the save options, and then performs the conversion. ```python import os import aspose.html as ah import aspose.html.converters as conv import aspose.html.saving as sav import aspose.html.rendering.image as rim import aspose.html.drawing as dr # Setup directories and define paths output_dir = "output/" input_dir = "data/" if not os.path.exists(output_dir): os.makedirs(output_dir) document_path = os.path.join(input_dir, "document.html") save_path = os.path.join(output_dir, "html-to-image.gif") # Initialize an HTML document from the file document = ah.HTMLDocument(document_path) # Initialize ImageSaveOptions options = sav.ImageSaveOptions(rim.ImageFormat.GIF) options.horizontal_resolution = dr.Resolution.from_dots_per_inch(96.0) options.vertical_resolution = dr.Resolution.from_dots_per_inch(96.0) options.css.media_type.PRINT # Convert HTML to GIF conv.Converter.convert_html(document, options, save_path) ``` -------------------------------- ### Load HTML Document from URL using Python Source: https://docs.aspose.com/html/python-net/create-a-document Demonstrates how to load an HTML document directly from a web URL using the HTMLDocument constructor in Python. Handles potential network errors by throwing a PlatformException. ```python import aspose.html as ah # Load a document from the specified web page document = ah.HTMLDocument("https://docs.aspose.com/html/files/aspose.html") # Write the document content to the output stream print(document.document_element.outer_html) ``` -------------------------------- ### Change Table Border Color Using Inline CSS in Python Source: https://docs.aspose.com/html/python-net/how-to-change-border-color This example shows how to change the border color of a table element in an HTML document using inline CSS with Aspose.HTML for Python. It utilizes the query_selector method to find the table and then sets the 'style' attribute directly on the element. ```python # Change table border color using Python import os import aspose.html as ah # Prepare an output path for saving the document output_dir = "output" os.makedirs(output_dir, exist_ok=True) save_path = os.path.join(output_dir, "change-table-border-color-inline-css.html") # Prepare a path to the source HTML file data_dir = "data" document_path = os.path.join(data_dir, "table.html") # Create an instance of an HTML document with ah.HTMLDocument(document_path) as doc: # Create a CSS selector to select the first table element element = doc.query_selector("table") # Set inline style for the selected element element.set_attribute("style", "border: 2px #0000ff solid;") # Save the modified HTML document to a file doc.save(save_path) ``` -------------------------------- ### Convert EPUB to Various Formats with Default Options (Python) Source: https://docs.aspose.com/html/python-net/convert-epub-to-pdf This snippet demonstrates converting an EPUB file to different output formats (PDF, DOCX, XPS, BMP, JPEG, GIF, PNG, TIFF) using default saving options. It requires the aspose.html library and handles different import statements based on the output format. ```python import aspose.html.converters as conv import aspose.html.saving as sav import aspose.html.rendering.image as rim # Open an existing EPUB file for reading with open("{{input lower}}", 'rb') as stream: # Initialize saving options {{#if_output 'PDF'}} options = sav.PdfSaveOptions() {{/if_output}} {{#if_output 'DOCX'}} options = sav.DocSaveOptions() {{/if_output}} {{#if_output 'XPS'}} options = sav.XpsSaveOptions() {{/if_output}} {{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF'}} options = sav.ImageSaveOptions(rim.ImageFormat.{{output param2 upper}}) {{/if_output}} # Convert EPUB to {{output upper}} conv.Converter.convert_epub(stream, options, "output.{{output lower}}") ``` -------------------------------- ### Convert Markdown to PDF, DOCX, XPS Source: https://docs.aspose.com/html/python-net/convert-markdown-to-image This snippet demonstrates converting a Markdown file to PDF, DOCX, or XPS formats. It utilizes the Aspose.HTML library, first converting the Markdown to an HTML document and then saving it with appropriate save options for the chosen output format. ```python import aspose.html.converters as conv import aspose.html.saving as sav # Convert Markdown to HTML document = conv.Converter.convert_markdown("input.md") # Initialize saving options based on output format # For PDF: options = sav.PdfSaveOptions() # For DOCX: # options = sav.DocSaveOptions() # For XPS: # options = sav.XpsSaveOptions() # Convert HTML to the specified format (e.g., PDF) conv.Converter.convert_html(document, options, "output.pdf") ```