Discussion:
[python-win32] I need to make a COM object with py2exe. Please HELP!
Diego Vélez Torres
2015-02-24 16:53:17 UTC
Permalink
Hi everyone out there:

I need to make a COM object upon my Python code, so I can call it from .NET
programs (ie. Visual Basic, VB6). I need the COM object (or DLL) to be
stand alone, because the target PCs where the final programs are going to
run won't have Python installed. I know so far that py2exe supports this
directly: http://www.py2exe.org/index.cgi/Py2exeAndCtypesComDllServer

I've tried to make some samples with the given link's instructions, but I
haven't been lucky. I only got empty 'dist' and 'build' folders. I've
search for documentation on the web for quite a long time with no luck. I
realized that I need more information besides the one provided by the
above's link:


1. How the Python module that I want to compile must be written? I
mean, all functions and classes in module will be available for the outside
code through the COM Object? That module needs specific lines of code?
2. py2exe looks for a __init__.py inside the 'dir' where the module I
want to compile is. What's the purpose of the __init__ file? What should
be in it?
3. Are there any specific information about the configuration of the
setup.py? Even though the given link's code example explains something, it
is not clear (at least for me). That's why I got just empty folders as a
result.
4. Are there more examples available?
5. etc.


I'll highly appreciate if you can provide with the most complete
documentation and tutorial available (step by step if possible). Please,
consider that my experience with DLLs and COM objects is practically null.
--
Diego Vélez Torres
***@gmail.com
Teléfono: 098 28 57 58
Cuenca, Ecuador
Tim Roberts
2015-02-24 18:07:09 UTC
Permalink
Post by Diego Vélez Torres
I need to make a COM object upon my Python code, so I can call it from
.NET programs (ie. Visual Basic, VB6). I need the COM object (or DLL)
to be stand alone, because the target PCs where the final programs are
going to run won't have Python installed. I know so far that py2exe
http://www.py2exe.org/index.cgi/Py2exeAndCtypesComDllServer
I've tried to make some samples with the given link's instructions,
but I haven't been lucky. I only got empty 'dist' and 'build' folders.
I've search for documentation on the web for quite a long time with no
luck. I realized that I need more information besides the one provided
1. How the Python module that I want to compile must be written? I
mean, all functions and classes in module will be available for
the outside code through the COM Object? That module needs
specific lines of code?
What you are trying to create is a COM server. If you search for
"python COM server", you will find many examples. It does have to be
written in a particular way. You have to identify the functions you
want to be exposed, the registry program ID, and the registry GUID in
specially named class members (_public_methods_, _reg_progid_, _reg_clsid_).

I suggest you make the COM part work first. Once you have that working,
then you can dig in to py2exe to make it standalone.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Tim Roberts
2015-03-09 22:01:20 UTC
Permalink
I just purchased M. Hammond's "Python Programming on Win32" book.
Though a bit old the (1999) still is of great help. I have been able
to make simple COM servers so far. However, I have a question
regarding the code examples found in his http://starship.python.net/
web site. Are they written in Python 1.x... or Python 2.x? I wonder
if there are code examples written for Python 3.x?
They are almost certainly Python 2. Python 1 was quickly replaced by
Python 2 in the 1990s.

The differences between 2 and 3 are small enough that the samples are
still useful. The most significant annoying difference is the print
function, and you can fix that with a text editor. Occasionally, I'll
have an issue with the change in string type to Unicode, but that's
about it. The COM stuff still works exactly the same as it did before.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Diego Vélez Torres
2015-04-30 21:06:47 UTC
Permalink
Hi Tim:

After studying the "Python Programming on Win32" book, I was able to make
COM servers . But so far I can pass simple arguments (in both directions)
between Visual Basic and the Python COM server (e.g. integers, strings,
floats and arrays). I wonder if it's possible to return from the COM
server other kind of objects such as graphics to be used directly from the
.NET program.

To be more precise, I have Python code that uses heavily Numpy and
Matplotlib. I want to make a COM server that returns images generated with
Matplotlib in order to use within Excel. The best solution I came out was
to store the Matplotlib's images as .PNG in temporary files. The COM
server would return their paths so Visual Basic could insert them into the
spreadsheet with its own commands.

