comtypes
comtypes copied to clipboard
Conversion from 2d ndarray to VARIANT fails
I was trying to pass 2D VARIANT array to a COM object, but it was always passed as one dimensional array Then I tried to pass ndarray, but was getting type conversion error. Then I tried to create 2D VARIANT array directly using the code below.
`import comtypes.client as CC
import comtypes.safearray as CS
import comtypes.automation as CA
import numpy as np
arr = np.ones((2,2))
varObject = CS._midlSAFEARRAY(CA.VARIANT).create(arr)`
It fails in safearray.py at line 380: varr.flat = [VARIANT(v) for v in value.flat]
it's a part of the function :
def _ndarray_to_variant_array(value):
""" Convert an ndarray to VARIANT_dtype array """
# Check that variant arrays are supported
if npsupport.VARIANT_dtype is None:
msg = "VARIANT ndarrays require NumPy 1.7 or newer."
raise RuntimeError(msg)
# special cases
if numpy.issubdtype(value.dtype, npsupport.datetime64):
return _datetime64_ndarray_to_variant_array(value)
from comtypes.automation import VARIANT
# Empty array
varr = numpy.zeros(value.shape, npsupport.VARIANT_dtype, order='F')
# Convert each value to a variant and put it in the array.
varr.flat = [VARIANT(v) for v in value.flat]
return varr
Python version 3.7.7 COMTypes version 1.1.7
I'm having a similar issue. I have these lines of code which cause the issue, which is the same error that is thrown inside the automation.py file. Did you ever find a solution?
beta_data = np.array([[0.1,2.33,1.],[0.65,5.22,1.],[0.88,8.234,1.]])
varr = np.zeros(beta_data.shape, npsupport.VARIANT_dtype, order='F')
varr.flat = [VARIANT(v) for v in beta_data.flat]
