mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-05 07:40:24 +08:00
add api LoadBodyItems
This commit is contained in:
@@ -223,9 +223,10 @@ func main() {
|
||||
}
|
||||
}
|
||||
if *splitre != "" {
|
||||
i := strings.LastIndex(*fileLocation, "/")
|
||||
for j, splitteddoc := range doc.SplitByParagraph(docx.SplitDocxByPlainTextRegex(regexp.MustCompile(*splitre))) {
|
||||
name := (*fileLocation)[:i+1] + "unmarshal_split" + strconv.Itoa(j) + "_" + (*fileLocation)[i+1:]
|
||||
a := strings.LastIndex(*fileLocation, "/")
|
||||
b := strings.LastIndex(*fileLocation, ".")
|
||||
for i, splitteddoc := range doc.SplitByParagraph(docx.SplitDocxByPlainTextRegex(regexp.MustCompile(*splitre))) {
|
||||
name := (*fileLocation)[:a+1] + "unmarshal_" + (*fileLocation)[a+1:b] + "_split" + strconv.Itoa(i) + (*fileLocation)[b:]
|
||||
f, err := os.Create(name)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
50
docx.go
50
docx.go
@@ -24,6 +24,7 @@ package docx
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"encoding/xml"
|
||||
"io"
|
||||
"io/fs"
|
||||
"sync"
|
||||
@@ -97,6 +98,55 @@ func Parse(reader io.ReaderAt, size int64) (doc *Docx, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// LoadBodyItems will load body and media to a new Docx struct.
|
||||
// You should call UseTemplate to set a template later.
|
||||
func LoadBodyItems(items []interface{}, media []Media) *Docx {
|
||||
doc := &Docx{
|
||||
Document: Document{
|
||||
XMLName: xml.Name{
|
||||
Space: "w",
|
||||
},
|
||||
XMLW: XMLNS_W,
|
||||
XMLR: XMLNS_R,
|
||||
XMLWP: XMLNS_WP,
|
||||
XMLWPS: XMLNS_WPS,
|
||||
XMLWPC: XMLNS_WPC,
|
||||
XMLWPG: XMLNS_WPG,
|
||||
Body: Body{Items: items},
|
||||
},
|
||||
docRelation: Relationships{
|
||||
Xmlns: XMLNS_REL,
|
||||
Relationship: []Relationship{
|
||||
{
|
||||
ID: "rId1",
|
||||
Type: `http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles`,
|
||||
Target: "styles.xml",
|
||||
},
|
||||
{
|
||||
ID: "rId2",
|
||||
Type: `http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme`,
|
||||
Target: "theme/theme1.xml",
|
||||
},
|
||||
{
|
||||
ID: "rId3",
|
||||
Type: `http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable`,
|
||||
Target: "fontTable.xml",
|
||||
},
|
||||
},
|
||||
},
|
||||
media: media,
|
||||
mediaNameIdx: make(map[string]int, 64),
|
||||
rID: 3,
|
||||
slowIDs: make(map[string]uintptr, 64),
|
||||
}
|
||||
doc.Document.Body.file = doc
|
||||
for i, m := range media {
|
||||
doc.mediaNameIdx[m.Name] = i
|
||||
}
|
||||
doc.slowIDs["图片"] = uintptr(len(media) + 1)
|
||||
return doc
|
||||
}
|
||||
|
||||
// WriteTo allows to save a docx to a writer
|
||||
func (f *Docx) WriteTo(writer io.Writer) (_ int64, err error) {
|
||||
zipWriter := zip.NewWriter(writer)
|
||||
|
||||
Reference in New Issue
Block a user