如何从python端自动将xlwings添加到VBA引用中



要激活excel book上的UDF,我们需要Tools>References>检查xlwings,如所示https://docs.xlwings.org/en/stable/addin.html

我想知道有没有任何方法可以从python端自动做到这一点?

import xlwings as xw
def test():    
wb = xw.books.add()
# wanna add something here to edit VBA reference.
wb.save("test.xlsm")

我希望这是可能的,因为xlwings快速启动可以(也许这是批量完成的(。

似乎以下内容将起作用:

if "xlwings" not in [i.Name for i in wb.api.VBProject.References]:
xlam_path = os.path.expanduser('~') + "\anaconda3\" + env_name
xlam_path += "\lib\site-packages\xlwings\addin\xlwings.xlam"
wb.api.VBProject.References.AddFromFile(xlam_path)

但如果有更好的方法,请告诉我。

最新更新