How to convert HTML to a Word DOCX Document in Python

Let’s look at how to use one of our APIs to easily convert HTML to DOCX.

Install the convert API client using pip installer:

pip install git+https://github.com/Cloudmersive/Cloudmersive.APIClient.Python.Convert.git

Call convert_web_html_to_docx, which accepts an HTML string as its input:

from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['Apikey'] = 'Bearer'
# create an instance of the API class
api_instance = cloudmersive_convert_api_client.ConvertWebApi(cloudmersive_convert_api_client.ApiClient(configuration))
input_request = cloudmersive_convert_api_client.HtmlToOfficeRequest() # HtmlToOfficeRequest |
try:
# HTML to DOCX
api_response = api_instance.convert_web_html_to_docx(input_request)
pprint(api_response)
except ApiException as e:
print("Exception when calling ConvertWebApi->convert_web_html_to_docx: %s\n" % e)

And it’s just that easy! This function supports rich text formatting as well as images (we recommend using qualified URLs).

Image for post
Image for post

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store