Python launches Excel

Source: Internet
Author: User

The interactive control of Python and Excel program is realized by simple example.

Need to install PYWIN32, choose the download version according to your actual situation

Introducing Components

Import Win32com.client, win32process

Start to start Excel

Win32com.client.Dispatch (' Excel.Application '): Starts the Excel process if an Excel process exists in system memory and uses the existing process directly will not add

Win32com.client.DispatchEx (' Excel.Application '): Start a new Excel process

Win32com.client.gencache.EnsureDispatch (' Excel.Application '): the same as in the first case, but detects if a cache file exists. Generating a constant cache file facilitates the invocation of the program's constant parameters such as: win32com.client.constants.xlCalculationManual, etc. Once the cache is generated, you can call the constant parameter in three ways

Directory of cached files:

Import Osprint os.path.join (win32com.__gen_path__, str (Win32com.client.gencache.GetClassForProgID (' Excel.Application '). Split ('. ') [2])

Here we test the way we choose desktop interaction to control an open, blank workbook

xlapp = Win32com.client.gencache.EnsureDispatch (' Excel.Application ') print (xlapp.version, xlapp.caption)

Get Excel Process ID

Pid = win32process. GetWindowThreadProcessId (Xlapp.hwnd) [1]print Pid

Control active Worksheet

Sht = xlApp.ActiveWorkbook.ActiveSheetprint Sht.nameimport datetimesht.name = Datetime.datetime.today (). Strftime ('%Y %m%d ') Print Sht.name

Cell Read and write operations

1.99 Multiplication Table

Sht.range (Sht.cells), Sht.cells (9,9). Formula = ' =if (column () >row (), "", Concatenate (column (), "X", ROW (), "=", COLUMN () *row ())) '

2. Bulk Read

Print Sht.range (Sht.cells, Sht.cells (9,9)). Value

3. Simply understand the difference between value, Value2, and text

A10 = Sht.range (' A10 ') A10. Value = Datetime.datetime.today () print (A10. Value, A10. Value2, A10. Text)

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5C/02/wKiom1UY10eR_seZAAboRMVllgI436.jpg "title=" Pythonexcel20150330-1.png "alt=" Wkiom1uy10er_sezaabormvllgi436.jpg "/>

(not finished)

This article is from the "Notes" blog, so be sure to keep this source http://indie.blog.51cto.com/2806716/1626378

Python launches Excel

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.