[python-win32] Excel win32com newbie question

Tim Roberts timr at probo.com
Fri Oct 6 01:05:57 CEST 2006


Jim Steil wrote:

> I’ve been working this week building some Excel spreadsheets using
> Python and win32com. ...
>
> Possibly my biggest problem is that I can’t get my machine to give me
> the Excel constants. Here is the code I’m doing to try to test the
> constants and the error I’m getting back.
>
> from win32com.client import constants, Dispatch
>
> x = constants.xlHAlignRight
>
> Traceback (most recent call last):
>
> File "<input>", line 1, in ?
>
> File "C:\Python24\Lib\site-packages\win32com\client\__init__.py", line
> 168, in __getattr__
>
> raise AttributeError, a
>
> AttributeError: xlHAlignRight
>
> I’m hoping there is something obvious that I’m missing. I think I can
> find my way through the rest of my issues if I can just get by this
> constants problem. Thanks for any help you can provide.
>

Did you run "makepy.py" on the Microsoft Excel Object Library? "makepy"
is the one that extracts the constants.

Alternatively, you can replace
x = win32com.client.Dispatch("Excel.Application")
with
x = win32com.client.gencache.EnsureDispatch("Excel.Application")

That runs the "makepy" process, which makes the constants available:

C:\tmp>python
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import win32com.client
>>> x = win32com.client.Dispatch("Excel.Application")
>>> from win32com.client import constants
>>> constants.xlHAlignRight
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "c:\apps\python24\lib\site-packages\win32com\client\__init__.py",
line 18, in __getattr__
raise AttributeError, a
AttributeError: xlHAlignRight

C:\tmp>python
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import win32com.client
>>> x = win32com.client.gencache.EnsureDispatch("Excel.Application")
>>> from win32com.client import constants
>>> constants.xlHAlignRight
-4152
>>>

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-win32 mailing list