forked from manuviswam/GoPPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (59 loc) · 1.89 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
package main
import (
"fmt"
"os"
"path/filepath"
z "github.com/manuviswam/GoPPT/zipper"
m "github.com/manuviswam/GoPPT/model"
fo "github.com/manuviswam/GoPPT/fileops"
c "github.com/manuviswam/GoPPT/constants"
"github.com/manuviswam/GoPPT/openxml"
"github.com/getgauge/gauge/util"
)
const (
templateImgName = "image1.jpg"
templateSlideName = "slide1.xml"
)
func main() {
sourcePPT := "Template.pptx"
tempFolder := "tmp"
targetPPT := "new.pptx"
newImage := "img.jpg"
err := z.Unzip(getAbsolutePath(sourcePPT), getAbsolutePath(tempFolder))
if err != nil {
fmt.Println("Error unziping : ", err)
return
}
defer util.Remove(getAbsolutePath(tempFolder))
//duplicate slide
newSlideName, err := openxml.DuplicateSlide(getAbsolutePath(tempFolder), templateSlideName)
if err != nil {
fmt.Println("Error while duplicationg slide : ",err)
return
}
slidePath := filepath.Join(tempFolder,c.PPTFolder,c.SlideFolder,newSlideName)
replacements := make([]m.SlideReplacement,1)
replacements = append(replacements,m.SlideReplacement{ PlaceHolder: "#title#", Replacement: "This is replaced title"})
replacements = append(replacements,m.SlideReplacement{ PlaceHolder: "#footer#", Replacement: "This is replaced footer"})
//replace text placeholders
err = fo.ReplaceTextInFile(getAbsolutePath(slidePath),replacements)
if err != nil {
fmt.Println("Error while content replacing",err)
return
}
targetImgPath := filepath.Join(tempFolder,c.PPTFolder,c.MediaFolder,templateImgName)
//replace image placeholder
err = fo.CopyFile(getAbsolutePath(newImage),getAbsolutePath(targetImgPath))
if err != nil {
fmt.Println("Error while image replacing",err)
return
}
err = z.Zipit(getAbsolutePath(tempFolder), getAbsolutePath(targetPPT))
if err != nil {
fmt.Println("Error ziping : ", err)
}
}
func getAbsolutePath(relPath string)string {
wd,_ := os.Getwd()
return filepath.Join(wd, relPath)
}