<br><br><div class="gmail_quote">On 19 November 2010 00:20, Joel Schwartz <span dir="ltr"><<a href="mailto:joel@joelschwartz.com">joel@joelschwartz.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div dir="ltr" align="left"><font color="#0000ff" size="2" face="Arial"><span>For those of us who are new to writing code that makes
various software packages interact with each other, can you say more about what
"COM object model" means in this context and where one can learn how to use it
to make Python interact with Excel and with Windows software in general. I've
seen term "COM" before and I know it has something to do with how Windows
programs interact with each other, but that's about it. Can you suggest some
resources for learning more?</span></font><br></div></div></blockquote></div><br>Sure, I'll just add to what Alan and Emile's already said. <br><br>The first thing (that's perhaps obvious but I'll just mention it explicitly) about COM is that it was one of the earlier attempts at solving the programming language and application interoperability problem. E.g, how do we write code so that other languages can easily consume our functionality, and vice versa? DDE was an early attempt by Microsoft at a solution, and COM was an attempt by Microsoft's at a more comprehensive and general answer to this. It basically defines a set of standards and API's that effectively can serve as a language-agnostic bridge between different programming languages, if those languages are "COM enabled" by their supporting and complying with COM's requirements. <br>
<br>An important extension or evolution of COM to networks was called DCOM (or "Distributed Component Object Model".) As the name aludes, it extends COM so that it can operate accross a network. In other words, have a program on one computer instantiate a component or object (possibly implemented in another language) on another computer, and work with this object as if it's a local object part of the lcoal program. Depending on how well a language supports and is integrated with COM/DCOM this may and should for the most part even look like a local native object in the language being programmed in. <br>
<br>DCOM eventually was further evolved into and eventually included under the moniker of COM itself, and a set of extensions to DCOM that solved/addressed a bunch of additional problems/concerns (like security and object pooling, that you would have to deal with yourself when using only COM/DCOM that typically occurs in the context of distributed applications) was then called COM+. (For more see <a href="http://ur.ly/v1Xy">http://ur.ly/v1Xy</a> and <a href="http://ur.ly/v8Ii">http://ur.ly/v8Ii</a>) <br>
<br>Anyway, suffice it to say, COM and friends introduces its own set of downsides and issues (suffice it to say I've lost a not insignificant amount of blood fighting with obscure DCOM security issues in the past), which eventually was at least one of the reasons for Microsoft eventually coming up with the .Net framework. (.Net then, is at one level another attempt at solving the language interoperability problem in a more seamless and elegant way. WSDL and web services standards were another, targetting the program interoperability accross the web/internet at large, COM/DCOM originally targetted local area networks and doesn't work very well or easily accross the internet.)<br>
<br>As for resources, there's a seemingly decent presentation on Python and COM and friends here: <a href="http://ur.ly/voQt">http://ur.ly/voQt</a><br><br>Also there's Activestate's documentation that's appropriate: <br>
<a href="http://docs.activestate.com/activepython/2.5/pywin32/html/com/win32com/HTML/GeneratedSupport.html">http://docs.activestate.com/activepython/2.5/pywin32/html/com/win32com/HTML/GeneratedSupport.html</a><br><a href="http://docs.activestate.com/activepython/2.5/pywin32/html/com/win32com/HTML/QuickStartClientCom.html">http://docs.activestate.com/activepython/2.5/pywin32/html/com/win32com/HTML/QuickStartClientCom.html</a><br>
<br>The package that enables Python to interoperate with COM applications/languages (as well as making available much of the Windows Win32 API's as well as the "PythonWin" application) is called "pywin32" and is available here:<br>
<a href="http://sourceforge.net/projects/pywin32/">http://sourceforge.net/projects/pywin32/</a><br><br>While the Python package "win32com" which is included in pywin32 (which enables Python to interoperate with COM applications/languages) provides a number of ways of querying and
accessing COM interfaces with few obvious differences between them (provided
you know the interface), it is usually more convenient when experimenting to know
what methods and properties are really available for a given interface as well as being more efficient to use so called "early-binding" when accessing COM objects. This
is where the "makepy" utility comes in. By selecting a particular type library and building the
Python wrappers using this tool in advance, more information becomes available since you and Python can then refer to the generated Python wrappers for casual investigation of object interfaces. All of this is also covered above in the ActiveState documentation as well as here: <a href="http://oreilly.com/catalog/pythonwin32/chapter/ch12.html">http://oreilly.com/catalog/pythonwin32/chapter/ch12.html</a><br>
<br>Hopefully that clarifies things a bit. I would suggest you try install the pywin32 package, then have a go at playing around with Excel perhaps interactively at first, and then with the tools and examples as outlined in some of the links I've posted. (As an aside, obviously I've commented mostly only on using Python as a COM client, it is also possible to write COM servers in Python, e.g. making your Python program available to other COM enabled programs and languages. Emile's post is relevant to this topic. )<br>
<br>HTH, <br><br>Walter<br>