docc

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2022 License: MIT Imports: 10 Imported by: 1

README

docc

Simple ".docx" converter implemented by Go. Convert ".docx" to plain text.

Note

This repository is an alpha version. Some disruptive changes could be applied.

License

MIT

Features

  • Less dependency.
  • No need for Microsoft Office.
  • Only on limited environment, also ".doc" could be converted.
    • Windows in which MS Office has been installed.

Usage

As a package

This is a simple example to read all paragraphs.

package main

import "github.com/tenkoh/go-docc"

func main(){
    fp := filepath.Clean("./target.docx")
    r, err := NewReader(fp)
    if err != nil {
        panic(err)
    }
    defer r.Close()
    ps, _ := r.ReadAll()
    // do something with ps:[]string
}

If you want read the document by a paragraph, the below example is useful.

package main

import "github.com/tenkoh/go-docc"

func main(){
    fp := filepath.Clean("./target.docx")
    r, err := NewReader(fp)
    if err != nil {
        panic(err)
    }
    defer r.Close()
    
    for {
        p, err := r.Read()
        if err == io.EOF {
            return
        } else if err != nil {
            panic(err)
        }
        // do something with p:string
    }
}

Before compiling, you shall execute go mod tidy or go get github.com/tenkoh/go-doccto get this package.

As a binary

go install is available.

go install github.com/tenkoh/go-docc/cmd/docc@latest

Then, docc command could be used. This is a simple example.

docc target.docx > plain.txt

Contribution

Your contribution is really welcomed!

Author

tenkoh

Documentation

Index

Constants

View Source
const (
	MSOFFICE_PATH = "C:/Program Files (x86)/Microsoft Office"
)

Variables

View Source
var ErrNotSupportFormat = errors.New("the file is not supported")
View Source
var ErrNotSupportOldDoc = errors.New("not supporting .doc file")

Functions

This section is empty.

Types

type Document

type Document struct {
	XMLName xml.Name `xml:"document"`
	Body    struct {
		P []struct {
			R []struct {
				T struct {
					Text  string `xml:",chardata"`
					Space string `xml:"space,attr"`
				} `xml:"t"`
			} `xml:"r"`
		} `xml:"p"`
	} `xml:"body"`
}

type Paragraph added in v0.0.3

type Paragraph struct {
	R []struct {
		T struct {
			Text  string `xml:",chardata"`
			Space string `xml:"space,attr"`
		} `xml:"t"`
	} `xml:"r"`
}

type Reader added in v0.0.3

type Reader struct {
	// contains filtered or unexported fields
}

func NewReader added in v0.0.3

func NewReader(docxPath string) (*Reader, error)

NewReader generetes a Reader struct. After reading, the Reader struct shall be Close().

func (*Reader) Close added in v0.0.3

func (r *Reader) Close() error

func (*Reader) Read added in v0.0.3

func (r *Reader) Read() (string, error)

Read reads the .docx file by a paragraph. When no paragraphs are remained to read, io.EOF error is returned.

func (*Reader) ReadAll added in v0.0.3

func (r *Reader) ReadAll() ([]string, error)

ReadAll reads the whole .docx file.

Directories

Path Synopsis
cmd
docc Module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL