mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-10 18:40:28 +08:00
优化 para.String
This commit is contained in:
@@ -24,7 +24,7 @@ import "unsafe"
|
|||||||
|
|
||||||
// AddParagraph adds a new paragraph
|
// AddParagraph adds a new paragraph
|
||||||
func (f *Docx) AddParagraph() *Paragraph {
|
func (f *Docx) AddParagraph() *Paragraph {
|
||||||
f.Document.Body.Items = append(f.Document.Body.Items, Paragraph{
|
f.Document.Body.Items = append(f.Document.Body.Items, &Paragraph{
|
||||||
Children: make([]interface{}, 0, 64),
|
Children: make([]interface{}, 0, 64),
|
||||||
file: f,
|
file: f,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ func (f *Docx) AddTable(row int, col int) *WTable {
|
|||||||
TableCells: cells,
|
TableCells: cells,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f.Document.Body.Items = append(f.Document.Body.Items, WTable{
|
f.Document.Body.Items = append(f.Document.Body.Items, &WTable{
|
||||||
TableProperties: &WTableProperties{
|
TableProperties: &WTableProperties{
|
||||||
Width: &WTableWidth{Type: "auto"},
|
Width: &WTableWidth{Type: "auto"},
|
||||||
TableBorders: &WTableBorders{
|
TableBorders: &WTableBorders{
|
||||||
@@ -99,7 +99,7 @@ func (f *Docx) AddTableTwips(rowHeights []int64, colWidths []int64) *WTable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f.Document.Body.Items = append(f.Document.Body.Items, WTable{
|
f.Document.Body.Items = append(f.Document.Body.Items, &WTable{
|
||||||
TableProperties: &WTableProperties{
|
TableProperties: &WTableProperties{
|
||||||
Width: &WTableWidth{Type: "auto"},
|
Width: &WTableWidth{Type: "auto"},
|
||||||
TableBorders: &WTableBorders{
|
TableBorders: &WTableBorders{
|
||||||
|
|||||||
@@ -191,23 +191,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
fmt.Println("Plain text:")
|
fmt.Println("Plain text:")
|
||||||
for _, it := range doc.Document.Body.Items {
|
for _, it := range doc.Document.Body.Items {
|
||||||
switch para := it.(type) {
|
switch it.(type) {
|
||||||
case docx.Paragraph:
|
case *docx.Paragraph, *docx.WTable: // printable
|
||||||
fmt.Println(para.String())
|
fmt.Println(it)
|
||||||
case docx.WTable:
|
|
||||||
fmt.Println("------------------------------")
|
|
||||||
for x, r := range para.TableRows {
|
|
||||||
fmt.Printf("[%d] ", x)
|
|
||||||
for y, c := range r.TableCells {
|
|
||||||
if len(c.Paragraphs) > 0 && len(c.Paragraphs[0].Children) > 0 {
|
|
||||||
fmt.Printf("<%d> %v\t", y, &c.Paragraphs[0])
|
|
||||||
} else {
|
|
||||||
fmt.Printf("<%d> \t\t", y)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fmt.Print("\n")
|
|
||||||
}
|
|
||||||
fmt.Println("------------------------------")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println("End of main")
|
fmt.Println("End of main")
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ func (b *Body) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
value.file = b.file
|
value.file = b.file
|
||||||
b.Items = append(b.Items, value)
|
b.Items = append(b.Items, &value)
|
||||||
case "tbl":
|
case "tbl":
|
||||||
var value WTable
|
var value WTable
|
||||||
err = d.DecodeElement(&value, &tt)
|
err = d.DecodeElement(&value, &tt)
|
||||||
@@ -83,7 +83,7 @@ func (b *Body) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
value.file = b.file
|
value.file = b.file
|
||||||
b.Items = append(b.Items, value)
|
b.Items = append(b.Items, &value)
|
||||||
default:
|
default:
|
||||||
err = d.Skip() // skip unsupported tags
|
err = d.Skip() // skip unsupported tags
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
143
structpara.go
143
structpara.go
@@ -184,55 +184,116 @@ func (p *Paragraph) String() string {
|
|||||||
case *Tab:
|
case *Tab:
|
||||||
sb.WriteByte('\t')
|
sb.WriteByte('\t')
|
||||||
case *Drawing:
|
case *Drawing:
|
||||||
if x.Inline != nil && x.Inline.Graphic != nil && x.Inline.Graphic.GraphicData != nil && x.Inline.Graphic.GraphicData.Pic != nil {
|
if x.Inline != nil && x.Inline.Graphic != nil && x.Inline.Graphic.GraphicData != nil {
|
||||||
sb.WriteString("
|
|
||||||
if x.Inline.Graphic.GraphicData.Pic.BlipFill != nil {
|
|
||||||
tgt, err := p.file.ReferTarget(x.Inline.Graphic.GraphicData.Pic.BlipFill.Blip.Embed)
|
|
||||||
if err != nil {
|
|
||||||
sb.WriteString(err.Error())
|
|
||||||
} else {
|
|
||||||
h := md5.Sum(p.file.Media(tgt[6:]).Data)
|
|
||||||
sb.WriteString(hex.EncodeToString(h[:]))
|
|
||||||
}
|
}
|
||||||
|
sb.WriteString("](")
|
||||||
|
if x.Inline.Graphic.GraphicData.Pic.BlipFill != nil {
|
||||||
|
tgt, err := p.file.ReferTarget(x.Inline.Graphic.GraphicData.Pic.BlipFill.Blip.Embed)
|
||||||
|
if err != nil {
|
||||||
|
sb.WriteString(err.Error())
|
||||||
|
} else {
|
||||||
|
h := md5.Sum(p.file.Media(tgt[6:]).Data)
|
||||||
|
sb.WriteString(hex.EncodeToString(h[:]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb.WriteByte(')')
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if x.Inline.Graphic.GraphicData.Shape != nil {
|
||||||
|
sb.WriteString("
|
||||||
|
if x.Inline.Graphic.GraphicData.Shape.SpPr != nil {
|
||||||
|
sb.WriteString(x.Inline.Graphic.GraphicData.Shape.SpPr.PrstGeom.Prst)
|
||||||
|
}
|
||||||
|
sb.WriteByte(')')
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if x.Inline.Graphic.GraphicData.Canvas != nil {
|
||||||
|
sb.WriteString("![inlncv ")
|
||||||
|
if x.Inline.DocPr != nil {
|
||||||
|
sb.WriteString(x.Inline.DocPr.Name)
|
||||||
|
} else {
|
||||||
|
sb.WriteString("nil")
|
||||||
|
}
|
||||||
|
sb.WriteString("]()")
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
sb.WriteByte(')')
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
if x.Anchor != nil && x.Anchor.Graphic != nil && x.Anchor.Graphic.GraphicData != nil && x.Anchor.Graphic.GraphicData.Pic != nil {
|
if x.Anchor != nil && x.Anchor.Graphic != nil && x.Anchor.Graphic.GraphicData != nil {
|
||||||
sb.WriteString("
|
|
||||||
if x.Anchor.Graphic.GraphicData.Pic.BlipFill != nil {
|
|
||||||
tgt, err := p.file.ReferTarget(x.Anchor.Graphic.GraphicData.Pic.BlipFill.Blip.Embed)
|
|
||||||
if err != nil {
|
|
||||||
sb.WriteString(err.Error())
|
|
||||||
} else {
|
|
||||||
h := md5.Sum(p.file.Media(tgt[6:]).Data)
|
|
||||||
sb.WriteString(hex.EncodeToString(h[:]))
|
|
||||||
}
|
}
|
||||||
|
sb.WriteString("](")
|
||||||
|
if x.Anchor.Graphic.GraphicData.Pic.BlipFill != nil {
|
||||||
|
tgt, err := p.file.ReferTarget(x.Anchor.Graphic.GraphicData.Pic.BlipFill.Blip.Embed)
|
||||||
|
if err != nil {
|
||||||
|
sb.WriteString(err.Error())
|
||||||
|
} else {
|
||||||
|
h := md5.Sum(p.file.Media(tgt[6:]).Data)
|
||||||
|
sb.WriteString(hex.EncodeToString(h[:]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb.WriteByte(')')
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if x.Anchor.Graphic.GraphicData.Shape != nil {
|
||||||
|
sb.WriteString("
|
||||||
|
if x.Anchor.Graphic.GraphicData.Shape.SpPr != nil {
|
||||||
|
sb.WriteString(x.Anchor.Graphic.GraphicData.Shape.SpPr.PrstGeom.Prst)
|
||||||
|
}
|
||||||
|
sb.WriteByte(')')
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if x.Anchor.Graphic.GraphicData.Canvas != nil {
|
||||||
|
sb.WriteString("![anchcv ")
|
||||||
|
if x.Anchor.DocPr != nil {
|
||||||
|
sb.WriteString(x.Anchor.DocPr.Name)
|
||||||
|
} else {
|
||||||
|
sb.WriteString("nil")
|
||||||
|
}
|
||||||
|
sb.WriteString("]()")
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
sb.WriteByte(')')
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case *RunProperties:
|
|
||||||
sb.WriteString("<prop>") //TODO: implement
|
|
||||||
default:
|
default:
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,30 @@ type WTable struct {
|
|||||||
file *Docx
|
file *Docx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *WTable) String() string {
|
||||||
|
if len(t.TableRows) == 0 || len(t.TableRows[0].TableCells) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
sb := strings.Builder{}
|
||||||
|
sb.WriteString("| ")
|
||||||
|
for i := 0; i < len(t.TableRows[0].TableCells); i++ {
|
||||||
|
sb.WriteString(" :----: |")
|
||||||
|
}
|
||||||
|
for _, r := range t.TableRows {
|
||||||
|
sb.WriteString("\n|")
|
||||||
|
for _, c := range r.TableCells {
|
||||||
|
if len(c.Paragraphs) > 0 && len(c.Paragraphs[0].Children) > 0 {
|
||||||
|
sb.WriteByte(' ')
|
||||||
|
sb.WriteString(c.Paragraphs[0].String())
|
||||||
|
} else {
|
||||||
|
sb.WriteString(" ")
|
||||||
|
}
|
||||||
|
sb.WriteString(" |")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
||||||
// UnmarshalXML implements the xml.Unmarshaler interface.
|
// UnmarshalXML implements the xml.Unmarshaler interface.
|
||||||
func (t *WTable) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
func (t *WTable) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||||
for {
|
for {
|
||||||
|
|||||||
Reference in New Issue
Block a user