xlwings module.py从excel调用python [英] xlwings module.py call python from excel

查看:55
本文介绍了xlwings module.py从excel调用python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试调用 module.py <时,我一直收到此错误./a>来自excel的文件

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'

当我将 wb = Workbook.caller()替换为 wb = Workbook()时,我会收到此错误

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

或者[方案2],我可以使用此示例代码

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)

但是excel中的调用仅适用于 wb = Workbook(),而不适用于 wb = Workbook.caller()

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

我知道此API文档更新

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代码

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

testing.py (测试示例代码-场景2)

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代码

Sub MyMacro()
    RunPython ("import testing") 
End Sub

推荐答案

当电子表格中的单元格B1保留为空时,xlwings版本0.3.4和winpython 2.7.9.4会出现完全相同的错误,因此出现错误必须为整数".因此,通过单元格B1传递numpy数组的维将解决第一组示例代码中的问题.

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

这篇关于xlwings module.py从excel调用python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