NumPy int32 array to Excel through COM server is failing
Hello, We came across a problem trying to get an array across COM when wrapped as a COM server using the win32com extension. What caught our attention is that arrays of type float64 work fine but do not work for any other array. Does anyone know if there is something we could do at the NumPy level to make it work ? Arrays are mapped onto Variant/SafeArray and the conversion to a Variant seems to be failing for anything that is not a float64. It is not a huge deal for us to workaround the problem but it is kind of ugly and I just wanted to make sure there was not something simple that could be done (particularly if something like this would be considered a bug). I include working sample code below that reproduces the problem where Excel instantiates a com server and requests an array of size and dtype. On the Python side, the code sets up the com server and exposes the function that just returns an array of ones. Code in Excel to get an array: ======================================== Public Sub NumPyVariantTest() Dim npcom As Object Dim arr '... instantiate com object Set npcom = CreateObject("NPTest.COM") size = 7 '... test float64. Works ! arr = npcom.GetNPArray(size, "float64") '... test int32. Fails ! arr = npcom.GetNPArray(size, "int32") End Sub ======================================== Code in Python to set up com server and expose the GetNPArray function ======================================== import win32com.server.util import win32com.client from pythoncom import CLSCTX_LOCAL_SERVER, CLSCTX_INPROC import sys, os import numpy from numpy import zeros, ones, array class NPTestCOM(object): """COM accessible version of CommandInterface""" _reg_clsid_ = "{A0E551F5-2F22-4FB4-B28E-FF1B6809D21C}" _reg_desc_ = "NumPy COM Test" _reg_progid_ = "NPTest.COM" _reg_clsctx_ = CLSCTX_INPROC _public_methods_ = ['GetNPArray'] _public_attrs_ = [] _readonly_attrs_ = [] def GetNPArray(self, size, dtype): """ Return an arbitrary NumPy array of type dtype to check conversion to Variant""" return ones(size, dtype=dtype) if __name__ == '__main__': import win32com.server.register import _winreg dllkey = 'nptestdll' if len(sys.argv) > 1 and sys.argv[1] == 'unregister': win32com.server.register.UnregisterClasses(NPTestCOM) software_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE') vmg_key = _winreg.OpenKey(software_key, 'VMG') _winreg.DeleteKey(vmg_key, dllkey) _winreg.CloseKey(vmg_key) _winreg.CloseKey(software_key) else: win32com.server.register.UseCommandLine(NPTestCOM) software_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE') vmg_key = _winreg.CreateKey(software_key, 'VMG') _winreg.SetValue(vmg_key, dllkey, _winreg.REG_SZ, os.path.abspath(os.curdir)) _winreg.CloseKey(vmg_key) _winreg.CloseKey(software_key) ======================================== Regards, Raul -- Raul Cota (P.Eng., Ph.D. Chemical Engineering) Research & Development Manager Phone: (403) 457 4598 Fax: (403) 457 4637 Virtual Materials Group - Canada www.virtualmaterials.com
participants (1)
-
Raul Cota