How to Convert PowerPoint PPTX Presentations to Text (TXT) using Go
PowerPoint presentations often contain a wide range of multimedia files accompanied by instances of rich text. As such, stripping text from these files for any reason (whether it be for reviewing copy, saving copy for redundancy, etc.) could take a lot longer than you might be prepared to spend doing it. Thankfully, our PPTX to TXT API makes that process incredibly simple. This API will accept your PPTX file as input, and in return, it will provide a plain text string containing all text elements contained within the original file (with formatting removed), and nothing more. For your reference, I’ve included an example response JSON:
{
"Successful": true,
"TextResult": "string"
}
Using the Go code examples provided below, you can quickly take advantage of this API & structure your API call with ease. All you’ll need in addition to these code examples is a Cloudmersive API key to authenticate the service; you can get one by registering a free account on our website (free accounts yield a limit of 800 API calls per month & have no additional commitments once that limit is reached).
To call the API, let’s include the code examples below in our file, and then let’s properly configure our file input & API key input fields:
package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)func main() {url := "https://api.cloudmersive.com/convert/pptx/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("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))
}
Just like that, you’re all done — no more code required. Easy as pie!