Go调用WPS转换文档为PDF

前言

COM接口名

MS控件名 name
WPS文字 KWPS.Aplication
WPS的Excel KET.Application
WPS的演示文档 KWPP.Application
Word Word.Application
Excel Excel.Application
Powerpoint Powerpoint.Application

添加依赖

1
go get github.com/go-ole/go-ole

代码

导出PDF

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main

import (
ole "github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
)

func office_word2pdf(fileName string, pdfPath string) {
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("Word.Application")
defer unknown.Release()
word, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer word.Release()
oleutil.PutProperty(word, "Visible", false)
documents := oleutil.MustGetProperty(word, "Documents").ToIDispatch()
defer documents.Release()
document := oleutil.MustCallMethod(documents, "Open", fileName).ToIDispatch()
defer document.Release()
oleutil.MustCallMethod(document, "SaveAs2", pdfPath, 17).ToIDispatch()
oleutil.CallMethod(document, "Close")
oleutil.CallMethod(word, "Quit")
println("success")
}

func office_excel2pdf(fileName string, pdfPath string) {
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("Excel.Application")
defer unknown.Release()
excel, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer excel.Release()
oleutil.PutProperty(excel, "Visible", false)
workbooks := oleutil.MustGetProperty(excel, "Workbooks").ToIDispatch()
defer workbooks.Release()
workbook := oleutil.MustCallMethod(workbooks, "Open", fileName).ToIDispatch()
defer workbook.Release()
worksheet := oleutil.MustGetProperty(workbook, "Worksheets", 1).ToIDispatch()
defer worksheet.Release()
oleutil.MustCallMethod(worksheet, "ExportAsFixedFormat", 0, pdfPath).ToIDispatch()
oleutil.CallMethod(workbook, "Close")
oleutil.CallMethod(excel, "Quit")
println("success")
}

func office_ppt2pdf(fileName string, pdfPath string) {
ole.CoInitializeEx(0, ole.COINIT_APARTMENTTHREADED)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("PowerPoint.Application")
defer unknown.Release()
ppt, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer ppt.Release()
presentations := oleutil.MustGetProperty(ppt, "Presentations").ToIDispatch()
defer presentations.Release()
presentation := oleutil.MustCallMethod(presentations, "Open", fileName).ToIDispatch()
defer presentation.Release()
oleutil.MustCallMethod(presentation, "SaveAs", pdfPath, 32).ToIDispatch()
oleutil.CallMethod(presentation, "Close")
oleutil.CallMethod(ppt, "Quit")
println("success")
}

func wps_word2pdf(fileName string, pdfPath string) {
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("KWPS.Application")
defer unknown.Release()
word, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer word.Release()
oleutil.PutProperty(word, "Visible", false)
documents := oleutil.MustGetProperty(word, "Documents").ToIDispatch()
defer documents.Release()
document := oleutil.MustCallMethod(documents, "Open", fileName).ToIDispatch()
defer document.Release()
oleutil.MustCallMethod(document, "SaveAs", pdfPath, 16).ToIDispatch()
oleutil.CallMethod(document, "Close")
oleutil.CallMethod(word, "Quit")
println("success")
}

func wps_ppt2pdf(fileName string, pdfPath string) {
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("KWPP.Application")
defer unknown.Release()
ppt, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer ppt.Release()
oleutil.PutProperty(ppt, "Visible", false)
presentations := oleutil.MustGetProperty(ppt, "Presentations").ToIDispatch()
defer presentations.Release()
presentation := oleutil.MustCallMethod(presentations, "Open", fileName).ToIDispatch()
defer presentation.Release()
oleutil.MustCallMethod(presentation, "SaveAs", pdfPath, 32).ToIDispatch()
oleutil.CallMethod(presentation, "Close")
oleutil.CallMethod(ppt, "Quit")
println("success")
}

func wps_excel2pdf(fileName string, pdfPath string) {
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("KET.Application")
defer unknown.Release()
excel, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer excel.Release()
oleutil.PutProperty(excel, "Visible", false)
workbooks := oleutil.MustGetProperty(excel, "Workbooks").ToIDispatch()
defer workbooks.Release()
workbook := oleutil.MustCallMethod(workbooks, "Open", fileName).ToIDispatch()
defer workbook.Release()
worksheet := oleutil.MustGetProperty(workbook, "Worksheets", 1).ToIDispatch()
defer worksheet.Release()
oleutil.MustCallMethod(worksheet, "ExportAsFixedFormat", 0, pdfPath).ToIDispatch()
oleutil.CallMethod(workbook, "Close")
oleutil.CallMethod(excel, "Quit")
println("success")
}

