gpt4 book ai didi

python - xlwings module.py 从 excel 调用 python

转载 作者:行者123 更新时间:2023-12-04 21:04:03 27 4
gpt4 key购买 nike

我在尝试调用 module.py 时不断收到此错误消息来自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()我收到此错误
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],我可以在使用 this sample code 时从 excel 调用 python 文件
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()
我知道 this API documentation update

模块.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

测试.py (测试示例代码 - 场景 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 数组的维度将解决第一组示例代码中的问题。

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

关于python - xlwings module.py 从 excel 调用 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28921933/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com