Convert Markdown to DOCX in Python
Aspose.HTML for Python via .NET provides the Aspose.Html.Converters namespace that offers easy access to various conversion methods.
This article provides information on how to convert Markdown to DOCX using the Aspose.HTML Python library. You will learn about the supported Markdown to DOCX conversion scenarios and consider Python code examples to illustrate them. Also, you can try an Online Markdown Converter to test the Aspose.HTML functionality and convert Markdown on the fly.
Note: All of the convert_markdown() methods allow for the basic Markdown to HTML conversion. Conversions from Markdown to other formats go through the Markdown to HTML conversion stage.
Online Markdown Converter
You can convert Markdown to other formats with Aspose.HTML in real time. Load a Markdown file, select the output format and run the example. The save options are configured by default. You will instantly receive the conversion result as a separate file.
If you want to convert Markdown to DOCX programmatically, please see the following Python code examples.
Convert Markdown to DOCX in Python
Markdown conversions to other formats go through an intermediate Markdown to HTML conversion stage. To convert Markdown to DOCX, you should follow a few steps:
- Prepare a source Markdown document. In the example, we create a Markdown file from code.
- Convert Markdown to HTML. Use the convert_markdown() method to save Markdown as an HTML document.
- Use one of the convert_html() methods and pass to it the HTMLDocument, DocSaveOptions, and output file path.
If your case is to create a Markdown document from a user string directly in your code and convert it to a DOCX file, the following example could help you:
1# Convert Markdown to DOCX using Python
2
3import os
4import aspose.html.converters as conv
5import aspose.html.saving as sav
6
7# Prepare a path to a source Markdown file
8output_dir = "output/"
9source_path = os.path.join(output_dir, "document.md")
10
11# Prepare a simple Markdown example
12code = "### Hello, World!\nConvert Markdown to DOCX!"
13
14# Create a Markdown file
15with open(source_path, "w") as file:
16 file.write(code)
17
18# Prepare a path to save the converted file
19save_path = os.path.join(output_dir, "document-output.docx")
20
21# Convert Markdown to HTML document
22document = conv.Converter.convert_markdown(source_path)
23
24# Convert HTML document to DOCX file format
25conv.Converter.convert_html(document, sav.DocSaveOptions(), save_path)
Convert Markdown to DOCX using DocSaveOptions
The process of converting Markdown to DOCX can be flexibly customized. The DocSaveOptions class is a powerful configuration tool that allows you to fine-tune converting HTML documents to the DOCX format. It includes the following properties:
- page_setup – This property lets you define the page’s layout, including page size, margins, and other layout aspects, ensuring the output document matches the desired format.
- horizontal_resolution – This property sets or gets the horizontal resolution for internal images in pixels per inch. By default, it is 300 dpi. Higher resolutions can produce better rendering quality but larger file sizes. This property allows you to control the trade-offs between quality and file size.
- vertical_resolution – This property sets or gets the vertical resolution for internal images in pixels per inch. By default, it is 300 dpi. Similar to
horizontal_resolution,
this controls the vertical resolution of documents, affecting their clarity and overall size. - background_color – This property allows you to set the background color for the rendered output. If not set, the default background is transparent.
- css – This property gets a CssOptions object, which is used to configure CSS properties processing. For example, the
css.media_type
property specifies different styles for different media types, ensuring that the correct CSS rules are applied based on how the document is being rendered. - font_embedding_rule – This property sets the rule for embedding fonts and controls whether and how fonts are embedded in the output document. The default value is
NONE
. - document_format – This property sets the file format of the output document. The default is DOCX.
The following code snippet shows how to convert Markdown to DOCX with custom save options:
1# Convert Markdown to DOCX using Python with custom settings
2
3import os
4import aspose.html.converters as conv
5import aspose.html.saving as sav
6import aspose.html.drawing as dr
7
8# Setup directories and define paths
9output_dir = "output/"
10input_dir = "data/"
11if not os.path.exists(output_dir):
12 os.makedirs(output_dir)
13document_path = os.path.join(input_dir, "document.md")
14save_path = os.path.join(output_dir, "md-to-docx-with-save-options.docx")
15
16# Convert Markdown to HTML
17document = conv.Converter.convert_markdown(document_path)
18
19# Create an instance of DocSaveOptions
20options = sav.DocSaveOptions()
21options.page_setup.any_page = dr.Page(dr.Size(900, 700), dr.Margin(40, 10, 10, 10))
22options.document_format.DOCX
23options.font_embedding_rule.FULL
24
25# Convert HTML to DOCX
26conv.Converter.convert_html(document, options, save_path)
In the example, we use the document_format
, page_setup
, and font_embedding_rule
properties. To learn more about DocSaveOptions
, please read the
Fine-Tuning Converters article.
Aspose.HTML offers a free online MD to DOCX Converter that converts Markdown to DOCX file with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!