1
所以我从我的Excel文件中用VBA和ExcelPython引用调用python脚本。该Python脚本工作正常,除了Excel中不断告诉我,我有注意的行类型不匹配:ExcelPython返回类型不匹配问题
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}
谢谢!
我不Python的工作,但无论是'PyVar'或'PyGetItem'期待特定类型的输入,它是没有得到这。因此输入不匹配错误。 –
有趣。就在你从Python函数返回之前,你能找出'type(capPrice)'是什么吗?虽然我不会说'PyVar'正在等待一个*特定的*类型(毕竟,它正在尝试创建一个VBA变体),但它可能仍然会变成VBA无法理解的类型。阻力最小的路径是在Python返回之前对Python进行一些额外的处理,将数据转换为VBA *可以理解的内容。 –