func main() {
//office_word2pdf("D:\\Tools\\Docs\\01.docx", "D:\\Tools\\Docs\\pdf\\01.pdf")
//office_ppt2pdf("D:\\Tools\\Docs\\02.pptx", "D:\\Tools\\Docs\\pdf\\02.pdf")
//office_excel2pdf("D:\\Tools\\Docs\\03.xlsx", "D:\\Tools\\Docs\\pdf\\03.pdf")

wps_word2pdf("D:\\Tools\\Docs\\01.docx", "D:\\Tools\\Docs\\pdf\\01.pdf")
wps_ppt2pdf("D:\\Tools\\Docs\\02.pptx", "D:\\Tools\\Docs\\pdf\\02.pdf")
wps_excel2pdf("D:\\Tools\\Docs\\03.xlsx", "D:\\Tools\\Docs\\pdf\\03.pdf")
}

Excel修改并保存

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
82
83
func office_excel2pdf(fileName string, pdfPath string) {
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("Excel.Application")
defer unknown.Release()
excel, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer excel.Release()
oleutil.PutProperty(excel, "Visible", false)
workbooks := oleutil.MustGetProperty(excel, "Workbooks").ToIDispatch()
defer workbooks.Release()
workbook := oleutil.MustCallMethod(workbooks, "Open", fileName).ToIDispatch()
defer workbook.Release()
worksheet := oleutil.MustGetProperty(workbook, "Worksheets", 1).ToIDispatch()
defer worksheet.Release()
ps := oleutil.MustGetProperty(worksheet, "PageSetup").ToIDispatch()
defer ps.Release()
oleutil.PutProperty(ps, "LeftHeader", "")
oleutil.PutProperty(ps, "CenterHeader", "")
oleutil.PutProperty(ps, "RightHeader", "")
oleutil.PutProperty(ps, "LeftFooter", "")
oleutil.PutProperty(ps, "CenterFooter", "")
oleutil.PutProperty(ps, "RightFooter", "")
oleutil.PutProperty(ps, "LeftMargin", 0)
oleutil.PutProperty(ps, "RightMargin", 0)
oleutil.PutProperty(ps, "TopMargin", 0)
oleutil.PutProperty(ps, "BottomMargin", 0)
oleutil.PutProperty(ps, "HeaderMargin", 0)
oleutil.PutProperty(ps, "FooterMargin", 0)
oleutil.PutProperty(ps, "Orientation", 2)
oleutil.PutProperty(ps, "Zoom", false)
oleutil.PutProperty(ps, "FitToPagesWide", 1)
oleutil.PutProperty(ps, "FitToPagesTall", false)
oleutil.PutProperty(ps, "CenterVertically", true)
oleutil.PutProperty(ps, "CenterHorizontally", true)
oleutil.PutProperty(ps, "Draft", false)
oleutil.PutProperty(ps, "FirstPageNumber", true)
oleutil.MustCallMethod(worksheet, "ExportAsFixedFormat", 0, pdfPath).ToIDispatch()
oleutil.CallMethod(workbook, "Close")
oleutil.CallMethod(excel, "Quit")
println("success")
}

func wps_excel2pdf(fileName string, pdfPath string) {
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("KET.Application")
defer unknown.Release()
excel, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer excel.Release()
oleutil.PutProperty(excel, "Visible", false)
workbooks := oleutil.MustGetProperty(excel, "Workbooks").ToIDispatch()
defer workbooks.Release()
workbook := oleutil.MustCallMethod(workbooks, "Open", fileName).ToIDispatch()
defer workbook.Release()
worksheet := oleutil.MustGetProperty(workbook, "Worksheets", 1).ToIDispatch()
defer worksheet.Release()
ps := oleutil.MustGetProperty(worksheet, "PageSetup").ToIDispatch()
defer ps.Release()
oleutil.PutProperty(ps, "LeftHeader", "")
oleutil.PutProperty(ps, "CenterHeader", "")
oleutil.PutProperty(ps, "RightHeader", "")
oleutil.PutProperty(ps, "LeftFooter", "")
oleutil.PutProperty(ps, "CenterFooter", "")
oleutil.PutProperty(ps, "RightFooter", "")
oleutil.PutProperty(ps, "LeftMargin", 0)
oleutil.PutProperty(ps, "RightMargin", 0)
oleutil.PutProperty(ps, "TopMargin", 0)
oleutil.PutProperty(ps, "BottomMargin", 0)
oleutil.PutProperty(ps, "HeaderMargin", 0)
oleutil.PutProperty(ps, "FooterMargin", 0)
oleutil.PutProperty(ps, "Orientation", 2)
oleutil.PutProperty(ps, "Zoom", false)
oleutil.PutProperty(ps, "FitToPagesWide", 1)
oleutil.PutProperty(ps, "FitToPagesTall", false)
oleutil.PutProperty(ps, "CenterVertically", true)
oleutil.PutProperty(ps, "CenterHorizontally", true)
oleutil.PutProperty(ps, "Draft", false)
oleutil.PutProperty(ps, "FirstPageNumber", true)
oleutil.MustCallMethod(worksheet, "ExportAsFixedFormat", 0, pdfPath).ToIDispatch()
oleutil.CallMethod(workbook, "Close")
oleutil.CallMethod(excel, "Quit")
println("success")
}

