Convert a Word DOCX File to HTML in Ruby

Cloudmersive
2 min readJun 14, 2021

--

While the DOCX format may ideal for creating a text document, HTML is one of the best formats for web-based documents. If you need to translate a DOCX file to an HTML format, the various formatting and styles offered in Word tend to make the process complicated and time-consuming. To simplify the conversion, you can use the following API in Ruby to automatically transform a DOCX file to HTML.

We will initiate the operation by adding the Ruby client to our Gemfile:

gem 'cloudmersive-convert-api-client', '~> 2.1.6'

Now we are ready to input our DOCX file and API key into the following code:

require 'cloudmersive-convert-api-client'
# setup authorization
CloudmersiveConvertApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
end
api_instance = CloudmersiveConvertApiClient::ConvertDocumentApi.newinput_file = File.new('/path/to/inputfile') # File | Input file to perform the operation on.begin
#Convert Word DOCX Document to HTML Document
result = api_instance.convert_document_docx_to_html(input_file)
p result
rescue CloudmersiveConvertApiClient::ApiError => e
puts "Exception when calling ConvertDocumentApi->convert_document_docx_to_html: #{e}"
end

If you need to obtain an API key, head to the Cloudmersive website to register for a free account; this provides 800 monthly calls across our extensive library of APIs.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet