可能的错误? xlwings不能运行Excel宏? [英] Possible bug? xlwings cannot run an Excel macro?

查看:2062
本文介绍了可能的错误? xlwings不能运行Excel宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取xlwings从Python运行宏时,我遇到了一个问题。尽管遵循xlwings文档中的代码,但我无法获取xlwings来执行Excel宏。例如,在Excel工作簿中的名为Book.xlsm的Excel工作簿中:

 '在Excel工作簿中Book.xlsm 
子测试()
设置ws =工作表(ABC)
ws.Range(A1)。值= 10
End Sub
pre>

此宏在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屋!

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