Though this solution may work, the images cannot be interactive. A change
would require the creation of new images each time, store them, reload them
back, etc... all processes together are expensive. Therefore, I wish I
could return directly from the COM server an image object that Visual Basic
could handle. Another reason why I want to return an image object directly
is because I want to use remote COM Servers (DCOMs); where the path
solution may not work.

There are two things I think should be in the answer:

1) How to make image objects in the COM server so the .NET programs can
use.
2) Can Matplotlib create an image that can be used within .NET GUIs.

Sincerely yours,
Diego
Thank you Tim. Your advise is been of great help.
I just purchased M. Hammond's "Python Programming on Win32" book. Though
a bit old the (1999) still is of great help. I have been able to make
simple COM servers so far. However, I have a question regarding the code
examples found in his http://starship.python.net/ web site. Are they
written in Python 1.x... or Python 2.x? I wonder if there are code
examples written for Python 3.x?
Cheers to all in the mailing list.
Post by Tim Roberts
Post by Diego Vélez Torres
I need to make a COM object upon my Python code, so I can call it from
.NET programs (ie. Visual Basic, VB6). I need the COM object (or DLL)
to be stand alone, because the target PCs where the final programs are
going to run won't have Python installed. I know so far that py2exe
http://www.py2exe.org/index.cgi/Py2exeAndCtypesComDllServer
I've tried to make some samples with the given link's instructions,
but I haven't been lucky. I only got empty 'dist' and 'build' folders.
I've search for documentation on the web for quite a long time with no
luck. I realized that I need more information besides the one provided
1. How the Python module that I want to compile must be written? I
mean, all functions and classes in module will be available for
the outside code through the COM Object? That module needs
specific lines of code?
What you are trying to create is a COM server. If you search for
"python COM server", you will find many examples. It does have to be
written in a particular way. You have to identify the functions you
want to be exposed, the registry program ID, and the registry GUID in
specially named class members (_public_methods_, _reg_progid_, _reg_clsid_).
I suggest you make the COM part work first. Once you have that working,
then you can dig in to py2exe to make it standalone.
--
Providenza & Boekelheide, Inc.
_______________________________________________
python-win32 mailing list
https://mail.python.org/mailman/listinfo/python-win32
--
Diego Vélez Torres
Teléfono: 098 28 57 58
Cuenca, Ecuador
--
Diego Vélez Torres
***@gmail.com
Teléfono: 098 28 57 58
Cuenca, Ecuador
Tim Roberts
2015-05-01 18:09:45 UTC
Permalink
Post by Diego Vélez Torres
After studying the "Python Programming on Win32" book, I was able to
make COM servers . But so far I can pass simple arguments (in both
directions) between Visual Basic and the Python COM server (e.g.
integers, strings, floats and arrays). I wonder if it's possible to
return from the COM server other kind of objects such as graphics to
be used directly from the .NET program.
To be more precise, I have Python code that uses heavily Numpy and
Matplotlib. I want to make a COM server that returns images generated
with Matplotlib in order to use within Excel. The best solution I
came out was to store the Matplotlib's images as .PNG in temporary
files. The COM server would return their paths so Visual Basic could
insert them into the spreadsheet with its own commands.
Though this solution may work, the images cannot be interactive. A
change would require the creation of new images each time, store them,
reload them back, etc... all processes together are expensive.
Therefore, I wish I could return directly from the COM server an image
object that Visual Basic could handle. Another reason why I want to
return an image object directly is because I want to use remote COM
Servers (DCOMs); where the path solution may not work.
You are trying to cross-connect several very different worlds here. You
are going to be in for a lot of frustrating experimentation.

HOWEVER, before we start brainstorming wild ideas, let me point out
something that you may have overlooked. You, right now, are using the
CPython implementation, which runs the interpreter as native code. If
you are going to spend a lot of time interacting with .NET, you may not
be aware that there is a complete .NET implementation of Python called
IronPython. It is a full .NET language, supported in Visual Studio,
running under the same Common Language Runtime as the other .NET
languages. And, it supports both NumPy and SciPy.

Theoretically, it should be possible to create an IronPython assembly
and invoke it directly from Excel. It's possible that would be your
least effort solution.

If there is a reason you can't do that (and do some serious thinking
before you discard it), then you probably have to rely on COM. Your COM
object can return other COM objects, although I don't think there are
any interfaces that will be recognized by Excel as native graphics objects.

