Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To convert HTML to DOCX in Python, call Converter.convert_html() with the HTML source, DocSaveOptions, and an output path. The source can be an HTML file, an HTMLDocument, inline HTML with a base URI, or a URL supported by the selected overload.
Aspose.HTML for Python via .NET converts HTML and CSS into an editable Microsoft Word DOCX document. Use the default settings for a direct conversion or configure page layout, CSS processing, background color, and font embedding with DocSaveOptions.
The basic conversion requires a source HTML file, DOCX save options, and an output path:
DocSaveOptions object.Converter.convert_html() to convert the HTML content to DOCX.1# Convert HTML to DOCX in Python
2
3import aspose.html.saving as sav
4import aspose.html.converters as conv
5
6# Convert HTML to DOCX
7conv.Converter.convert_html("document.html", sav.DocSaveOptions(), "document.docx")This overload reads the local HTML file, applies the default DOCX settings, and writes the result to the specified path.
Use a Url object to convert an online HTML page to an editable Word document:
Url object for the webpage.DocSaveOptions and specify the output path.Converter.convert_html(). 1import os
2import aspose.html as ah
3import aspose.html.converters as conv
4import aspose.html.saving as sav
5
6output_dir = "output"
7os.makedirs(output_dir, exist_ok=True)
8
9url = ah.Url("https://docs.aspose.com/html/files/aspose.html")
10output_path = os.path.join(output_dir, "document-from-url.docx")
11
12options = sav.DocSaveOptions()
13conv.Converter.convert_html(url, options, output_path)The application must have network access to the page and its linked resources. This example uses a small static HTML file; a complex website can take longer to process while external resources are loaded.
Use DocSaveOptions when the Word document requires custom page settings, a page background, or embedded fonts:
HTMLDocument.DocSaveOptions and configure the required output settings.Converter.convert_html().The following example uses an A4 page with 2 cm margins, applies a background color, and enables full font embedding:
1import os
2import aspose.html as ah
3import aspose.html.converters as conv
4import aspose.html.drawing as dr
5import aspose.html.saving as sav
6import aspose.pydrawing as pd
7
8input_path = os.path.join("data", "document.html")
9output_path = os.path.join("output", "document.docx")
10os.makedirs("output", exist_ok=True)
11
12with ah.HTMLDocument(input_path) as document:
13 options = sav.DocSaveOptions()
14 options.page_setup.any_page.size = dr.Size(
15 dr.Length.from_centimeters(21.0),
16 dr.Length.from_centimeters(29.7)
17 )
18 options.page_setup.any_page.margin = dr.Margin(
19 dr.Length.from_centimeters(2.0),
20 dr.Length.from_centimeters(2.0),
21 dr.Length.from_centimeters(2.0),
22 dr.Length.from_centimeters(2.0)
23 )
24 options.background_color = pd.Color.bisque
25 options.font_embedding_rule = options.font_embedding_rule.FULL
26
27 conv.Converter.convert_html(document, options, output_path)The example customizes the page layout, background color, and font embedding. Full font embedding can improve font availability in Word but may increase the DOCX file size.
The DocSaveOptions class extends the common document rendering settings with options used for DOCX output.
| Property | Use it to control |
|---|---|
| page_setup | Page size, margins, and page layout. |
| font_embedding_rule | Whether fonts are embedded in the DOCX file. The default value is NONE; FULL can increase the output file size. |
| css | CSS processing, including the media type used during conversion. Print media is used by default. |
| background_color | The background color of the rendered pages. The default is transparent. |
| document_format | The output document format. DocSaveOptions produces DOCX. |
| horizontal_resolution and vertical_resolution | Resolution of raster images generated for graphical filter effects. Both values default to 300 dpi; they do not set the resolution of the entire DOCX document. |
For relative images, stylesheets, and other linked resources, the HTML source must have a valid document location or base URI so that the resource URLs can be resolved during conversion.
Yes. Use the Converter.convert_html() overload that accepts the HTML string, a base URI, DocSaveOptions, and an output path. The base URI is required when the string contains relative URLs for images, stylesheets, fonts, or other resources.
Yes. Pass a Url object, DocSaveOptions, and the output DOCX path to Converter.convert_html(). Remote resources must be accessible while the webpage is loaded.
The converter loads the webpage and its linked resources before producing the DOCX file. Large images, web fonts, scripts, slow servers, or unavailable resources can delay completion. Use a stable URL and verify that every required resource is accessible from the application environment.
Relative resource URLs must resolve against the document location or base URI, and remote resources must be accessible while the HTML is loaded. Fonts must also be available to the application or embedded by setting font_embedding_rule to FULL.
Not always. HTML uses a browser-oriented flow layout, while DOCX uses a word-processing document model. Aspose.HTML preserves supported content and styling where the target format allows it, but complex browser-specific layouts may look different in Word.
No. DocSaveOptions produces DOCX output. It does not provide the legacy binary DOC format.
Use the free online HTML to DOCX Converter below for occasional manual conversions. For programmatic conversion, you can evaluate the library without a license or follow the licensing guide to request a temporary license.
Use the free online HTML to DOCX Converter for quick manual conversion without writing code. Use Aspose.HTML for Python via .NET when HTML-to-DOCX conversion must run in a Python application, service, or document-processing workflow.
Download complete examples and data files from GitHub.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.