Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 4th, 2004, 01:12 PM
oli oli is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 56 oli User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 13 sec
Reputation Power: 6
Com

I'm trying to access some COM objects with Python. I read some sample code with the following line:

o = win32com.client.Dispatch("Object.Name")

eg.
o = win32com.client.Dispatch("Excel.Application")

How do I know what I should put as "Object.Name"? I couldn't find anything from the doc of my COM objects.

Reply With Quote
  #2  
Old June 4th, 2004, 01:50 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,548 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 109876 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109876 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109876 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109876 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109876 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109876 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 1 h 44 m 38 sec
Reputation Power: 378
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
you mean..
Code:
import win32com
print dir( win32com )

there you have mostly everything, run the code

Reply With Quote
  #3  
Old June 4th, 2004, 03:40 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,224 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 23 h 4 m 39 sec
Reputation Power: 263
Quote:
Originally Posted by oli
How do I know what I should put as "Object.Name"? I couldn't find anything from the doc of my COM objects.


It should be stated in the docs for the COM object, but it often isn't (M$ docs are particularly bad for this).

There is one way I know to find out, but it is a bit long-winded and there may well be easier ways.

1) open the PythonWin IDE and select the "tools/COM makepy utility" menu entry.

2) select the type library that you want in the listbox and click OK. This will generate the stub file for that type library.

3) navigate to Python\Lib\site-packages\win32com\gen_py and open the .py file you have just generated. The files are named after the library GUID, so it is easiest to use the timestamp to find the most recent file.

4) The stub file will have a class derived from 'CoClassBaseClass' for each class in the type library. The comment just before the class definition gives the name that you want. e.g.

Code:
from win32com.client import CoClassBaseClass
# This CoClass is known by the name 'Excel.Application.8'
class Application(CoClassBaseClass): # A CoClass
	CLSID = IID('{00024500-0000-0000-C000-000000000046}')
	coclass_sources = [
		AppEvents,
	]
	default_source = AppEvents
	coclass_interfaces = [
		_Application,
	]
	default_interface = _Application


Generating the stubs has other advantages too. It not only speeds up accessing the COM objects, but it also enables you to query the interfaces in interactive mode. e.g.

Code:
>>> app = win32com.client.Dispatch("Excel.Application")
>>> dir(app)
['ActivateMicrosoftApp', 'AddChartAutoFormat', 'AddCustomList', 'CLSID', 'Calculate', 'CentimetersToPoints',...]
>>> 


Dave - The Developers' Coach

Last edited by DevCoach : June 4th, 2004 at 03:46 PM.

Reply With Quote
  #4  
Old June 4th, 2004, 04:14 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,224 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 23 h 4 m 39 sec
Reputation Power: 263
I should add that not every class will have the comment at the start - I think it is only those that are intended to be creatable directly by calls to win32com.client.Dispatch(). Other classes will be generated only by method calls on existing objects.

Note that for Excel at least, it is not always necessary to start by creating the Application object. If you create a Sheet object then the Application object will be automatically created along with a single Sheet, and can be accessed by the Sheet.Application property.

Dave - The Developers' Coach

Reply With Quote
  #5  
Old June 7th, 2004, 09:07 AM
oli oli is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 56 oli User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 13 sec
Reputation Power: 6
There's a line like this in the .py file that the makepy utility generated.

# This CoClass is known by the name 'SMIEngine.SMIHost.1'

I tried
w=win32com.client.Dispatch("SMIEngine.SMIHost")
but I got the following error message

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python23\Lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "C:\Python23\Lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Python23\Lib\site-packages\win32com\client\dynamic.py", line 79, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
com_error: (-2147467262, 'No such interface supported', None, None)

Did I do anything wrong?

Thank you.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Com


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT