This article mainly describes the use of Python through the Win32 COM open Excel and add Sheet method, has a certain reference value, now share to everyone, the need for friends can refer to
Win32 COM is not very familiar with, do not know how many properties or methods of a program can be manipulated. It took me a long time to add just a sheet page, because this success comes from temptation.
Edit the code as follows:
#!/usr/bin/python from win32com.client import Dispatch xlapp = Dispatch (' excel.application ') xlapp.visible = TRUEXLAPP.WORKBOOKS.ADD () XlApp.Worksheets.Add ()
Program Run Result:
I use the version of Excel by default when there is only one sheet page, through the above operation after the opening of the two sheet page, the creation of sheet page is visible successfully.
If you want to specify the name of the sheet page, you have to modify a property when you create it. Modify the code as follows:
#!/usr/bin/pythonfrom win32com.client Import Dispatchxlapp = Dispatch (' excel.application ') xlapp.visible = TRUEXLAPP.WORKBOOKS.ADD () XlApp.Worksheets.Add (). Name = ' Test ' xlsheet = xlapp.worksheets (' Test ') xlsheet.cells (in). Value = ' title ' Xlsheet. Cells (2,1). Value = 123
The results of the program execution are as follows:
As can be seen from the above results, the operation not only realizes the creation of a new sheet page, but also implements the function of naming the newly created sheet page. After the creation and naming is finished, the sheet page, which is guided by the name, implements the writing of the information.