docx-templater
This repo is a continuation of smarterlab-docx repository, the original repository replaces text and inserts tables(with a tricky way), now it replaces images as well.
-
To replace text use {{somename}} in your docx file and then use Replace() function of the package.
-
To Add table(tricky a bit): you need to extract the docx file, then add {{anothername}} in the document.xml file then re-zip it (Note: office will remove your variable if you edit the document using office suite software, edit from the inside instead).
-
For replacing images you will need to know the name of the image you want to replacec from media folder (take the name from inside the zip file) then just initialize it and append it in the Images object.
See The demo below 👇
TODO
These are the currently major concerns to look at for this package
- Refactor the code
- Write Unit Tests
- Solve Table Issue
- Refactor table code
Installation
go get -u github.com/Sulaiman-SM/docx-templater
Usage (demo)
simple usage is below or you can have a look at demo directory in the repo
example usage of the package
package main
import (
"fmt"
"strconv"
"github.com/Sulaiman-SM/docx-templater/docx"
)
func main() {
// example open docx file to manipulate
x, err := docx.Open("sample.docx")
if err != nil {
panic(err)
}
// exmple replace text
x.ReplaceText("TOBEREPLACED", "Hello, World!", -1)
// example replace image
img := []docx.ReplaceImages{
{Src: "./gopher.png", Dst: "image1.gif"},
}
x.ReplaceImage(img)
// exmple tables: either use table 1 or table 2
table1(x)
// table2(docxObj)
// save the manipulated docx
x.Save("hhh.docx")
fmt.Println("Success")
}
// func table1(docxObj){}
// func table2(docxObj){}
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change 👋😇.
License
This Package is Under MIT License.