How to Convert a Word DOCX Document to Text (TXT) using Go

Cloudmersive
2 min readNov 3, 2022

--

The proprietary formatting used in Word DOCX documents makes it difficult to edit those documents without purchasing Microsoft applications. The rules that govern TXT files are far less complex, however, and as a result, converting DOCX content to TXT format is a great way of increasing that content’s accessibility (not to mention, it’s also a great way of separating text from other content types within a DOCX file). Thankfully, our DOCX to TXT Conversion API is free to use and makes that format transition seamlessly. This API will return a plain text string containing all the text from your original DOCX file, and nothing more.

Below, I’ll demonstrate how you can call this API using complementary, ready-to-run code examples in Go. Beyond copying these examples into your file, all you’ll need to do is register a free account on our website to get your free-tier API key, which you can then copy into your code as a string to authenticate access.

To call this API, all you need to do is include the below code in your file and pass your DOCX inputs through the inputFile parameter. If you’d like to specify how whitespace should be handled in your text string, you can set textFormattingMode equal to the string ‘preserveWhitespace,’ which will try to preserve whitespace within the document and relative position of text within the document (the default here is ‘minimizeWhitespace’, which will usually not include any additional spaces in the document).

package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/convert/docx/to/txt"
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("inputFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("textFormattingMode", "<string>")
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
req.Header.Set("Content-Type", writer.FormDataContentType())
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))
}

After that, you’re all done. Simple as can be!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet