Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

QuickStartClientCom.html 7.1KB

1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <HTML>
  2. <HEAD>
  3. <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
  4. <META NAME="Generator" CONTENT="Microsoft Word 97">
  5. <TITLE>Quick Start to Client side COM and Python</TITLE>
  6. <META NAME="Template" CONTENT="D:\Program Files\Microsoft Office\Office\html.dot">
  7. </HEAD>
  8. <BODY LINK="#0000ff" VLINK="#800080">
  9. <H1>Quick Start to Client side COM and Python</H1>
  10. <H2>Introduction</H2>
  11. <P>This documents how to quickly start using COM from Python. It is not a thorough discussion of the COM system, or of the concepts introduced by COM.</P>
  12. <P>Other good information on COM can be found in various conference tutorials - please see <A HREF="http://starship.python.net/crew/mhammond/conferences">the collection of Mark's conference tutorials</A></P>
  13. <P>For information on implementing COM objects using Python, please see <A HREF="http://www.python.org/windows/win32com/QuickStartServerCom.html">a Quick Start to Server side COM and Python</A></P>
  14. <P>In this document we discuss the following topics:</P>
  15. <UL>
  16. <LI><A HREF="#Using">Using a COM object from Python</A> </LI>
  17. <LI><A HREF="#WhatObjects">How do I know which objects are available?</A> </LI>
  18. <LI><A HREF="#StaticDispatch">Static Dispatch/Type Safe objects (using the new improved makepy.py)</A></LI>
  19. <LI><A HREF="#UsingComConstants">Using COM Constants with makepy.</A></LI></UL>
  20. <H2>Quick Start</H2>
  21. <H3><A NAME="Using">To use a COM object from Python</A></H3>
  22. <CODE><P>import win32com.client<BR>
  23. o = win32com.client.Dispatch("Object.Name")<BR>
  24. o.Method()<BR>
  25. o.property = "New Value"<BR>
  26. print o.property</P>
  27. </CODE><P>Example</P>
  28. <CODE><P>o = win32com.client.Dispatch("Excel.Application")<BR>
  29. o.Visible = 1<BR>
  30. o.Workbooks.Add() # for office 97 – 95 a bit different!<BR>
  31. o.Cells(1,1).Value = "Hello"</CODE> </P>
  32. <P>And we will see the word "Hello" appear in the top cell. </P>
  33. <H3><A NAME="WhatObjects">How do I know which methods and properties are available?</A></H3>
  34. <P>Good question. This is hard! You need to use the documentation with the products, or possibly a COM browser. Note however that COM browsers typically rely on these objects registering themselves in certain ways, and many objects to not do this. You are just expected to know.</P>
  35. <H4>The Python COM browser</H4>
  36. <P>PythonCOM comes with a basic COM browser that may show you the information you need. Note that this package requires Pythonwin (ie, the MFC GUI environment) to be installed for this to work.</P>
  37. <P>There are far better COM browsers available - I tend to use the one that comes with MSVC, or this one!</P>
  38. <P>To run the browser, simply select it from the Pythonwin <I>Tools</I> menu, or double-click on the file <I>win32com\client\combrowse.py</I></P>
  39. <H2><A NAME="StaticDispatch">Static Dispatch (or Type Safe) objects</A></H2>
  40. <P>In the above examples, if we printed the '<CODE>repr(o)</CODE>' object above, it would have resulted in</P>
  41. <CODE><P>&lt;COMObject Excel.Application&gt;</P>
  42. </CODE><P>This reflects that the object is a generic COM object that Python has no special knowledge of (other than the name you used to create it!). This is known as a "dynamic dispatch" object, as all knowledge is built dynamically. The win32com package also has the concept of <I>static dispatch</I> objects, which gives Python up-front knowledge about the objects that it is working with (including arguments, argument types, etc)</P>
  43. <P>In a nutshell, Static Dispatch involves the generation of a .py file that contains support for the specific object. For more overview information, please see the documentation references above.</P>
  44. <P>The generation and management of the .py files is somewhat automatic, and involves one of 2 steps:</P>
  45. <UL>
  46. <LI>Using <I>makepy.py</I> to select a COM library. This process is very similar to Visual Basic, where you select from a list of all objects installed on your system, and once selected the objects are magically useable. </LI></UL>
  47. <P>or</P>
  48. <UL>
  49. <LI>Use explicit code to check for, and possibly generate, support at run-time. This is very powerful, as it allows the developer to avoid ensuring the user has selected the appropriate type library. This option is extremely powerful for OCX users, as it allows Python code to sub-class an OCX control, but the actual sub-class can be generated at run-time. Use <I>makepy.py</I> with a </I>-i</I> option to see how to include this support in your Python code.</LI></UL>
  50. <P>The <I>win32com.client.gencache</I> module manages these generated files. This module has <A HREF="GeneratedSupport.html">some documentation of its own</A>, but you probably don't need to know the gory details!</P>
  51. <H3>How do I get at the generated module?</H3>
  52. <P>You will notice that the generated file name is long and cryptic - obviously not designed for humans to work with! So how do you get at the module object for the generated code?</P>
  53. <P>Hopefully, the answer is <I>you shouldn't need to</I>. All generated file support is generally available directly via <I>win32com.client.Dispatch</I> and <I>win32com.client.constants</I>. But should you ever really need the Python module object, the win32com.client.gencache module has functions specifically for this. The functions GetModuleForCLSID and GetModuleForProgID both return Python module objects that you can use in your code. See the docstrings in the gencache code for more details.</P>
  54. <H3>To generate Python Sources supporting a COM object</H3>
  55. <H4>Example using Microsoft Office 97.</H4>
  56. <P>Either:</P>
  57. <UL>
  58. <LI>Run '<CODE>win32com\client\makepy.py</CODE>' (eg, run it from the command window, or double-click on it) and a list will be presented. Select the Type Library '<CODE>Microsoft Word 8.0 Object Library</CODE>' </LI>
  59. <LI>From a command prompt, run the command '<CODE>makepy.py "Microsoft Word 8.0 Object Library"</CODE>' (include the double quotes). This simply avoids the selection process. </LI>
  60. <LI>If you desire, you can also use explicit code to generate it just before you need to use it at runtime. Run <CODE>'makepy.py -i "Microsoft Word 8.0 Object Library"</CODE>' (include the double quotes) to see how to do this.</LI></UL>
  61. <P>And that is it! Nothing more needed. No special import statements needed! Now, you simply need say</P>
  62. <CODE><P>&gt;&gt;&gt; import win32com.client</P>
  63. <P>&gt;&gt;&gt; w=win32com.client.Dispatch("Word.Application")</P>
  64. <P>&gt;&gt;&gt; w.Visible=1</P>
  65. <P>&gt;&gt;&gt; w</P>
  66. <P>&lt;win32com.gen_py.Microsoft Word 8.0 Object Library._Application&gt;</P>
  67. </CODE><P>Note that now Python knows the explicit type of the object.</P>
  68. <H3><A NAME="UsingComConstants">Using COM Constants</A></H3>
  69. <P>Makepy automatically installs all generated constants from a type library in an object called <I>win32com.clients.constants</I>. You do not need to do anything special to make these constants work, other than create the object itself (ie, in the example above, the constants relating to <I>Word</I> would automatically be available after the <CODE>w=win32com.client.Dispatch("Word.Application</CODE>") statement<CODE>.</P>
  70. </CODE><P>For example, immediately after executing the code above, you could execute the following:</P>
  71. <CODE><P>&gt;&gt;&gt; w.WindowState = win32com.client.constants.wdWindowStateMinimize</P>
  72. </CODE><P>and Word will Minimize.</P></BODY>
  73. </HTML>