通过XLwings将一个变量从Excel传递给Python [英] Passing a variable from Excel to Python with XLwings
本文介绍了通过XLwings将一个变量从Excel传递给Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Xlwings
从 Excel传递值
。我运行了一些带有加载项的示例,您需要导入用户定义的函数,但这似乎过于复杂。 为什么我的示例不工作?
VBA:
函数Hello(name As String)As String
RunPython(import Test; Test.sayhi(name))
结束函数
Python( Test.py
):
从xlwings import工作簿,范围
def sayhi(name):
wb = Workbook.caller )
return'Hello {}'。format(name)
错误:
NameError:name'name'未定义
解决方案
确保正确提供参数:
RunPython(import Test; Test.sayhi('&&'))
I am trying write a simple user defined function in Python that I pass a value to from Excel
via Xlwings
. I ran across some examples with an Add-in that you need to import user defined functions, but that seems overly complex.
Why isn't my example working?
VBA:
Function Hello(name As String) As String
RunPython ("import Test; Test.sayhi(name)")
End Function
Python (Test.py
):
from xlwings import Workbook, Range
def sayhi(name):
wb = Workbook.caller()
return 'Hello {}'.format(name)
Error:
NameError: name 'name' is not defined
解决方案
Make sure you're supplying the argument correctly:
RunPython ("import Test; Test.sayhi('" & name & "')")
这篇关于通过XLwings将一个变量从Excel传递给Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文