1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-04 23:30:25 +08:00

fix: para.copymedia

This commit is contained in:
源文雨
2023-03-09 22:42:16 +08:00
parent b68d555c67
commit 65da4dc8f8
3 changed files with 25 additions and 4 deletions

View File

@@ -25,6 +25,8 @@ import (
"flag"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"github.com/fumiama/go-docx"
@@ -35,6 +37,7 @@ func main() {
analyzeOnly := flag.Bool("a", false, "analyze file only")
clean := flag.Bool("c", false, "clean mode (keep text and picture only)")
unm := flag.Bool("u", false, "lease unmarshalled file")
splitre := flag.String("s", "", "split file into many docxs by matching regex")
flag.Parse()
var w *docx.Docx
if !*analyzeOnly {
@@ -202,5 +205,23 @@ func main() {
fmt.Println(o.String())
}
}
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:]
f, err := os.Create(name)
if err != nil {
panic(err)
}
_, err = splitteddoc.WriteTo(f)
if err != nil {
panic(err)
}
err = f.Close()
if err != nil {
panic(err)
}
}
}
fmt.Println("End of main")
}

View File

@@ -26,7 +26,6 @@ import (
"reflect"
"regexp"
"strings"
"unsafe"
)
//nolint:revive,stylecheck
@@ -127,7 +126,7 @@ func (b *Body) DropDrawingOf(name string) {
switch o := item.(type) {
case *Paragraph:
f := reflect.ValueOf(o).MethodByName("Drop" + name)
if *(*uintptr)(unsafe.Pointer(&f)) == 0 {
if !f.IsValid() {
continue
}
_ = f.Call(nil)
@@ -136,7 +135,7 @@ func (b *Body) DropDrawingOf(name string) {
for _, tc := range tr.TableCells {
for _, p := range tc.Paragraphs {
f := reflect.ValueOf(p).MethodByName("Drop" + name)
if *(*uintptr)(unsafe.Pointer(&f)) == 0 {
if !f.IsValid() {
continue
}
_ = f.Call(nil)
@@ -301,6 +300,7 @@ func (p *Paragraph) copymedia(to *Docx) (np Paragraph) {
}
nr.Children = append(nr.Children, rc)
}
np.Children = append(np.Children, &nr)
continue
}
np.Children = append(np.Children, pc)

View File

@@ -169,7 +169,7 @@ type Paragraph struct {
RsidP string `xml:"w:rsidP,attr,omitempty"`
Properties *ParagraphProperties
Children []interface{} // Children will generate an unnecessary tag <Children> ... </Children> and we skip it by a self-defined xml.Marshaler
Children []interface{}
file *Docx
}