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