关闭窗口

添加依赖

1
2
go get golang.org/x/sys@v0.4.0
go get github.com/lxn/win

方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
func colseWinTask(str string) {
timer := time.NewTimer(3 * time.Second)
go func() {
for {
<-timer.C
closeWindow(str)
timer.Reset(3 * time.Second)
}
}()
}

func closeWindow(str string) bool {
closeWin := win.FindWindow(nil, syscall.StringToUTF16Ptr(str))
if closeWin != 0 {
win.PostMessage(closeWin, win.WM_CLOSE, 0, 0)
return true
}
return false
}

调用

1
colseWinTask(`WPS 演示`)

封装一下

工具类

添加依赖

1
2
3
go get github.com/go-ole/go-ole
go get golang.org/x/sys@v0.4.0
go get github.com/lxn/win

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main

import (
"fmt"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
"github.com/lxn/win"
"os"
"path/filepath"
"strings"
"syscall"
"time"
)

func officeWord2pdf(fileName string, pdfPath string) {
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("Word.Application")
defer unknown.Release()
word, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer word.Release()
oleutil.PutProperty(word, "Visible", false)
documents := oleutil.MustGetProperty(word, "Documents").ToIDispatch()
defer documents.Release()
document := oleutil.MustCallMethod(documents, "Open", fileName).ToIDispatch()
defer document.Release()
oleutil.MustCallMethod(document, "SaveAs2", pdfPath, 17).ToIDispatch()
oleutil.CallMethod(document, "Close")
oleutil.CallMethod(word, "Quit")
println("success")
}

func officeExcel2pdf(fileName string, pdfPath string) {
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("Excel.Application")
defer unknown.Release()
excel, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer excel.Release()
oleutil.PutProperty(excel, "Visible", false)
workbooks := oleutil.MustGetProperty(excel, "Workbooks").ToIDispatch()
defer workbooks.Release()
workbook := oleutil.MustCallMethod(workbooks, "Open", fileName).ToIDispatch()
defer workbook.Release()
worksheet := oleutil.MustGetProperty(workbook, "Worksheets", 1).ToIDispatch()
defer worksheet.Release()
oleutil.MustCallMethod(worksheet, "ExportAsFixedFormat", 0, pdfPath).ToIDispatch()
oleutil.CallMethod(workbook, "Close")
oleutil.CallMethod(excel, "Quit")
println("success")
}

func officePpt2pdf(fileName string, pdfPath string) {
ole.CoInitializeEx(0, ole.COINIT_APARTMENTTHREADED)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("PowerPoint.Application")
defer unknown.Release()
ppt, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer ppt.Release()
presentations := oleutil.MustGetProperty(ppt, "Presentations").ToIDispatch()
defer presentations.Release()
presentation := oleutil.MustCallMethod(presentations, "Open", fileName).ToIDispatch()
defer presentation.Release()
oleutil.MustCallMethod(presentation, "SaveAs", pdfPath, 32).ToIDispatch()
oleutil.CallMethod(presentation, "Close")
oleutil.CallMethod(ppt, "Quit")
println("success")
}

func wpsWord2pdf(fileName string, pdfPath string) {
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("KWPS.Application")
defer unknown.Release()
word, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer word.Release()
oleutil.PutProperty(word, "Visible", false)
documents := oleutil.MustGetProperty(word, "Documents").ToIDispatch()
defer documents.Release()
document := oleutil.MustCallMethod(documents, "Open", fileName).ToIDispatch()
defer document.Release()
oleutil.MustCallMethod(document, "SaveAs2", pdfPath, 17).ToIDispatch()
oleutil.CallMethod(document, "Close")
oleutil.CallMethod(word, "Quit")
println("success")
}

