I usually develop a little in-house tool in Golang The other day, I received such a consultation from a junior "Every week, I just have to paste a document written in Word into Excel and put it together ..."
** "Moreover, .docx and .doc are mixed ..." **
Oh...
When I looked it up, I found a nice package, so I tried using it
A program that simply reads a Word file and outputs it to the console
package main
import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"strings"
	"code.sajari.com/docconv"
)
//WordContent Preserves the content retrieved from a Word file
type WordContent struct {
	body string
}
// String fmt.Println()Called when outputting with
func (wc *WordContent) String() string {
	return strings.TrimSpace(wc.body)
}
//FileRead Reads the file in the path specified by filename and returns its contents
func FileRead(filename string) (*WordContent, error) {
	f, err := os.Open(filename)
	if err != nil {
		return nil, fmt.Errorf("fail to open file: %v", err)
	}
	defer f.Close()
	switch filepath.Ext(filename) {
	case ".docx":
		content, _, err := docconv.ConvertDocx(f)
		wc := WordContent{content}
		return &wc, err
	case ".doc":
		content, _, err := docconv.ConvertDoc(f)
		wc := WordContent{content}
		return &wc, err
	}
	return nil, nil
}
func main() {
	filename1 := "samples/sample.docx"
	wc, err := FileRead(filename1)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(wc)
	fmt.Println("----------------")
	filename2 := "samples/sample.doc"
	wc, err = FileRead(filename2)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(wc)
}
The point is that the method to read the file differs depending on the extension.
.docx -> docconv.ConvertDocx().doc -> docconv.ConvertDoc()I hope it works
Recommended Posts