ExcelPython返回类型不匹配问题

时间:2014-03-31 02:56:48

标签: python excel vba excel-vba

所以我用我的Excel文件用VBA和ExcelPython引用调用python脚本。 python脚本工作正常,除了Excel一直告诉我,我在标注的行上有类型不匹配:

Function calcCapValue(times As Range, fRates As Range, strike As Range, vol As Range, delta As Double, pv As Range) As Variant

    Set methods = PyModule("PyFunctions", AddPath:=ThisWorkbook.Path)
    Set result = PyCall(methods, "CalculateCapValue", KwArgs:=PyDict("times", times.Value2, "fwdRates", fRates.Value2, "strike", strike.Cells(1, 1).Value2, "flatVol", vol.Cells(1, 1).Value2, "delta", delta, "pv", pv.Cells(1, 1).Value2))

    calcCapValue = PyVar(PyGetItem(result, "res"))   ' <--- TYPE MISMATCH HERE

    Exit Function

End Function

无法弄清楚原因,我正在按照此处的示例代码进行操作:https://sourceforge.net/p/excelpython/wiki/Putting%20it%20all%20together/ 在这里:http://www.codeproject.com/Articles/639887/Calling-Python-code-from-Excel-with-ExcelPython

这种类型仍然不匹配,我无法弄清楚原因。

这是python脚本:

#imports
import numpy as np
from scipy.stats import norm

#
#   Cap Price Calculation
#
def CalculateCapValue(times, fwdRates, strike, flatVol, delta, pv):

    capPrice = 0;


    #Iterate through each time for caplet price

    for i in range(0, len(times)):

        ifr = float(fwdRates[i][0])
        iti = float(times[i][0])

        d1 = (np.log(ifr/strike)+((flatVol**2)*iti)/2)/(flatVol*np.sqrt(iti))
        d2 = d1 - (flatVol*np.sqrt(iti))
        Nd1 = norm.cdf(d1)
        Nd2 = norm.cdf(d2)

        capPrice += pv*delta*(ifr*Nd1 - strike*Nd2)

    return {"res": capPrice}

谢谢!

1 个答案:

答案 0 :(得分:2)

在Sourceforge上的ExcelPython论坛上回答了问题:

http://sourceforge.net/p/excelpython/discussion/general/thread/97f6c1b0/