Use Python to open Excel and add sheet methods by Win32 COM

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.