1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-07 16:50:24 +08:00

make lint happy

This commit is contained in:
源文雨
2023-02-23 15:04:09 +08:00
parent fa053fefd4
commit 958a125885
9 changed files with 54 additions and 116 deletions

View File

@@ -1,78 +1,36 @@
# 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.
One of the most functional libraries to read and write .docx (a.k.a. Microsoft Word documents or ECMA-376 Office Open XML) files in Go.
This is a variant optimized and expanded by fumiama. The original repo is [gonfva/docxlib](https://github.com/gonfva/docxlib).
## Introduction
As part of my work for [Basement Crowd](https://www.basementcrowd.com) and [FromCounsel](https://www.fromcounsel.com), we were in need of a basic library to manipulate (both read and write) Microsoft Word documents.
> As part of my work for [Basement Crowd](https://www.basementcrowd.com) and [FromCounsel](https://www.fromcounsel.com), 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:
> The difference with other projects is the following:
- [UniOffice](https://github.com/unidoc/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.
> - [UniOffice](https://github.com/unidoc/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](https://github.com/gingfrederik/docx) only allows to write.
> - [gingfrederik/docx](https://github.com/gingfrederik/docx) only allows to write.
There are also a couple of other projects [kingzbauer/docx](https://github.com/kingzbauer/docx) and [nguyenthenguyen/docx](https://github.com/nguyenthenguyen/docx)
> There are also a couple of other projects [kingzbauer/docx](https://github.com/kingzbauer/docx) and [nguyenthenguyen/docx](https://github.com/nguyenthenguyen/docx)
[gingfrederik/docx](https://github.com/gingfrederik/docx) was a heavy influence (the original structures and the main method come from that project).
> [gingfrederik/docx](https://github.com/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](https://github.com/golang/go/issues/9519).
> 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](https://github.com/golang/go/issues/9519).
Additionally, my requirements go beyond the original structure and a hard fork seemed more sensible.
> 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).
> 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.
> In the mean time, shared as an example in case somebody finds it useful.
## Getting Started
The Introduction above is copied from the original repo. I had evolved that repo again to fit my needs. Here is the supported functions now.
### Install
Go modules supported
```sh
go get github.com/fumiama/docxlib
```
### Usage
See [main](main/main.go) for an example
```
$ go run ./cmd/main
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](getstructure/main.go)
```
$ go build -o docxlib ./cmd/getstructure/ && ./docxlib -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 ./...
```
- [x] parse and save document
- [x] edit simple text (color, size, alignment, ...)
- [x] edit picture
## License

View File

@@ -97,14 +97,14 @@ func (p *Paragraph) AddInlineDrawingFrom(file string) (*Run, error) {
}
// Size of the inline drawing by EMU
func (in *WPInline) Size(w, h int64) {
if in.Extent != nil {
in.Extent.CX = w
in.Extent.CY = h
func (r *WPInline) Size(w, h int64) {
if r.Extent != nil {
r.Extent.CX = w
r.Extent.CY = h
}
if in.Graphic != nil && in.Graphic.GraphicData != nil && in.Graphic.GraphicData.Pic != nil && in.Graphic.GraphicData.Pic.SpPr != nil {
in.Graphic.GraphicData.Pic.SpPr.Xfrm.Ext.CX = w
in.Graphic.GraphicData.Pic.SpPr.Xfrm.Ext.CY = h
if r.Graphic != nil && r.Graphic.GraphicData != nil && r.Graphic.GraphicData.Pic != nil && r.Graphic.GraphicData.Pic.SpPr != nil {
r.Graphic.GraphicData.Pic.SpPr.Xfrm.Ext.CX = w
r.Graphic.GraphicData.Pic.SpPr.Xfrm.Ext.CY = h
}
}
@@ -205,13 +205,13 @@ func (p *Paragraph) AddAnchorDrawingFrom(file string) (*Run, error) {
}
// Size of the anchor drawing by EMU
func (a *WPAnchor) Size(w, h int64) {
if a.Extent != nil {
a.Extent.CX = w
a.Extent.CY = h
func (r *WPAnchor) Size(w, h int64) {
if r.Extent != nil {
r.Extent.CX = w
r.Extent.CY = h
}
if a.Graphic != nil && a.Graphic.GraphicData != nil && a.Graphic.GraphicData.Pic != nil && a.Graphic.GraphicData.Pic.SpPr != nil {
a.Graphic.GraphicData.Pic.SpPr.Xfrm.Ext.CX = w
a.Graphic.GraphicData.Pic.SpPr.Xfrm.Ext.CY = h
if r.Graphic != nil && r.Graphic.GraphicData != nil && r.Graphic.GraphicData.Pic != nil && r.Graphic.GraphicData.Pic.SpPr != nil {
r.Graphic.GraphicData.Pic.SpPr.Xfrm.Ext.CX = w
r.Graphic.GraphicData.Pic.SpPr.Xfrm.Ext.CY = h
}
}

View File

@@ -89,7 +89,7 @@ func main() {
panic(err)
}
size := fileinfo.Size()
doc, err := docxlib.Parse(readFile, int64(size))
doc, err := docxlib.Parse(readFile, size)
if err != nil {
panic(err)
}

View File

@@ -1,3 +1,5 @@
// Package docxlib is one of the most functional libraries to read and write .docx
// (a.k.a. Microsoft Word documents or ECMA-376 Office Open XML) files in Go.
package docxlib
import (

View File

@@ -42,8 +42,7 @@ func (b *Body) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
if tt.Name.Local == "p" {
var value Paragraph
err = d.DecodeElement(&value, &tt)
@@ -89,8 +88,7 @@ func (doc *Document) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
if tt.Name.Local == "body" {
err = d.DecodeElement(&doc.Body, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {

View File

@@ -34,8 +34,7 @@ func (r *Drawing) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "inline":
r.Inline = new(WPInline)
@@ -112,8 +111,7 @@ func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "extent":
r.Extent = new(WPExtent)
@@ -296,8 +294,7 @@ func (w *WPCNvGraphicFramePr) UnmarshalXML(d *xml.Decoder, start xml.StartElemen
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "graphicFrameLocks":
var value AGraphicFrameLocks
@@ -350,8 +347,7 @@ func (a *AGraphic) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "graphicData":
var value AGraphicData
@@ -387,8 +383,7 @@ func (a *AGraphicData) UnmarshalXML(d *xml.Decoder, start xml.StartElement) erro
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "pic":
var value PICPic
@@ -426,8 +421,7 @@ func (p *PICPic) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "nvPicPr":
var value PICNonVisualPicProperties
@@ -476,8 +470,7 @@ func (p *PICNonVisualPicProperties) UnmarshalXML(d *xml.Decoder, start xml.Start
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "cNvPr":
p.NonVisualDrawingProperties.ID = getAtt(tt.Attr, "id")
@@ -514,8 +507,7 @@ func (p *PicCNvPicPr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
}
// Switch based on token type
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "picLocks":
var value APicLocks
@@ -562,8 +554,7 @@ func (p *PICBlipFill) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "blip":
err = d.DecodeElement(&p.Blip, &tt)
@@ -612,8 +603,7 @@ func (a *ABlip) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "alphaModFix":
var value AAlphaModFix
@@ -665,8 +655,7 @@ func (p *PICSpPr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "xfrm":
err = d.DecodeElement(&p.Xfrm, &tt)
@@ -730,8 +719,7 @@ func (a *AXfrm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error)
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "off":
a.Off.X, err = strconv.ParseInt(getAtt(tt.Attr, "x"), 10, 64)
@@ -914,8 +902,7 @@ func (r *WPAnchor) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "simplePos":
r.SimplePosXY = new(WPSimplePos)
@@ -1014,8 +1001,7 @@ func (r *WPPositionH) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "posOffset":
err = d.DecodeElement(&r.PosOffset, &tt)
@@ -1053,8 +1039,7 @@ func (r *WPPositionV) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "posOffset":
err = d.DecodeElement(&r.PosOffset, &tt)

View File

@@ -23,8 +23,7 @@ func (r *Hyperlink) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
if tt.Name.Local == "r" {
err = d.DecodeElement(&r.Run, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {

View File

@@ -20,8 +20,7 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElemen
if err != nil {
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "jc":
p.Justification = &Justification{Val: getAtt(tt.Attr, "val")}
@@ -100,8 +99,7 @@ func (p *Paragraph) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
if err != nil {
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
var elem interface{}
switch tt.Name.Local {
case "hyperlink":

View File

@@ -32,8 +32,7 @@ func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "rPr":
var value RunProperties
@@ -108,8 +107,7 @@ func (r *RunProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err
return err
}
switch tt := t.(type) {
case xml.StartElement:
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "color":
var value Color