Convert an HTML Document to Word in Go

Cloudmersive
2 min readJul 27, 2021

Are you working with HTML files and running into storage or sharing issues? If so, it may be easier to transform the HTML document to a Word DOCX file. This will preserve the message of the content, while providing a smaller file size and a much more functional format for sharing. In this brief tutorial, we will demonstrate how you can use an API in Go to instantly convert an HTML file to DOCX, providing a simple solution to your problem.

To ensure the process runs smoothly, you will need to input the HTML file and your API key into the following code:

package mainimport (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/convert/html/to/docx"
method := "POST"
payload := strings.NewReader("Html=%3Cstring%3E")client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

And you’re done! If you need to retrieve your API key, you can do so by registering for a free account on the Cloudmersive website; this provides 800 calls/month across our multitude of APIs.

--

--

Cloudmersive

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