Access to rescatter constants in Excel COM using Python and win32com

I am using python 2.7 win32com module to load MS Excel worksheet from Python:

    book = xlApp.Workbooks.Open("myFile.xls")
    sheet = book.Sheets(1)

Many methods and properties of Range, Worksheet, etc. use enums such as XlDirection, XlFillWith, etc. They define constants such as xlDown, xlUp, xlFillWithContents, etc. Are these constants available for win32com so that I can do this, for example:

    column = outputsSheet.Range("I5:I150")
    lastRow = column.End(xlInterop.xlDown)
    print "Last row:", lastRow.Row

This does not work because xlInterop is undefined, is there a way to access it using win32com? Finding values ​​of constants like xlDown by trial and error is not very practical.

+4
source share
2 answers

win32com, , ... Excel

xlApp = win32com.client.gencache.EnsureDispatch("Excel.Application")

makepy, Python ... \Lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x7 ( , ). init.py, .

: :

from win32com.client import constants as c
+10

Olav "win32com gencache EnsureDispatch ", . / COM Python?, . ActiveState COM Python . , , client.Dispatch , , , gencache.EnsureDispatch, :

Portable Python >>> from win32com import client as com
Portable Python >>> from win32com.client import constants as c
Portable Python >>> word = com.Dispatch('Word.Application')
Portable Python >>> c.wdWindowStateMinimize
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "F:\PORTAB~1.1\App\lib\site-packages\win32com\client\__init__.py", line 170, in __getattr__
      raise AttributeError(a)
  AttributeError: wdWindowStateMinimize
Portable Python >>> word = com.gencache.EnsureDispatch("Word.Application")
Portable Python >>> c.wdWindowStateMinimize
2
+3

Source: https://habr.com/ru/post/1524577/


All Articles