可能的错误? xlwings不能运行Excel宏? [英] Possible bug? xlwings cannot run an Excel macro?
问题描述
'在Excel工作簿中Book.xlsm
pre>
子测试()
设置ws =工作表(ABC)
ws.Range(A1)。值= 10
End Sub
此宏在Excel中运行正常。但是当我尝试从Python调用此模块时,它会失败:
#in Python
import xlwings
wb = xlwings.Book('C:\\Book.xlsm')
wb.macro('Test')
print('done。'
没有错误消息。 Python代码只是运行和结束,打印消息完成。但是当我检查工作表ABC时,没有任何内容被写入。请注意,我可以连接到此工作簿,并使用xlwings更改单元格值。我只是不能得到它来运行测试宏。
另请注意,我使用了一个更老的xlwings(之前的0.7.0,我认为)之前,它运行我的宏没有问题。我现在使用0.10.0版本。
解决方案尝试以下操作:
在VBA中
:
Sub Test(number)
设置ws =工作表(Hoja1)
ws.Range(A1)。Value = number
End Sub
在python中:
import xlwings as xw
wb1 = xw.Book('Libro1.xlsm')
宏= wb1.macro('Test')
宏(10)
print('done。')
I am having a problem getting xlwings to run a macro from Python. Despite following the code from xlwings documentation, I cannot get xlwings to execute an Excel macro. For instance, in Excel workbook named "Book.xlsm":
' in Excel workbook Book.xlsm Sub Test() Set ws = Worksheets("ABC") ws.Range("A1").Value = 10 End Sub
This macro runs OK within Excel. But when I try calling this module from Python, it fails:
# in Python import xlwings wb = xlwings.Book('C:\\Book.xlsm') wb.macro('Test') print('done.')
No error messages. The Python code just runs and ends, printing the message "done." but when I check the worksheet ABC, nothing is written. Please note I am able to connect to this workbook and change cell values using xlwings. I just cannot get it to run the Test macro.
Also note I have used a much older xlwings (prior to 0.7.0, I think) before and it runs my macros with no problems. I am using the 0.10.0 version now.
解决方案Try the following:
In VBA:
Sub Test(number) Set ws = Worksheets("Hoja1") ws.Range("A1").Value = number End Sub
In python:
import xlwings as xw wb1 = xw.Book('Libro1.xlsm') macro=wb1.macro('Test') macro(10) print('done.')
这篇关于可能的错误? xlwings不能运行Excel宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!