Home > Backend Development > Python Tutorial > How to use Python to open Excel through win32 COM and add Sheet

How to use Python to open Excel through win32 COM and add Sheet

不言
Release: 2018-05-02 15:58:25
Original
3342 people have browsed it

This article mainly introduces the method of using Python to open Excel and add Sheet through win32 COM. It has a certain reference value. Now I share it with you. Friends in need can refer to it

on win32 COM I'm not very familiar with it, and I don't know how many attributes or methods a program can operate on. Just adding a Sheet page took me a long time, because this kind of success comes from trial and error.

Edit the code as follows:

#!/usr/bin/python
 
from win32com.client import Dispatch
 
xlApp = Dispatch('Excel.Application')
xlApp.Visible = True
xlApp.Workbooks.Add()
xlApp.Worksheets.Add()
Copy after login

Program running result:

The Excel version I use has only one Sheet page when it is opened by default. After the above operation, two Sheet pages appear when it is opened. It can be seen that the Sheet page was created successfully.

If you want to specify the name of the Sheet page, you must modify an attribute when creating it. Modify the code as follows:

#!/usr/bin/python
from win32com.client import Dispatch
xlApp = Dispatch('Excel.Application')
xlApp.Visible = True
xlApp.Workbooks.Add()
xlApp.Worksheets.Add().Name = 'test'
xlSheet = xlApp.Worksheets('test')
xlSheet.Cells(1,1).Value = 'title'
xlSheet.Cells(2,1).Value = 123
Copy after login

The program execution result is as follows:

As can be seen from the above results , the operation not only realizes the creation of a new Sheet page, but also realizes the function of naming the newly created Sheet page. After the creation and naming are completed, the Sheet page guided by the name realizes the writing of information.

Related recommendations:

How to use Python’s Requests package to implement simulated login




##

The above is the detailed content of How to use Python to open Excel through win32 COM and add Sheet. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template