Fork me on GitHub

VBA 调用 Python 函数

python

import xlwings as xw
def test_vba():
    wb = xw.Book.caller()
    sht = wb.sheets[0]
    sht.range('A1').value = '看到我了吗?'

Book.caller() 是 xlwings 提供的一个方法,用于获取调用该方法的 Excel 工作簿对象。通常,这个方法用在 Excel VBA 宏中,当你想从 VBA 宏中调用 Python 脚本并获取该脚本所在的工作簿对象。

VBA

Sub test_vba()
    RunPython ("from test import test_vba;test_vba()")
End Sub

参考:https://zhuanlan.zhihu.com/p/282110033

posted @ 2024-01-03 20:47  Cloudhan  阅读(196)  评论(0编辑  收藏  举报