项目作者: briiC

项目描述 :
Generate new docx with params from given docx template.
高级语言: Go
项目地址: git://github.com/briiC/docxplate.git
创建时间: 2019-09-10T06:42:23Z
项目社区:https://github.com/briiC/docxplate

开源协议:MIT License

下载


docxplate Go

Generate document from .docx template file.
(!) Unstable with complex/many levels Structs.

Examples

Click on image to see larger.
docxplate preview

  1. // Using this data for all examples below
  2. type User struct {
  3. Name string
  4. Age int
  5. Nicknames []string
  6. Friends []*User
  7. ImageLocal *docxplate.Image
  8. ImageURL *docxplate.Image
  9. }
  10. user := User{
  11. Name: "Alice",
  12. Age: 27,
  13. Nicknames: []string{"amber", "AL", "ice"},
  14. Friends: []*User{
  15. &User{Name: "Bob", Age: 28},
  16. &User{Name: "Cecilia", Age: 29},
  17. &User{Name: "Den", Age: 30},
  18. },
  19. ImageLocal: &docxplate.Image{
  20. Path: "images/github.png",
  21. Width: 50,
  22. Height: 50,
  23. },
  24. ImageURL: &docxplate.Image{
  25. URL: "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png",
  26. Width: 50,
  27. Height: 50,
  28. },
  29. }
  30. // Template to document
  31. tdoc, _ := docxplate.OpenTemplate("template.docx")
  32. tdoc.Params(user)
  33. tdoc.ExportDocx("MyDoc.docx")

Struct as data input

Hello, {{Name}}! —> Hello, Alice!
You are {{Age}} years old. —> You are 27 years old.

Slice/Map placeholder to multiple rows

  1. Here is my nicknames:
  2. - {{Nicknames}}
  3. ---------------------------------------------------
  4. Here is my nicknames:
  5. - amber
  6. - AL
  7. - ice

List slice structs expands to multiple rows:

  1. My Friends:
  2. * {{Friends.Name}} is {{Friends.Age}} years old
  3. ---------------------------------------------------
  4. My Friends:
  5. * Bob is 28 years old
  6. * Cecilia is 29 years old
  7. * Den is 30 years old

Slice/Map placeholder to implode multiple values

Use {{Nicknames ***}} to concatenate values with given separator: amber***AL***ice.
Or no separator at all {{Nicknames }} (still one space required to mark placeholder as inline values): amberALice.

  1. Here is my nicknames: {{Nicknames , }} :)
  2. ---------------------------------------------------
  3. Here is my nicknames: amber, AL, ice :)

Bugs

Don’t use too complicated struct.
Above exmaple with User->Friends->User is limit in depth.
{{User->Friends->User->Friends}} is not supported at this moment.

Examples