Golang implements word and Excel processing

Golang uses the underlying development of Windows

I know from the previous blog, Python can realize Windows low-level development through pywin32 The most common are word and Excel processing, automatic printing, etc.
I once said that programming language is just a tool. As long as the tool plays smoothly, everything is possible. According to the functions implemented in the previous blog, you can also use Golang.
Golang calls the underlying interface of Windows and can use the third-party library github.com/go-ole/go-ole
If you are familiar with Golang, you can take a look at the go ole source code. From the source code file, you can find that it is implemented by calling the Windows underlying file dll through the standard library syscall, as shown in the figure below


After a general understanding of the underlying principles of the third library go Ole, the next step is to describe the following usage.

The third Library: go OLE

The third library go ole is not documented, but it has been provided Sample code: Describe how to use. By analyzing the sample code, we can see that the entire library simply calls the following functions and methods:
oleutil.CreateObject("Excel.Application"): create an object with a fixed writing method, and the parameters of CreateObject are variable.
unknown.QueryInterface(ole.IID_IDispatch): query interface, fixed writing method.
oleutil.PutProperty(): sets the property of the object.
oleutil.GetProperty(): get property.
oleutil.MustGetProperty().ToIDispatch(): get the property and encapsulate it on the basis of GetProperty().
oleutil.CallMethod(): call method.
oleutil.MustCallMethod(): call the method and encapsulate it on the basis of CallMethod().
Release(): release resources.
ole.CoInitialize(0): initialize.
Ole. Coinitialize(): release resources.
When calling COM components with win32com, you need to initialize with ole.CoInitialize. Finally, you need to release resources with ole.CoInitialize.
The entire library mainly uses the above function methods. For each function parameter, you can check the source code definition or the specific use of the sample code.

Convert Word to pdf

The sample code does not delay how Word is used, but you can use the Microsoft official website document Find the specific usage method. The example code is as follows:

package main

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

func setWord(fileName string)  {
	ole.CoInitialize(0)
	unknown, _ := oleutil.CreateObject("Word.Application")
	word, _ := unknown.QueryInterface(ole.IID_IDispatch)
	oleutil.PutProperty(word, "Visible", false)
	documents := oleutil.MustGetProperty(word, "Documents").ToIDispatch()
	document := oleutil.MustCallMethod(documents, "Open", fileName).ToIDispatch()
	oleutil.MustCallMethod(document, "SaveAs2", "E:\\mygo\\bb.pdf", 17).ToIDispatch()
	document.Release()
	documents.Release()
	word.Release()
	ole.CoUninitialize()
}

func main() {
	setWord("E:\\mygo\\abc.docx")
}

Excel settings page and border, convert to PDF

For the operation of Excel, the sample code does not have much description, and can also be combined Microsoft official website document To realize the functions we want, the code is as follows:

package main

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

func setExcel(fileName string){
	ole.CoInitialize(0)
	unknown, _ := oleutil.CreateObject("Excel.Application")
	excel, _ := unknown.QueryInterface(ole.IID_IDispatch)
	oleutil.PutProperty(excel, "Visible", false)
	workbooks := oleutil.MustGetProperty(excel, "Workbooks").ToIDispatch()
	workbook, _ := oleutil.CallMethod(workbooks, "Open", fileName)
	//defer workbook.ToIDispatch().Release()
	worksheet := oleutil.MustGetProperty(workbook.ToIDispatch(), "Worksheets", 1).ToIDispatch()
	//defer worksheet.Release()
	ps := oleutil.MustGetProperty(worksheet, "PageSetup").ToIDispatch()
	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, "E:\\mygo\\aa.pdf").ToIDispatch()
	ps.Release()
	worksheet.Release()
	workbooks.Release()
	excel.Release()
	ole.CoUninitialize()
}

func main() {
	setExcel("E:\\mygo\\abc.xls")
}

When saving a file, it is best to use an absolute path. If a relative path is used, the final saved file may not be in the path where the go file is located.

summary

Whether you use Python or Golang to implement the underlying development of Windows, the final implementation function depends on Microsoft official website document Provided interface method.
In addition, Golang can also implement printer functions, such as Network printing function

Tags: Go Windows programming language

Posted by Iconoclast on Fri, 19 Aug 2022 10:39:05 +0300