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:
1uimport os
2from aspose.html import *
3from aspose.html.saving import *
4from aspose.html.converters import *
5
6# Prepare a path to a source Markdown file
7output_dir = "output/"
8source_path = os.path.join(output_dir, "document.md")
9
10# Prepare a simple Markdown example
11code = "### Hello, World!\nConvert Markdown to DOCX!"
12
13# Create a Markdown file
14with open(source_path, "w") as file:
15 file.write(code)
16
17# Prepare a path to save the converted file
18save_path = os.path.join(output_dir, "document-output.docx")
19
20# Convert Markdown to HTML document
21document = Converter.convert_markdown(source_path)
22
23# Convert HTML document to DOCX file format
24Converter.convert_html(document, 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:
1import os
2from aspose.html import *
3from aspose.html.converters import *
4from aspose.html.saving import *
5from aspose.html.drawing import *
6
7# Setup directories and define paths
8output_dir = "output/"
9input_dir = "data/"
10if not os.path.exists(output_dir):
11 os.makedirs(output_dir)
12document_path = os.path.join(input_dir, "document.md")
13save_path = os.path.join(output_dir, "document.docx")
14
15# Convert Markdown to HTML
16document = Converter.convert_markdown(document_path)
17
18# Create an instance of DocSaveOptions
19options = DocSaveOptions()
20options.document_format.DOCX
21options.page_setup.any_page = Page(Size(600, 800), Margin(10, 10, 10, 10))
22options.font_embedding_rule.FULL
23
24# Convert Markdown to DOCX
25Converter.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!