func wpsPpt2pdf(fileName string, pdfPath string) {
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("KWPP.Application")
defer unknown.Release()
ppt, _ := unknown.QueryInterface(ole.IID_IDispatch)
oleutil.PutProperty(ppt, "DisplayAlerts", false)
oleutil.PutProperty(ppt, "DontShowFontSubstitution", true)
defer ppt.Release()
oleutil.PutProperty(ppt, "Visible", false)

presentations := oleutil.MustGetProperty(ppt, "Presentations").ToIDispatch()
defer presentations.Release()

presentation := oleutil.MustCallMethod(presentations, "Open", fileName).ToIDispatch()
defer presentation.Release()

oleutil.MustCallMethod(presentation, "SaveAs", pdfPath, 32).ToIDispatch()
oleutil.CallMethod(presentation, "Close")
oleutil.CallMethod(ppt, "Quit")
println("success")
}

func wpsExcel2pdf(fileName string, pdfPath string) {
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("KET.Application")
defer unknown.Release()
excel, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer excel.Release()
oleutil.PutProperty(excel, "Visible", false)
workbooks := oleutil.MustGetProperty(excel, "Workbooks").ToIDispatch()
defer workbooks.Release()
workbook := oleutil.MustCallMethod(workbooks, "Open", fileName).ToIDispatch()
defer workbook.Release()
worksheet := oleutil.MustGetProperty(workbook, "Worksheets", 1).ToIDispatch()
defer worksheet.Release()
oleutil.MustCallMethod(worksheet, "ExportAsFixedFormat", 0, pdfPath).ToIDispatch()
oleutil.CallMethod(workbook, "Close")
oleutil.CallMethod(excel, "Quit")
println("success")
}

func colseWinTask(str string) {
timer := time.NewTimer(2 * time.Second)
go func() {
for {
<-timer.C
closeWindow(str)
timer.Reset(2 * time.Second)
}
}()
}

func closeWindow(str string) bool {
closeWin := win.FindWindow(nil, syscall.StringToUTF16Ptr(str))
if closeWin != 0 {
win.PostMessage(closeWin, win.WM_CLOSE, 0, 0)
return true
}
return false
}

/*
是否包含
*/
func contains(s []string, e string) bool {
for _, v := range s {
if v == e {
return true
}
}
return false
}

func isFileExists(filename string) bool {
if _, err := os.Open(filename); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}

func main() {
args := os.Args[1:]
if len(args) != 3 {
fmt.Println("注意参数为:office|wps 源文件路径 PDF文件路径")
return
}
docArr := []string{".doc", ".docx"}
pptArr := []string{".ppt", ".pptx"}
excelArr := []string{".xls", ".xlsx"}
var typename = args[0]
var fileName = args[1]
var pdfPath = args[2]
if !isFileExists(fileName) {
fmt.Println("源文件不存在")
return
}
ext := strings.ToLower(filepath.Ext(fileName))

if typename == "wps" {
if contains(docArr, ext) {
colseWinTask(`WPS 文字`)
wpsWord2pdf(fileName, pdfPath)
} else if contains(pptArr, ext) {
colseWinTask(`WPS 演示`)
wpsPpt2pdf(fileName, pdfPath)
} else if contains(excelArr, ext) {
colseWinTask(`WPS 表格`)
wpsExcel2pdf(fileName, pdfPath)
} else {
fmt.Println("格式不支持")
}
} else if typename == "office" {
if contains(docArr, ext) {
officeWord2pdf(fileName, pdfPath)
} else if contains(pptArr, ext) {
officePpt2pdf(fileName, pdfPath)
} else if contains(excelArr, ext) {
officeExcel2pdf(fileName, pdfPath)
} else {
fmt.Println("格式不支持")
}
}
}

其中

WPS的Word转PDF部分失败

要把

1
oleutil.MustCallMethod(document, "SaveAs", pdfPath, 16).ToIDispatch()

改为

1
oleutil.MustCallMethod(document, "SaveAs2", pdfPath, 17).ToIDispatch()

这样就可以保证转换过都没有问题了。

调用方式

1
2
wps2pdf.exe wps "D:\\Tools\\Docs\\01.docx" "D:\\Tools\\Docs\\pdf\\01.pdf"
wps2pdf.exe office "D:\\Tools\\Docs\\01.docx" "D:\\Tools\\Docs\\pdf\\01.pdf"