1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-11 02:50:27 +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" "flag"
"fmt" "fmt"
"os" "os"
"regexp"
"strconv"
"strings" "strings"
"github.com/fumiama/go-docx" "github.com/fumiama/go-docx"
@@ -35,6 +37,7 @@ func main() {
analyzeOnly := flag.Bool("a", false, "analyze file only") analyzeOnly := flag.Bool("a", false, "analyze file only")
clean := flag.Bool("c", false, "clean mode (keep text and picture only)") clean := flag.Bool("c", false, "clean mode (keep text and picture only)")
unm := flag.Bool("u", false, "lease unmarshalled file") unm := flag.Bool("u", false, "lease unmarshalled file")
splitre := flag.String("s", "", "split file into many docxs by matching regex")
flag.Parse() flag.Parse()
var w *docx.Docx var w *docx.Docx
if !*analyzeOnly { if !*analyzeOnly {
@@ -202,5 +205,23 @@ func main() {
fmt.Println(o.String()) 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") fmt.Println("End of main")
} }

View File

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

View File

@@ -169,7 +169,7 @@ type Paragraph struct {
RsidP string `xml:"w:rsidP,attr,omitempty"` RsidP string `xml:"w:rsidP,attr,omitempty"`
Properties *ParagraphProperties 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 file *Docx
} }