Categorygithub.com/extrame/docx
modulepackage
0.0.0-20240514014431-8d343ad70f41
Repository: https://github.com/extrame/docx.git
Documentation: pkg.go.dev

# README

Docx library

Yet another library to read and write .docx (a.k.a. Microsoft Word documents or ECMA-376 Office Open XML) files in Go.

About this fork

first, this fork is a testless fork.

second, what I added:

  • style unmarshall

Introduction

As part of my work for Basement Crowd and FromCounsel, we were in need of a basic library to manipulate (both read and write) Microsoft Word documents.

The difference with other projects is the following:

  • UniOffice is probably the most complete but it is also commercial (you need to pay). It also very complete, but too much for my needs.

  • gingfrederik/docx only allows to write.

There are also a couple of other projects kingzbauer/docx and nguyenthenguyen/docx

gingfrederik/docx was a heavy influence (the original structures and the main method come from that project).

However, those original structures didn't handle reading and extending them was particularly difficult due to Go xml parser being a bit limited including a 6 year old bug.

Additionally, my requirements go beyond the original structure and a hard fork seemed more sensible.

The plan is to evolve the library, so the API is likely to change according to my company's needs. But please do feel free to send patches, reports and PRs (or fork).

In the mean time, shared as an example in case somebody finds it useful.

Getting Started

Install

Go modules supported

go get github.com/extrame/docx

Usage

See main for an example

$ go build -o docx ./main
$ ./docx
Preparing new document to write at /tmp/new-file.docx
Document writen.
Now trying to read it
	We've found a new run with the text ->test
	We've found a new run with the text ->test font size
	We've found a new run with the text ->test color
	We've found a new run with the text ->test font size and color
	We've found a new hyperlink with ref http://google.com and the text google
End of main

You can also increase the log level (-logtostderr=true -v=0) and just dump a specific file(-file /tmp/new-file.docx). See getstructure/main

$ go build -o docx ./getstructure/ && ./docx -logtostderr=true -v=0 -file /tmp/new-file.docx
I0511 12:37:40.898493   18466 unpack.go:69] Relations: [...]
I0511 12:37:40.898787   18466 unpack.go:47] Doc: [...]
I0511 12:37:40.899330   18466 unpack.go:58] Paragraph [0xc000026d40 0xc000027d00 0xc000172340]
I0511 12:37:40.899369   18466 main.go:31] There is a new paragraph [...]
	We've found a new run with the text ->test
	We've found a new run with the text ->test font size
	We've found a new run with the text ->test color
I0511 12:37:40.899389   18466 main.go:31] There is a new paragraph [...]
	We've found a new run with the text ->test font size and color
I0511 12:37:40.899396   18466 main.go:31] There is a new paragraph [...]
	We've found a new hyperlink with ref http://google.com and the text google
End of main

Build

$ go build ./...

License

MIT. See LICENSE

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

New generates a new empty docx file that we can manipulate and later on, save.
No description provided by the author
Parse generates a new docx file in memory from a reader You can it invoke from a file readFile, err := os.Open(FILE_PATH) if err != nil { panic(err) } fileinfo, err := readFile.Stat() if err != nil { panic(err) } size := fileinfo.Size() doc, err := docx.Parse(readFile, int64(size)) but also you can invoke from a webform (BEWARE of trusting users data!!!) func uploadFile(w http.ResponseWriter, r *http.Request) { r.ParseMultipartForm(10 << 20) file, handler, err := r.FormFile("file") if err != nil { fmt.Println("Error Retrieving the File") fmt.Println(err) http.Error(w, err.Error(), http.StatusBadRequest) return } defer file.Close() docx.Parse(file, handler.Size) }.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
DocxFile is the structure that allow to access the internal represntation in memory of the doc (either read or about to be written).
Fonts contains the font family.
The hyperlink element contains links.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
A Run is part of a paragraph that has its own style.
RunProperties encapsulates visual properties of a run.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
The Text object contains the actual text.