我尝试使用
VBA
运行 python 脚本,发现 Xlwings
非常有用,如各种博客文章中所述。 我在终端窗口中使用以下行创建了一个项目:
xlwings quickstart hallo_welt
因此,在各自的文件夹“hallo_welt”中创建了一个 python 脚本和一个 xslm 文件。两者的名称相同。 如果我尝试点击“运行主要”按钮,它没有运行,但返回以下内容
error:Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'hallo_welt'
import xlwings as xw
def main():
wb = xw.Book.caller()
sheet = wb.sheets[0]
if sheet["A1"].value == "Hello xlwings!":
sheet["A1"].value = "Bye xlwings!"
else:
sheet["A1"].value = "Hello xlwings!"
if __name__ == "__main__":
xw.Book("hallo_welt.xlsm").set_mock_caller()
main()
因此,它应该返回“Hello xlwings!”在单元格 A1 中,我第一次单击“运行主程序”时。