forked from unidoc/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
81 lines (76 loc) · 1.64 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2017 Baliance. All rights reserved.
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"baliance.com/gooxml/document"
)
func main() {
flag.Parse()
if flag.NArg() != 1 {
log.Fatalf("pass a single document as a parameter")
}
doc, err := document.Open(flag.Arg(0))
if err != nil {
log.Fatalf("error opening: %s", err)
}
imgCnt := 0
for _, para := range doc.Paragraphs() {
if s := para.Style(); s != "" {
// pull out particular headings
switch s {
case "Heading1":
fmt.Print("# ")
case "Heading2":
fmt.Print("## ")
case "Heading3":
fmt.Print("### ")
case "Heading4":
fmt.Print("#### ")
}
}
for _, run := range para.Runs() {
// print paragraph text
if s := run.Text(); s != "" {
switch {
case run.Properties().IsBold():
fmt.Printf("**%s**", s)
case run.Properties().IsItalic():
fmt.Printf("*%s*", s)
default:
fmt.Print(s)
}
}
// check for any anchored images
for _, anc := range run.DrawingAnchored() {
img, ok := anc.GetImage()
if ok {
imgCnt++
fn := fmt.Sprintf("image%d.%s", imgCnt, img.Format())
// copy image to disk
dst, err := os.Create(fn)
if err != nil {
log.Fatalf("error creating %s: %s", fn, err)
}
f, err := os.Open(img.Path())
if err != nil {
log.Fatalf("error reading %s: %s", img.Path(), err)
}
_, err = io.Copy(dst, f)
if err != nil {
log.Fatalf("error copying image: %s", err)
}
f.Close()
dst.Close()
// and add a markdown reference to it
fmt.Printf("\n![%s](%s)\n", fn, fn)
}
}
}
fmt.Println()
fmt.Println()
}
}