How to Autodetect a Document and Convert it to Text (TXT) using Go
Removing and de-formatting text from a document is a simple process, and it should only take a single step to accomplish. Thankfully, our Document to Text API provides a one-step, one-size-fits-all solution which can greatly improve the efficiency of your application’s text conversion services. This API will automatically detect your input content type (dozens of common file formats are supported, including DOCX, XLSX, PPT, PDF, and more) and return only plain, unformatted text from within that document (please note: if you use an Excel file, all worksheets in your file will be converted). It’s easy to take advantage of this API, too: all you need to do is register a free account on our website (this yields a free-tier API key valid for 800 API calls per month) and then copy the complementary, ready-to-run Go code examples provided below into your file.
To call this API, we’ll just need to copy & paste the below into our file, and then configure our file path & API key headers where indicated. If you’d like to specify how the response should handle whitespace, you can add the string “minimizeWhitespace” into the textFormattingMode header (default setting is “preserveWhitespace”).
package main
import (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.cloudmersive.com/convert/autodetect/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))
}
And that’s all — no more work required. You can put your text conversion needs to rest!