How to use win32com.client.constants with MS Word?

2024/11/13 10:21:58

Whats wrong with this code? Why win32com.client.constants doesn't have attribute wdWindowStateMinimize?

>>> import win32com.client
>>> w=win32com.client.Dispatch("Word.Application")
>>> w.WindowState = win32com.client.constants.wdWindowStateMinimize
Traceback (most recent call last):File "<pyshell#2>", line 1, in <module>w.WindowState = win32com.client.constants.wdWindowStateMinimizeFile "C:\Python34\lib\site-packages\win32com\client\__init__.py", line 170, in __getattr__raise AttributeError(a)
AttributeError: wdWindowStateMinimize`
Answer

You must use EnsureDispatch instead:

>>> w=win32com.client.gencache.EnsureDispatch('Word.Application')
>>> win32com.client.constants.wdWindowStateMinimize
2
>>>

Note that the first time you use EnsureDispatch on a particular COM server, pywin32 generates the COM type lib for it (Word in your case), so it can take many seconds. For Excel, it took almost 30 seconds. But after that, the dispatch is quick, and you can even use the regular Dispatch (so you could code your app to use Dispatch, which is faster than EnsureDispatch, and check if the constant is defined, and if not, the code uses EnsureDispatch).

See my answer to this other post for more details.

https://en.xdnf.cn/q/72340.html

Related Q&A

How to properly patch boto3 calls in unit test

Im new to Python unit testing, and I want to mock calls to the boto3 3rd party library. Heres my stripped down code:real_code.py:import boto3def temp_get_variable(var_name):return boto3.client(ssm).ge…

import a github into jupyter notebook directly?

Hey Im creating a jupyter notebook, would like to install: https://github.com/voice32/stock_market_indicators/blob/master/indicators.py which is a python program not sure how to do it directly so anybo…

Django : Call a method only once when the django starts up

I want to initialize some variables (from the database) when Django starts. I am able to get the data from the database but the problem is how should I call the initialize method . And this should be o…

Mocking instance attributes

Please help me understand why the following doesnt work. In particular - instance attributes of a tested class are not visible to Pythons unittest.Mock.In the example below bar instance attribute is no…

Are there any good 3rd party GUI products for Python? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

not able to get root window resize event

im trying to display the size(dimension) of the root window (top level window) on a label. whenever the user resize the window, new window dimensions should be displayed on the label. I tried to bind t…

Inverting large sparse matrices with scipy

I have to invert a large sparse matrix. I cannot escape from the matrix inversion, the only shortcut would be to just get an idea of the main diagonal elements, and ignore the off-diagonal elements (Id…

Error with tweepy OAuthHandler

Im new here and kind of unexperienced with python, so sorry if the question is trivial.I have this simple script, to fetch followers of a given twitter user:import time import tweepyconsumer_key="…

Overriding __or__ operator on python classes

As a contrived example, suppose Im generating a random fruit basket in python. I create the basket:basket = FruitBasket()Now I want to specify specific combinations of fruit that can occur in the baske…

python charmap codec cant decode byte X in position Y character maps to undefined

Im experimenting with python libraries for data analysis,the problem im facing is this exceptionUnicodeDecodeError was unhandled by user code Message: charmap codeccant decode byte 0x81 in position 165…