Skip to content

Commit 9c8e795

Browse files
Merge pull request #29 from securityguy/master
Fix example code so that it compiles and added as a go file
2 parents 1903da0 + 711411f commit 9c8e795

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

README.md

+12-8
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@
33
The following constitutes the bare minimum required to replace text in DOCX document.
44
``` go
55

6+
package main
7+
68
import (
9+
"strconv"
10+
711
"github.com/nguyenthenguyen/docx"
812
)
913

1014
func main() {
1115
// Read from docx file
12-
r, err := docx.ReadDocxFile("./template.docx")
16+
r, err := docx.ReadDocxFile("./TestDocument.docx")
1317
// Or read from memory
1418
// r, err := docx.ReadDocxFromMemory(data io.ReaderAt, size int64)
15-
19+
1620
// Or read from a filesystem object:
1721
// r, err := docx.ReadDocxFromFS(file string, fs fs.FS)
18-
22+
1923
if err != nil {
2024
panic(err)
2125
}
@@ -38,12 +42,12 @@ func main() {
3842

3943
docx3 := r.Editable()
4044
//Currently only swaps apples for apples i.e. png to png, and not png to jpeg etc.
41-
d.ReplaceImage("word/media/image1.png", "./new.png")
42-
docx3.WriteToFile("./new_result_3.docx")
43-
45+
docx3.ReplaceImage("word/media/image1.png", "./new.png")
46+
4447
// replace the last image
45-
imageIndex := docxRaw.ImagesLen()
46-
d.ReplaceImage("word/media/image" strconv.Itoa(imageIndex) + ".png", "./new.png")
48+
imageIndex := docx3.ImagesLen()
49+
docx3.ReplaceImage("word/media/image"+strconv.Itoa(imageIndex)+".png", "./new.png")
50+
docx3.WriteToFile("./new_result_3.docx")
4751

4852
r.Close()
4953
}

example/example.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/nguyenthenguyen/docx"
7+
)
8+
9+
// Note - this sample code does not perform error checking, etc.
10+
11+
func main() {
12+
13+
// Read from docx file
14+
r, err := docx.ReadDocxFile("./TestDocument.docx")
15+
// Or read from memory
16+
// r, err := docx.ReadDocxFromMemory(data io.ReaderAt, size int64)
17+
18+
// Or read from a filesystem object:
19+
// r, err := docx.ReadDocxFromFS(file string, fs fs.FS)
20+
21+
if err != nil {
22+
panic(err)
23+
}
24+
docx1 := r.Editable()
25+
// Replace like https://golang.org/pkg/strings/#Replace
26+
docx1.Replace("old_1_1", "new_1_1", -1)
27+
docx1.Replace("old_1_2", "new_1_2", -1)
28+
docx1.ReplaceLink("http://example.com/", "https://github.com/nguyenthenguyen/docx", 1)
29+
docx1.ReplaceHeader("out with the old", "in with the new")
30+
docx1.ReplaceFooter("Change This Footer", "new footer")
31+
docx1.WriteToFile("./new_result_1.docx")
32+
33+
docx2 := r.Editable()
34+
docx2.Replace("old_2_1", "new_2_1", -1)
35+
docx2.Replace("old_2_2", "new_2_2", -1)
36+
docx2.WriteToFile("./new_result_2.docx")
37+
38+
// Or write to ioWriter
39+
// docx2.Write(ioWriter io.Writer)
40+
41+
docx3 := r.Editable()
42+
//Currently only swaps apples for apples i.e. png to png, and not png to jpeg etc.
43+
docx3.ReplaceImage("word/media/image1.png", "./new.png")
44+
45+
// replace the last image
46+
imageIndex := docx3.ImagesLen()
47+
docx3.ReplaceImage("word/media/image"+strconv.Itoa(imageIndex)+".png", "./new.png")
48+
docx3.WriteToFile("./new_result_3.docx")
49+
50+
r.Close()
51+
}

0 commit comments

Comments
 (0)