forked from nguyenthenguyen/docx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocx.go
240 lines (204 loc) · 5.22 KB
/
docx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package docx
import (
"archive/zip"
"bufio"
"bytes"
"encoding/xml"
"errors"
"io"
"io/ioutil"
"os"
"strings"
)
type ReplaceDocx struct {
zipReader *zip.ReadCloser
content string
headers map[string]string
footers map[string]string
}
func (r *ReplaceDocx) Editable() *Docx {
return &Docx{
files: r.zipReader.File,
content: r.content,
headers: r.headers,
footers: r.footers,
}
}
func (r *ReplaceDocx) Close() error {
return r.zipReader.Close()
}
type Docx struct {
files []*zip.File
content string
headers map[string]string
footers map[string]string
}
func (d *Docx) Replace(oldString string, newString string, num int) (err error) {
oldString, err = encode(oldString)
if err != nil {
return err
}
newString, err = encode(newString)
if err != nil {
return err
}
d.content = strings.Replace(d.content, oldString, newString, num)
return nil
}
func (d *Docx) ReplaceHeader(oldString string, newString string) (err error) {
return replaceHeaderFooter(d.headers, oldString, newString)
}
func (d *Docx) ReplaceFooter(oldString string, newString string) (err error) {
return replaceHeaderFooter(d.footers, oldString, newString)
}
func (d *Docx) WriteToFile(path string) (err error) {
var target *os.File
target, err = os.Create(path)
if err != nil {
return
}
defer target.Close()
err = d.Write(target)
return
}
func (d *Docx) Write(ioWriter io.Writer) (err error) {
w := zip.NewWriter(ioWriter)
for _, file := range d.files {
var writer io.Writer
var readCloser io.ReadCloser
writer, err = w.Create(file.Name)
if err != nil {
return err
}
readCloser, err = file.Open()
if err != nil {
return err
}
if file.Name == "word/document.xml" {
writer.Write([]byte(d.content))
} else if strings.Contains(file.Name, "header") && d.headers[file.Name] != "" {
writer.Write([]byte(d.headers[file.Name]))
} else if strings.Contains(file.Name, "footer") && d.footers[file.Name] != "" {
writer.Write([]byte(d.footers[file.Name]))
} else {
writer.Write(streamToByte(readCloser))
}
}
w.Close()
return
}
func replaceHeaderFooter(headerFooter map[string]string, oldString string, newString string) (err error) {
oldString, err = encode(oldString)
if err != nil {
return err
}
newString, err = encode(newString)
if err != nil {
return err
}
for k, _ := range headerFooter {
headerFooter[k] = strings.Replace(headerFooter[k], oldString, newString, -1)
}
return nil
}
func ReadDocxFile(path string) (*ReplaceDocx, error) {
reader, err := zip.OpenReader(path)
if err != nil {
return nil, err
}
content, err := readText(reader.File)
if err != nil {
return nil, err
}
headers, footers, _ := readHeaderFooter(reader.File)
return &ReplaceDocx{zipReader: reader, content: content, headers: headers, footers: footers}, nil
}
func readHeaderFooter(files []*zip.File) (headerText map[string]string, footerText map[string]string, err error) {
h, f, err := retrieveHeaderFooterDoc(files)
if err != nil {
return map[string]string{}, map[string]string{}, err
}
headerText, err = buildHeaderFooter(h)
if err != nil {
return map[string]string{}, map[string]string{}, err
}
footerText, err = buildHeaderFooter(f)
if err != nil {
return map[string]string{}, map[string]string{}, err
}
return headerText, footerText, err
}
func buildHeaderFooter(headerFooter []*zip.File) (map[string]string, error) {
headerFooterText := make(map[string]string)
for _, element := range headerFooter {
documentReader, err := element.Open()
if err != nil {
return map[string]string{}, err
}
text, err := wordDocToString(documentReader)
if err != nil {
return map[string]string{}, err
}
headerFooterText[element.Name] = text
}
return headerFooterText, nil
}
func readText(files []*zip.File) (text string, err error) {
var documentFile *zip.File
documentFile, err = retrieveWordDoc(files)
if err != nil {
return text, err
}
var documentReader io.ReadCloser
documentReader, err = documentFile.Open()
if err != nil {
return text, err
}
text, err = wordDocToString(documentReader)
return
}
func wordDocToString(reader io.Reader) (string, error) {
b, err := ioutil.ReadAll(reader)
if err != nil {
return "", err
}
return string(b), nil
}
func retrieveWordDoc(files []*zip.File) (file *zip.File, err error) {
for _, f := range files {
if f.Name == "word/document.xml" {
file = f
}
}
if file == nil {
err = errors.New("document.xml file not found")
}
return
}
func retrieveHeaderFooterDoc(files []*zip.File) (headers []*zip.File, footers []*zip.File, err error) {
for _, f := range files {
if strings.Contains(f.Name, "header") {
headers = append(headers, f)
}
if strings.Contains(f.Name, "footer") {
footers = append(footers, f)
}
}
if len(headers) == 0 && len(footers) == 0 {
err = errors.New("headers[1-3].xml file not found and footers[1-3].xml file not found.")
}
return
}
func streamToByte(stream io.Reader) []byte {
buf := new(bytes.Buffer)
buf.ReadFrom(stream)
return buf.Bytes()
}
func encode(s string) (string, error) {
var b bytes.Buffer
enc := xml.NewEncoder(bufio.NewWriter(&b))
if err := enc.Encode(s); err != nil {
return s, err
}
return strings.Replace(strings.Replace(b.String(), "<string>", "", 1), "</string>", "", 1), nil
}