xlwings 从 VBA 调用 python
xlwings Calling python from VBA
这是我在尝试从 Excel VBA [http://docs.xlwings.org/quickstart.html]
中调用 python 脚本后收到的错误
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named mymodule
Press Ctrl+C to copy this message to the clipboard.
我在哪里保存 module.py 文件,其中包含:
import numpy as np
from xlwings import Workbook, Range
def rand_numbers():
""" produces standard normally distributed random numbers with shape (n,n)"""
wb = Workbook.caller() # Creates a reference to the calling Excel file
n = Range('Sheet1', 'B1').value # Write desired dimensions into Cell B1
rand_num = np.random.randn(n, n)
Range('Sheet1', 'C3').value = rand_num
我已经将 xlwings.bas
作为模块导入到 Visual Basic 编辑器中,我在 anaconda 2.1.0
上安装了 python 2.7.8
$ which python
/c/ana/python
$ pip show xlwings
---
Name: xlwings
Version: 0.2.2
Location: c:\ana\lib\site-packages
Requires:
我假设 xlwings.bas 文件链接到我的 pythonpath
但子过程如何知道如何调用 module.py 文件 and/or 子过程如何知道程序知道 module.py 文件在哪里?
Sub RandomNumbers()
RunPython ("import mymodule; mymodule.rand_numbers()")
End Sub
默认情况下,xlwings VBA 模块需要 Python 文件与 Excel 文件位于同一目录中。这是可以在 xlwings VBA 模块中更改的 PYTHONPATH
设置。
这是我在尝试从 Excel VBA [http://docs.xlwings.org/quickstart.html]
中调用 python 脚本后收到的错误Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named mymodule
Press Ctrl+C to copy this message to the clipboard.
我在哪里保存 module.py 文件,其中包含:
import numpy as np
from xlwings import Workbook, Range
def rand_numbers():
""" produces standard normally distributed random numbers with shape (n,n)"""
wb = Workbook.caller() # Creates a reference to the calling Excel file
n = Range('Sheet1', 'B1').value # Write desired dimensions into Cell B1
rand_num = np.random.randn(n, n)
Range('Sheet1', 'C3').value = rand_num
我已经将 xlwings.bas
作为模块导入到 Visual Basic 编辑器中,我在 anaconda 2.1.0
$ which python
/c/ana/python
$ pip show xlwings
---
Name: xlwings
Version: 0.2.2
Location: c:\ana\lib\site-packages
Requires:
我假设 xlwings.bas 文件链接到我的 pythonpath
但子过程如何知道如何调用 module.py 文件 and/or 子过程如何知道程序知道 module.py 文件在哪里?
Sub RandomNumbers()
RunPython ("import mymodule; mymodule.rand_numbers()")
End Sub
默认情况下,xlwings VBA 模块需要 Python 文件与 Excel 文件位于同一目录中。这是可以在 xlwings VBA 模块中更改的 PYTHONPATH
设置。