Domanda

I'm currently wondering how to list the constants in win32com in python,

for example using excel win32com.client.Dispatch('Excel.Application')

Is there a way to display all constants using win32com.client.Constants ?

Or does someone know where i could find win32com's documentation ? Because all the links I found are dead ...

È stato utile?

Soluzione

To find out what Com applications you can use... See http://timgolden.me.uk/pywin32-docs/html/com/win32com/HTML/QuickStartClientCom.html

Basically you can't know for sure. Sorry. Each computer will have a different list based on the software installed, the webpage I linked suggests using pyWins ComBrowser, however I haven't ever found it that useful.

My normal approach is 'I want to interface with application X in Python... lets google "python com X" and see what comes up' or 'I want to interface with application X in Python.. lets look through the documentation of AppX for references to COM'

Afterall you'll want to have some form of documentation to that programmes COM interface in order to be able to do anything meaningful with the program (other than opening it).

Altri suggerimenti

The answer given by @CastleH in his comment pointed me in the right direction: help() helped me a lot, but you have to use it on module-level!

For example,

import win32print
help(win32print)

gives you all constants in win32print. You can do the same for every module in win32com (for a full list of modules see the win32com documentation, section modules).

Using help(win32com)did not help me a lot since it is mainly listing the packages

To list available constants, you can do something like:

import win32com

for k, v in win32com.client.constants.__dicts__[0].items(): 
    print("{:<45}: {}".format(k, v))

#  __module__                                   : win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9
#  xl3DBar                                      : -4099
#  xl3DEffects1                                 : 13
#  xl3DEffects2                                 : 14
#  xl3DSurface                                  : -4103
#  ...

The best resource for pywin32, even in 2020 (16+ years after its creation), is the source code1.

It turns out that win32com.client.constants is an instance of the Constants class:

class Constants:
  """A container for generated COM constants.
  """
  def __init__(self):
    self.__dicts__ = [] # A list of dictionaries
  def __getattr__(self, a):
    for d in self.__dicts__:
      if a in d:
        return d[a]
    raise AttributeError(a)

# And create an instance.
constants = Constants()

The constants are all stored in self.__dicts__ which is a list of dictionaries. That list happens to contain only a single dictionary.

Knowing this, you can iterate through the dictionary (as done above) to see what constants are available.


1 For dead links, try The Internet Archive's Wayback Machine. There are plugins for various browsers which make searching for archived versions of a page easy or automatic.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top