我需要做一个脚本,将自动打开Excel与COM设置- SAP分析从Excel。我正在学习的语言,所以我不能找到解决方案自己对Github/Stackoverflow。它开始与一个简单的代码行,但最终我找到了下面的解决方案。但当我加载Excel,分析加载项不打开文件。然而,如果你打开Excel文件没有脚本,将显示加载项。
import win32com.client as win32
from pathlib import Path
sap_aof_excel_file = Path.cwd().joinpath(r"C:\Users\alexandrovn\Desktop\mo\MO2022.xlsm")
bwclient = "CODE"
bwuser = "LOGIN"
bwpassword = "PASS"
excel_instance = win32.gencache.EnsureDispatch('Excel.Application')
excel_instance.Visible = True
excel_instance.DisplayAlerts = False
workbook_sap = excel_instance.Workbooks.Open(sap_aof_excel_file, False, False)
for addin in excel_instance.Application.COMAddIns:
if addin.progID == 'SapExcelAddIn':
if addin.Connect == False:
addin.Connect = True
elif addin.Connect == True:
addin.Connect = False
addin.Connect = True
lResult = excel_instance.Application.Run("SAPLogon", "DS_1", bwclient, bwuser, bwpassword)
lResult = excel_instance.Application.Run("SAPExecuteCommand", "RefreshData", "DS_1")
workbook_sap.Save
workbook_sap.Close()
excel_instance.DisplayAlerts = True
excel_instance.Application.Quit()
excel_instance = None
workbook_sap = None
但是,当您加载Excel时,分析加载项不会在文件中打开。但是,如果您打开不带脚本的Excel文件,则会显示加载项。出现的错误如下:
C:\Users\alexandrovn\Anaconda3\python.exe C:/Users/alexandrovn/Desktop/mo/test.py
Traceback (most recent call last):
File "C:\Users\alexandrovn\Desktop\mo\test.py", line 30, in <module>
addin.Connect = True
File "C:\Users\alexandrovn\Anaconda3\lib\site-packages\win32com\client\dynamic.py", line 549, in __setattr__
self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
pywintypes.com_error: (-2147352567, 'Error.', (0, 'Microsoft.VisualStudio.Tools.Applications.Hosting', 'Failed to install the add-on.', None, 0, -2146233088), None)
Process finished with exit code 1
请解释错误以及如何使用此加载项启动Excel?可能存在访问问题(通过公司网络工作,并且在PC上没有管理员权限)
1条答案
按热度按时间8ehkhllq1#
我不认为代码会持久化COM加载项。最好使用管理员帮助并在AO中启用加载项