使用Python中的win32com加载Excel加载项 [英] Load Excel add-in using win32com from Python

查看:1228
本文介绍了使用Python中的win32com加载Excel加载项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从这里可以看到各种各样的问题,如果使用Python从Python中打开Excel的一个实例:

  xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
xl.Visible = True
wb = xl.Workbooks.Open('Test.xlsx')

然后它不加载默认加载项。我试图强制加载加载,而不是运行:

  xl = win32com.client.gencache.EnsureDispatch(' Excel.Application')
xl.Visible = True
addin = xl.Workbooks.Open('C:/path/addIn.xll')
wb = xl.Workbooks.Open(' Test.xlsx')

但是,当我这样做时,Excel的一个实例打开,我得到一个安全消息要求我验证我要打开此加载项。如果我单击仅启用此加载项,Excel将立即关闭,并且不会打开所需的xlsx文件。



有没有人有任何想法如何强制加载项加载,然后允许我打开我的文件?



非常感谢您的帮助!

解决方案

我实际上已经通过借用MSDN文章中与VBA做同样的事情来解决这个问题:



http://support.microsoft.com/default.aspx?scid = KB; en-us; q213489



以下内容现在完美完成:

  xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
xl.Visible = True
xl.RegisterXLL('C:/path/addin.xll' )
wb = xl.Workbooks.Open('Test.xlsx')


I've seen from various questions on here that if an instance of Excel is opened from Python using:

xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
xl.Visible = True
wb = xl.Workbooks.Open('Test.xlsx')

Then it does not load the default add-ins. I've tried forcing my add-in to load by instead running:

xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
xl.Visible = True
addin = xl.Workbooks.Open('C:/path/addIn.xll')
wb = xl.Workbooks.Open('Test.xlsx')

However, when I do this an instance of Excel opens and I get a security message asking me to verify I want to open this add-in. If I click "Enable this add-in for this session only" Excel immediately closes and does not open my desired xlsx file.

Does anyone have any ideas how to force the add-in to load and then to allow me to open my file?

Thanks very much for your help!

解决方案

I have actually managed to resolve this by borrowing something from this MSDN article relating to doing the same thing with VBA:

http://support.microsoft.com/default.aspx?scid=KB;en-us;q213489

The following now works perfectly:

xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
xl.Visible = True
xl.RegisterXLL('C:/path/addin.xll')
wb = xl.Workbooks.Open('Test.xlsx')

这篇关于使用Python中的win32com加载Excel加载项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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