[python-win32] How to call Python methods and properties from VBS?

Chris Lambacher chris at kateandchris.net
Tue Sep 6 21:22:41 CEST 2011


*sigh*

I responded to him and didn't notice that he only responded to me and
the list bounce address. Here is *my* response to Bruce which went to
him off list:


Hi Bruce,

You have to write some Python objects that have special properties
that describe the COM interface to expose. That is the "class
PythonUtilities: ..." part of the example I linked to. Then there is
some magic involved in the "if __name__== '__main__': ..." section of
the example. That does the registration and you don't need to point
windows to a particular DLL like you would with C based COM server but
you need to have Python and pywin32 installed. I have included the if
portion below because it is a bit mangled in the web page:

# Add code so that when this script is run by
# Python.exe, it self-registers.
if __name__=='__main__':
   print "Registering COM server..."
   import win32com.server.register
   win32com.server.register.UseCommandLine(PythonUtilities)


If you want to not require Python and pywin32 be installed you have to
do a little bit more work. Python code does not compile to a DLL, but
there are tools that you can use to package things up into their own
self contained distributable package. Two options that should work are
Py2Exe and PyInstaller.

Instructions for configuring Py2Exe to work with a pywin32 com server:
http://www.py2exe.org/index.cgi/Py2exeAndWin32com

Instructions for configuring PyInstaller:
http://www.pyinstaller.org/export/latest/trunk/doc/Manual.html?format=raw#windows-com-server-support

My recommendation is to worry about getting your code to work first
and then worry about distribution to boxes that don't already have
Python and PyWin32.

-Chris

On Sun, Sep 4, 2011 at 2:57 PM, FT <Chester_Lab at fltg.net> wrote:
> Hi Chris,
>
>    I had read that before you responded and could not get a clear picture
> there.
>
>    In other words, I have a compiled python library or python.DLL and other
> dll's and pyd files.
>    Which one, or how to get access to them is what I could not find in that
> chapter 12 description.
>
>    I wanted to have a compiled Python distribute folder and be able to call
> on methods that reside there either in the python.dll or any of the .pyd
> files.
>
>        Bruce
>



On Tue, Sep 6, 2011 at 12:24 AM, Mark Hammond <mhammond at skippinet.com.au> wrote:
> Please keep replies on the python-win32 list - others may also be able to
> help you too.
>
> You might like to check out the sample in samples\pywin32\com_server in the
> py2exe distribution - that demonstrates a simple COM object and has a
> VBScript sample which calls it.
>
> Hope this helps,
>
> Mark
>
> On 6/09/2011 1:38 PM, FT wrote:
>>
>> Hi Mark,
>>
>>     I wanted to have the python and other modules from a py2exe accessible
>> from vb script. So I would need to have the best documentation on how to
>> set
>> up the python com library to be able to call it from vb script. This is
>> not
>> using a web server, but inside a screen reader script language. The one I
>> am
>> using is Windoweyes Scripting language which has all or most windows
>> methods
>> and the external xml file for screen displays.
>>
>>     I am a blind programmer and use the screen reader with a built in vb
>> script programming language. So, knowing Python I want to be able to call
>> those module methods at will. I have made executables but they reside in a
>> sub directory of Windoweyes and are limited to only what the .exe file
>> does
>> and not flexible beyond that point.
>>
>>     I was given snap shots of examples but they are very spotty and actual
>> say they show a vb script example, but none was displayed.
>>
>>     I am assuming that you have to make a class for all the modules you
>> want
>> to be able to call, but not really sure on that. I have spent all evening
>> trying to find examples and such and came up with almost nothing but an
>> old
>> activestate 2.4 brief description which said little on what to do.
>>
>>     So, this is where I am at the moment and have found little to explain
>> fully what to do. It showed a way to set up the py2exe but the so called
>> example call from vb script was not there but it said it was. The typical
>> hello script example.
>>
>>     So, what I need is a very good tutorial on how to step by step make
>> the
>> class and compile it to a distributle so it can be registered and called
>> as
>> a com or automation call from vb script. Using what I would assume to be
>> the
>> CreatObject command in vbs to make an instance of that
>> object/module/method.
>>
>>     I am blind, so no pictures, I need just plain text to do it.
>>
>>         Sincerely
>>         Bruce
>>
>> Sent: Monday, September 05, 2011 6:51 PM
>> Subject: Re: [python-win32] How to call Python methods and properties from
>> VBS?
>>
>>
>> On 4/09/2011 11:35 PM, FT wrote:
>>>
>>> Hi Again,
>>> To be more specific I have compiled the Python inside a distributed
>>> package and need to know how to call from within that compiled package
>>> any method. In other words do I have to register the Python inside my
>>> .vbs script before calling any methods?
>>> I am saying this so another user does not need to install Python, just
>>> call using com inside the distributed Python compiled version.
>>> If so, how do I get the Python setup or in a form to call? I will be
>>> doing all the calls from .VBS and how would the CreateObject from there
>>> be done to setup to call the compiled Python interpreter?
>>
>> You should develop and test using normal Python, then once you want to
>> distribute it to others, look into py2exe to "compile" it and an
>> installer program such as nsis or inno so the user can simply install it.
>>
>> Hope this helps,
>>
>> Mark
>>
>
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
>



-- 
Christopher Lambacher
chris at kateandchris.net


More information about the python-win32 mailing list