前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Go调用WPS转换文档为PDF

Go调用WPS转换文档为PDF

作者头像
码客说
发布2023-04-27 15:25:25
1.6K0
发布2023-04-27 15:25:25
举报
文章被收录于专栏:码客码客

前言

COM接口名

MS控件名

name

WPS文字

KWPS.Aplication

WPS的Excel

KET.Application

WPS的演示文档

KWPP.Application

Word

Word.Application

Excel

Excel.Application

Powerpoint

Powerpoint.Application

添加依赖

代码语言:javascript
复制
go get github.com/go-ole/go-ole

代码

导出PDF

代码语言:javascript
复制
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修改并保存

代码语言:javascript
复制
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")
}

关闭窗口

添加依赖

代码语言:javascript
复制
go get golang.org/x/sys@v0.4.0
go get github.com/lxn/win

方法

代码语言:javascript
复制
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
}

调用

代码语言:javascript
复制
colseWinTask(`WPS 演示`)

封装一下

工具类

添加依赖

代码语言:javascript
复制
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

代码语言:javascript
复制
package main

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

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, "SaveAs", pdfPath, 16).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)
	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 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) {
			wpsWord2pdf(fileName, pdfPath)
		}else if contains(pptArr,ext) {
			wpsPpt2pdf(fileName, pdfPath)
		}else if contains(excelArr,ext) {
			wpsExcel2pdf(fileName, pdfPath)
		}
	}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)
		}
	}
}

其中

WPS的Word转PDF部分失败

要把

代码语言:javascript
复制
oleutil.MustCallMethod(document, "SaveAs", pdfPath, 16).ToIDispatch()

改为

代码语言:javascript
复制
oleutil.MustCallMethod(document, "SaveAs2", pdfPath, 17).ToIDispatch()

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

调用方式

代码语言:javascript
复制
wps2pdf.exe wps "D:\\Tools\\Docs\\01.docx" "D:\\Tools\\Docs\\pdf\\01.pdf"
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-04-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 添加依赖
  • 代码
    • 导出PDF
      • Excel修改并保存
      • 关闭窗口
      • 封装一下
        • 工具类
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档