--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Waldemar Osuch
2015-05-01 19:34:54 UTC
Permalink
This project seems to be relevant to the discussion.
http://xlwings.org/

It's Excel <-> Python integration. Maybe you do not have to start from
scratch to achieve your goal.

w/o
Diego Vélez Torres
2015-05-05 00:14:06 UTC
Permalink
Hi Tim and Waldemar:

I have tried ironPython before. I guess that is pretty straight to compile
an ironPython code into a DLL so other .NET program can call it. I never
done it before, but I followed an example of a DLL made with C++ that later
was called from an ironPython program. However, in order to use Matplotlib
within ironPython, this library has to be called with a CPython engine
embedded. In fact, the guys of Resolver Systems started "The C Extensions
for IronPython Project" in order to use any CPython library with
ironPython. But this doesn't solve my original question. It seems that
Matplotlib (not any library out there) doesn't generate a usable image
objects for Excel. So I think, at the end there would not be any
difference if I go for the COM server solution or the ironPython one.

I have been checking some external libriaries and APIs that integrates
Excel with Python. Among them is "xlwings" as suggested by Waldermar.
I'll write my findings so other people reading this forums may benefit:


- xlwings: I think this is the earliest package and is open source. I
never tried, but it seems very suitable for replacing VBA for Python in
Excel spreadsheets. I checked if there's any easy way to bring to the
spreadsheet images generated in the Python code , but I didn't find any.
Therefore, the only way may be the one I found with the COM servers: save
an image file with python, and then load the image with VBA.


- ExcelPython: Also is open source, and newer than xlwings. It seems
to be better for writting Excel Functions (UDFs). But still relies on VBA
on the spreadsheet. I don't know about importing images.


- PyXll: This is not an open source, and its fee is quite expensive
($250). It's been around for 5 years and it works very well in all
issues. It manages images very well, but looking more in detail, it
imports images through temporally files. However, I don't have to write
any code in VBA to do so. It relies on PyQt to do so, and some code in
Python is required.


- DataNitro: This in not open source and its fee is $99. This is
basically an Excel Add-In. This is the only one I tested directly. It does
work very good in almost everything. It imports images beautifully. It
has a PyPlot wrapper called "nitroplot" that behaves exactly the same, but
as if it were native to Excel. There're examples of dynamic images working
on the spreadsheet. I have no idea how DataNitro imports images. However,
I found that this Add-In doesn't work on any version of Excel. For
example, I use a Spanish version and there are some issues very difficult
(sometimes impossible) to fix.


The following blog makes a very interesting comparision
https://www.pyxll.com/blog/performance-comparison/ but for obvious
reasons is biased towards PyXll.

As I don't need dynamic images, I'll go through the route with COM servers
and and temporally files in order to import images.

So TIM: Now that I know how to build COM servers, can you please give my
lights how can I make a stand alone one using py2exe? I need to install
in computers were the clients may not even know that Python exists.

Thanks everybody for reading and writing about this topic.
Best regards,
Diego
Post by Waldemar Osuch
This project seems to be relevant to the discussion.
http://xlwings.org/
It's Excel <-> Python integration. Maybe you do not have to start from
scratch to achieve your goal.
w/o
_______________________________________________
python-win32 mailing list
https://mail.python.org/mailman/listinfo/python-win32
--
Diego Vélez Torres
***@gmail.com
Teléfono: 098 28 57 58
Cuenca, Ecuador
Mark Hammond
2015-05-05 08:17:51 UTC
Permalink
Post by Diego Vélez Torres
So TIM: Now that I know how to build COM servers, can you please give
my lights how can I make a stand alone one using py2exe? I need to
install in computers were the clients may not even know that Python exists.
py2exe comes with some sample COM objects - your best bet is to start
with them and the py2exe wiki.

Mark
divelez69
2015-05-05 11:54:33 UTC
Permalink
Thanks Mark


Sent from Samsung Mobile
Post by Diego Vélez Torres
So TIM: Now that I know how to build COM servers, can you please give
my lights how can I make a stand alone one using py2exe? I need to
install in computers were the clients may not even know that Python exists.
py2exe comes with some sample COM objects - your best bet is to start
with them and the py2exe wiki.

Mark

Loading...