### Install Aspose.HTML for Python via .NET Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Installs the Aspose.HTML for Python via .NET library using pip. This is the first step to using the library in your Python projects. ```bash pip install aspose-html-net ``` -------------------------------- ### Convert HTML to PDF using Aspose.HTML for Python Source: https://github.com/aspose-html/aspose.html-for-python-via-.net/blob/main/README.md This snippet shows how to convert an HTML document to a PDF file using the Aspose.HTML for Python via .NET library. It utilizes the `Converter.convert_html` method, which requires a document object, conversion options, and the desired output file path. Ensure the Aspose.HTML library is installed and the necessary document and options objects are properly initialized before execution. ```python from aspose.html import Converter from aspose.html.saving import PdfSaveOptions from aspose.html.dom import HtmlDocument # Assume 'document' is an initialized HtmlDocument object and 'options' is a PdfSaveOptions object # Example initialization (replace with your actual document and options setup): document = HtmlDocument() options = PdfSaveOptions() # Convert HTML to PDF converter = Converter() converter.convert_html(document, options, "output.pdf") ``` -------------------------------- ### Create Empty HTML Document with Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Demonstrates how to create a new, blank HTML document using the Aspose.HTML for Python library and save it to a file. This is useful for starting new HTML files programmatically. ```python import aspose.html as ah # Create an empty HTML document document = ah.HTMLDocument() document.save("output/empty-document.html") ``` -------------------------------- ### Convert HTML to PDF using Aspose.HTML for Python Source: https://github.com/aspose-html/aspose.html-for-python-via-.net/blob/main/README.md Shows how to convert an existing HTML document to a PDF file using the Aspose.HTML for Python library. It involves loading an HTML document and setting up PDF saving options before performing 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.PdfSaveOptions() ``` -------------------------------- ### Create HTML File from Scratch using Aspose.HTML for Python Source: https://github.com/aspose-html/aspose.html-for-python-via-.net/blob/main/README.md Demonstrates how to create a new, blank HTML document and add text content to it using the Aspose.HTML for Python library. It initializes an HTML document, creates a text node, appends it to the body, and saves the document to a file. ```python 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") ``` -------------------------------- ### Render HTML to DOCX Device using Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Renders HTML content to Microsoft Word DOCX format using the DocDevice. This method allows conversion of HTML to a Word document with customizable page setup and font embedding rules. ```python import aspose.html as ah import aspose.html.rendering.doc as rd import aspose.html.drawing as dr doc = ah.HTMLDocument("https://docs.aspose.com/html/files/document.html") options = rd.DocRenderingOptions() options.page_setup.any_page = dr.Page(dr.Size(dr.Length.from_inches(8.0), dr.Length.from_inches(10.0))) options.font_embedding_rule = rd.FontEmbeddingRule.FULL device = rd.DocDevice(options, "output/nature-options.docx") doc.render_to(device) ``` -------------------------------- ### Custom Page Setup for PDF Output with Aspose.HTML Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Configures different page sizes for left and right pages in a PDF output document. This allows for customized document layouts. It utilizes 'aspose-html' and 'aspose-html.drawing'. ```python import aspose.html as ah import aspose.html.rendering.pdf as rp import aspose.html.drawing as dr code = "\n
First Page
\n
Second Page
\n
Third Page
\n
Fourth Page
" doc = ah.HTMLDocument(code, ".") options = rp.PdfRenderingOptions() options.page_setup.set_left_right_page( dr.Page(dr.Size(400, 150)), dr.Page(dr.Size(400, 50)) ) device = rp.PdfDevice(options, "output/custom-page-size.pdf") doc.render_to(device) ``` -------------------------------- ### Save SVG Document Programmatically using Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Provides an example of creating and saving an SVG graphic using Aspose.HTML for Python. The SVG code is defined as a string and then rendered to a file. ```python import aspose.html.dom.svg as ahsvg svg_code = """ """ document = ahsvg.SVGDocument(svg_code, '.') document.save("output/save-html-to-svg.svg") ``` -------------------------------- ### Convert Markdown to PDF via HTML (Python) Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Converts a Markdown file to PDF by first converting it to an HTML intermediate format and then to PDF. This process allows for custom page setup and JPEG quality settings for the final PDF output. ```python import aspose.html.converters as conv import aspose.html.saving as sav import aspose.html.drawing as dr # First convert Markdown to HTML document = conv.Converter.convert_markdown("document.md") # Then convert HTML to PDF options = sav.PdfSaveOptions() options.page_setup.any_page = dr.Page(dr.Size(300, 300), dr.Margin(30, 10, 10, 10)) options.jpeg_quality = 100 conv.Converter.convert_html(document, options, "output/markdown-to-pdf.pdf") ``` -------------------------------- ### Convert EPUB to DOCX (Python) Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Converts EPUB e-book files to Microsoft Word DOCX format. This conversion uses the 'aspose-html' library and supports custom page setup for the output document. ```python import aspose.html.converters as conv import aspose.html.saving as sav import aspose.html.drawing as dr with open("input.epub", "rb") as stream: options = sav.DocSaveOptions() options.page_setup.any_page = dr.Page(dr.Size(900, 700), dr.Margin(10, 10, 10, 10)) conv.Converter.convert_epub(stream, options, "output.docx") ``` -------------------------------- ### Convert EPUB to PDF (Python) Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Converts an EPUB e-book file to PDF format. This function utilizes the 'aspose-html' library and allows for custom page setup, including size and margins for the output PDF. ```python import aspose.html.converters as conv import aspose.html.saving as sav import aspose.html.drawing as dr with open("input.epub", "rb") as stream: options = sav.PdfSaveOptions() options.page_setup.any_page = dr.Page(dr.Size(800, 600), dr.Margin(10, 10, 10, 10)) conv.Converter.convert_epub(stream, options, "output.pdf") ``` -------------------------------- ### Convert Pixels to Centimeters using Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Illustrates unit conversion from pixels to centimeters using the Aspose.HTML drawing module in Python. It demonstrates creating a unit value from pixels and then getting its equivalent in centimeters. ```python import aspose.html.drawing as dr px = dr.Unit.from_pixels(1000.0) # 1000 pixels cm = px.get_value(dr.UnitType.CM) # Convert to centimeters print(cm) # Output: approximately 26.46 cm ``` -------------------------------- ### Render HTML to Encrypted PDF using Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Renders HTML content to an encrypted PDF file with password protection and specific permissions. It utilizes PdfRenderingOptions to define page setup and encryption settings, including user/owner passwords and encryption algorithm. ```python import aspose.html as ah import aspose.html.rendering as rn import aspose.html.rendering.pdf as rp import aspose.html.rendering.pdf.encryption as rpe import aspose.html.drawing as dr document = ah.HTMLDocument("data/document.html") renderer = rn.HtmlRenderer() options = rp.PdfRenderingOptions() options.page_setup.any_page = dr.Page(dr.Size(600, 200)) options.encryption = rpe.PdfEncryptionInfo( user_password="user_pwd", owner_password="owner_pwd", permissions=rpe.PdfPermissions.PRINT_DOCUMENT, encryption_algorithm=rpe.PdfEncryptionAlgorithm.RC4_128 ) device = rp.PdfDevice(options, "output/encrypted.pdf") renderer.render(device, document) ``` -------------------------------- ### Convert SVG to PDF using Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Converts SVG vector graphics to PDF format. It takes an SVG document object and PDF save options as input, producing a PDF file. Options include page setup and JPEG quality. ```python import aspose.html.dom.svg as ahsvg import aspose.html.converters as conv import aspose.html.saving as sav import aspose.html.drawing as dr document = ahsvg.SVGDocument("flower.svg") options = sav.PdfSaveOptions() options.page_setup.any_page = dr.Page(dr.Size(600, 500), dr.Margin(20, 20, 10, 10)) options.jpeg_quality = 80 options.is_tagged_pdf = True conv.Converter.convert_svg(document, options, "output/svg-to-pdf.pdf") ``` -------------------------------- ### Create HTML Document from String with Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Shows how to initialize an HTML document from a string containing HTML code. A base URI can be provided for resolving relative resources. ```python import aspose.html as ah # Create HTML document from string html_code = "

Hello, World!

" document = ah.HTMLDocument(html_code, ".") document.save("output/from-string.html") ``` -------------------------------- ### Create HTML Document with Content using Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Illustrates building an HTML document programmatically by creating and appending DOM nodes. This allows for dynamic content generation. ```python import aspose.html as ah with ah.HTMLDocument() as document: # Create a text node and add it to the body text = document.create_text_node("Hello, World!") document.body.append_child(text) document.save("output/new-document.html") ``` -------------------------------- ### Download File from URL using Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Demonstrates how to download binary files from a given URL using the network API provided by Aspose.HTML for Python. It saves the downloaded content to a local file. ```python import os import aspose.html as ah import aspose.html.net as ahnet output_dir = "output/" os.makedirs(output_dir, exist_ok=True) doc = ah.HTMLDocument() url = ah.Url("https://docs.aspose.com/html/images/handlers/message-handlers.png") request = ahnet.RequestMessage(url) response = doc.context.network.send(request) if response.is_success: file_path = os.path.join(output_dir, os.path.basename(url.pathname)) with open(file_path, "wb") as file: file.write(response.content.read_as_byte_array()) ``` -------------------------------- ### Convert HTML to PDF with Default Options Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Converts an HTML document to PDF using the default settings. This is the simplest way to perform the conversion. It requires the HTML document path and an output PDF path. ```python import aspose.html as ah import aspose.html.converters as conv import aspose.html.saving as sav document = ah.HTMLDocument("document.html") options = sav.PdfSaveOptions() conv.Converter.convert_html(document, options, "output.pdf") ``` -------------------------------- ### Save HTML with Resource Handling Depth using Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Demonstrates how to control the depth of linked resource handling when saving an HTML document using Aspose.HTML for Python. This allows for selective inclusion of nested resources. ```python import aspose.html as ah import aspose.html.saving as sav document = ah.HTMLDocument("save-with-linked-file.html") options = sav.HTMLSaveOptions() options.resource_handling_options.max_handling_depth = 1 document.save("output/save-with-linked-file_out.html", options) ``` -------------------------------- ### Load HTML Document from File with Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Loads an existing HTML file from the local filesystem using Aspose.HTML for Python. The loaded document can then be manipulated or saved. ```python import aspose.html as ah document = ah.HTMLDocument("data/document.html") document.save("output/loaded-document.html") ``` -------------------------------- ### Convert HTML to DOCX Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Converts HTML documents to Microsoft Word DOCX format using default settings. This provides a straightforward way to create Word documents from HTML. ```python import aspose.html.converters as conv import aspose.html.saving as sav conv.Converter.convert_html("document.html", sav.DocSaveOptions(), "document.docx") ``` -------------------------------- ### Create SVG Document from String/Stream using Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Shows how to create an SVG document programmatically using Aspose.HTML for Python. It supports creating documents from string content or byte streams. ```python import io import aspose.html.dom.svg as ahsvg svg_content = "" base_uri = "." content_stream = io.BytesIO(svg_content.encode('utf-8')) document = ahsvg.SVGDocument(content_stream, base_uri) document.save("output/CreateSVGDocument_out.svg") ``` -------------------------------- ### Navigate HTML DOM with Aspose.HTML Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Demonstrates how to traverse the HTML Document Object Model (DOM) using node relationships like 'firstChild' and 'nextSibling'. This allows for programmatic inspection and manipulation of HTML structure. Requires 'aspose-html'. ```python import aspose.html as ah html_code = "Hello, World!" with ah.HTMLDocument(html_code, ".") as document: element = document.body.first_child print(element.text_content) # Hello, element = element.next_sibling print(element.text_content) # (whitespace) element = element.next_sibling print(element.text_content) # World! ``` -------------------------------- ### Edit HTML DOM with Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Demonstrates manipulating the HTML Document Object Model (DOM) by creating elements, setting attributes, and appending nodes using Aspose.HTML for Python. ```python import aspose.html as ah document = ah.HTMLDocument() body = document.body # Create a paragraph element with attributes p = document.create_element("p") p.set_attribute("id", "my-paragraph") text = document.create_text_node("The Aspose.Html.Dom namespace provides an API for HTML, XML, or SVG documents.") p.append_child(text) body.append_child(p) document.save("output/edit-document-tree.html") ``` -------------------------------- ### Convert HTML to PDF with Custom Options Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Converts HTML to PDF with advanced customization including page size, margins, resolution, background color, and PDF encryption. This allows for fine-grained control over the output PDF. ```python import aspose.html as ah import aspose.html.converters as conv import aspose.html.saving as sav import aspose.html.drawing as dr import aspose.pydrawing as pd import aspose.html.rendering.pdf.encryption as rpe document = ah.HTMLDocument("data/nature.html") options = sav.PdfSaveOptions() # Set page dimensions and margins options.page_setup.any_page.size = dr.Size(680, 500) options.page_setup.any_page.margin = dr.Margin(0, 10, 10, 10) # Set resolution and quality options.horizontal_resolution = dr.Resolution.from_dots_per_inch(50.0) options.vertical_resolution = dr.Resolution.from_dots_per_inch(50.0) options.background_color = pd.Color.bisque options.jpeg_quality = 90 # Add PDF encryption options.encryption = rpe.PdfEncryptionInfo( user_password="user123", owner_password="owner123", permissions=rpe.PdfPermissions.PRINT_DOCUMENT | rpe.PdfPermissions.EXTRACT_CONTENT, encryption_algorithm=rpe.PdfEncryptionAlgorithm.RC4_128 ) conv.Converter.convert_html(document, options, "output/nature-options.pdf") ``` -------------------------------- ### Extract Text Content with CSS Selectors using Aspose.HTML Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Demonstrates how to use CSS selectors to query and extract specific text content from HTML documents. This is useful for targeted data retrieval. It requires the 'aspose-html' library. ```python from aspose.html import HTMLDocument document = HTMLDocument("https://www.wikipedia.org/") elements = document.query_selector_all("h2") if elements.length > 0: first_heading = elements[0] content = first_heading.text_content.strip() if first_heading.text_content else "" print("Text of the first heading:") print(content) else: print("No

elements found on the page") ``` -------------------------------- ### Load HTML Document from URL with Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Loads an HTML document directly from a web URL using Aspose.HTML for Python. The content is then accessible for processing. ```python import aspose.html as ah # Load document from URL document = ah.HTMLDocument("https://docs.aspose.com/html/files/aspose.html") print(document.document_element.outer_html) ``` -------------------------------- ### Load HTML Document from Stream with Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Creates an HTML document from a byte stream using Aspose.HTML for Python. A base URI is required for resolving any relative resources within the stream. ```python import io import aspose.html as ah content_stream = io.BytesIO(b"

Hello, World!

") base_uri = "." document = ah.HTMLDocument(content_stream, base_uri) document.save("output/from-stream.html") ``` -------------------------------- ### Save HTML to Markdown using Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Shows how to convert an HTML document to Markdown format using Aspose.HTML for Python. The HTML document is loaded and then saved with the specified Markdown format. ```python import aspose.html as ah import aspose.html.saving as sav document = ah.HTMLDocument("data/document.html") document.save("output/html-to-markdown.md", sav.HTMLSaveFormat.MARKDOWN) ``` -------------------------------- ### Render HTML to Image Device using Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Renders HTML content to image files (e.g., JPEG) using the ImageDevice. This method takes an HTML document object and image rendering options to produce an image file. ```python import aspose.html as ah import aspose.html.rendering.image as ri document = ah.HTMLDocument("data/drawing.html") image_options = ri.ImageRenderingOptions(ri.ImageFormat.JPEG) device = ri.ImageDevice(image_options, "output/drawing-output.jpg") doc.render_to(device) ``` -------------------------------- ### Render HTML to PDF Device using Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Renders HTML content directly to a PDF device, offering fine-grained control over the output. This method takes an HTML document object and a PDF device object to generate a PDF file. ```python import aspose.html as ah import aspose.html.rendering.pdf as rp code = "Hello, World!!" doc = ah.HTMLDocument(code, ".") device = rp.PdfDevice("output/document.pdf") doc.render_to(device) ``` -------------------------------- ### Add Internal CSS Styles to HTML with Aspose.HTML for Python Source: https://context7.com/aspose-html/aspose.html-for-python-via-.net/llms.txt Shows how to add internal CSS styles to an HTML document by creating a `