2

I keep receiving this error while trying to call the module.py file from excel

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "F:\ana\module.py", line 6, in rand_numbers
    wb = Workbook.caller()  # Creates a reference to the calling Excel file
AttributeError: type object 'Workbook' has no attribute 'caller'

When I replace wb = Workbook.caller() with wb = Workbook() I receive this error

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "F:\ana\module.py", line 11, in rand_numbers
    rand_num = np.random.randn(n, n)
  File "mtrand.pyx", line 1341, in mtrand.RandomState.randn (numpy\random\mtrand\mtrand.c:11537)
  File "mtrand.pyx", line 1454, in mtrand.RandomState.standard_normal (numpy\random\mtrand\mtrand.c:11839)
  File "mtrand.pyx", line 142, in mtrand.cont0_array (numpy\random\mtrand\mtrand.c:1867)
TypeError: an integer is required

Alternatively [scenario 2], I am able to call a python file from excel while using this sample code

from xlwings import Workbook, Sheet, Range, Chart
wb = Workbook()  # Creates a connection with a new workbook
#wb = Workbook.caller()
Range('A1').value = 'Foo 1'
Range('A2').value = [['Foo 1', 'Foo 2', 'Foo 3'], [10.0, 20.0, 30.0]]
Range('A13').table.value  # or: Range('A1:C2').value
Sheet(1).name
chart = Chart.add(source_data=Range('A2').table)

However the call in excel only works with wb = Workbook() and not wb = Workbook.caller()

I am aware of this API documentation update

module.py

import numpy as np
from xlwings import Workbook, Range

def rand_numbers():
    """ produces std. 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

VBA code

Sub MyMacro()
    RunPython ("import module; module.rand_numbers()")
End Sub

testing.py (test example code - scenario 2)

from xlwings import Workbook, Sheet, Range, Chart
wb = Workbook()  # Creates a connection with a new workbook
#wb = Workbook.caller()
Range('A1').value = 'Foo 1'
Range('A2').value = [['Foo 1', 'Foo 2', 'Foo 3'], [10.0, 20.0, 30.0]]
Range('A13').table.value  # or: Range('A1:C2').value
Sheet(1).name
chart = Chart.add(source_data=Range('A2').table)

VBA code

Sub MyMacro()
    RunPython ("import testing") 
End Sub
Community
  • 1
  • 1
phillipsK
  • 1,466
  • 5
  • 29
  • 43
  • Can you put the exact code you are running (both `module.py` and the VBA Macro? Also, is the python file in the same directory as the excel file? – Alejandro Mar 08 '15 at 02:06
  • added the code; yes ALL files are in the same directory – phillipsK Mar 08 '15 at 02:40
  • I did a search and replace for `ThisWorkbook` to `ActiveWorkbook` per http://stackoverflow.com/questions/28521702/when-excel-addin-runs-runpython-workbook-caller-raise-an-error but the code still came up short – phillipsK Mar 08 '15 at 02:49
  • 1
    Hmmm, and you're running the latest version of xlwings? I just tried it and it worked for me with version `0.3.2`. I'm using a Mac with all the out-of-the-box settings and just did a fresh install of xlwings using `pip`. – Alejandro Mar 08 '15 at 02:52
  • 1
    It really looks like you're operating on an old version of xlwings: Open a command prompt, type `python`, then `import xlwings` and `xlwings.__version__`. – Felix Zumstein Mar 08 '15 at 12:15
  • 1
    I had downloaded anaconda and while xlwings ships with conda, I had to update xlwings as the default version shipped was not the latest, `conda install xlwings` – phillipsK Mar 12 '15 at 00:54

1 Answers1

0

I get the same error exactly with xlwings version 0.3.4 and winpython 2.7.9.4 when cell B1 in spreadsheet is left as a null, hence the error "an integer is required". So passing in the dimension of the numpy array through cell B1 will fix the problem in the first set of example code.

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "mymodule.py", line 8, in rand_numbers
    rand_num = np.random.randn(n, n)
  File "mtrand.pyx", line 1352, in mtrand.RandomState.randn (numpy\random\mtrand\mtrand.c:13134)
  File "mtrand.pyx", line 1465, in mtrand.RandomState.standard_normal (numpy\random\mtrand\mtrand.c:13467)
  File "mtrand.pyx", line 145, in mtrand.cont0_array (numpy\random\mtrand\mtrand.c:1810)
TypeError: an integer is required
Charles
  • 439
  • 4
  • 18