Win32com代码无法在IIS上运行

时间:2015-01-28 15:16:57

标签: python iis com webserver win32com

我试图在IIS网络服务器上部署一个Python应用程序,只要有一个使用win32com对象的代码被激活,它会抛出错误,但代码在Python内置的webserver上工作正常 这是代码:

xlapp = win32com.client.Dispatch(r“Excel.Application”)

这是错误:

xlapp undefined, global win32com = <module 'win32com' from 'C:\Python27\lib\site-packages\win32com\__init__.pyc'>, win32com.client = <module 'win32com.client' from 'C:\Python27\lib\site-packages\win32com\client\__init__.pyc'>, win32com.client.Dispatch = <function Dispatch> 
C:\Python27\lib\site-packages\win32com\client\__init__.py in Dispatch(dispatch='Excel.Application', userName=None, resultCLSID=None, typeinfo=None, UnicodeToString=None, clsctx=21) 
 93   """

 94   assert UnicodeToString is None, "this is deprecated and will go away"

 =>   95   dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)

 96   return __WrapDispatch(dispatch, userName, resultCLSID, typeinfo, clsctx=clsctx)

 97 

dispatch = 'Excel.Application', userName = None, global dynamic = <module 'win32com.client.dynamic' from 'C:\Python27\lib\site-packages\win32com\client\dynamic.pyc'>, dynamic._GetGoodDispatchAndUserName = <function _GetGoodDispatchAndUserName>, clsctx = 21 
C:\Python27\lib\site-packages\win32com\client\dynamic.py in _GetGoodDispatchAndUserName(IDispatch='Excel.Application', userName='Excel.Application', clsctx=21) 
113         else:

114                 userName = str(userName)

=>  115         return (_GetGoodDispatch(IDispatch, clsctx), userName)

116 

117 def _GetDescInvokeType(entry, default_invoke_type):

global _GetGoodDispatch = <function _GetGoodDispatch>, IDispatch = 'Excel.Application', clsctx = 21, userName = 'Excel.Application' 
C:\Python27\lib\site-packages\win32com\client\dynamic.py in _GetGoodDispatch(IDispatch='Excel.Application', clsctx=21) 
 90                         IDispatch = pythoncom.connect(IDispatch)

 91                 except pythoncom.ole_error:

=>   92                         IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)

 93         else:

 94                 # may already be a wrapped class.

IDispatch = 'Excel.Application', global pythoncom = <module 'pythoncom' from 'C:\windows\system32\pythoncom27.dll'>, pythoncom.CoCreateInstance = <built-in function CoCreateInstance>, builtin None = None, clsctx = 21, pythoncom.IID_IDispatch = IID('{00020400-0000-0000-C000-000000000046}') 

   <class 'pywintypes.com_error'>:(-2147024891, 'Access is denied.', None, None) 
  argerror = None 
  args = (-2147024891, 'Access is denied.', None, None) 
  excepinfo = None 
  hresult = -2147024891 
  message = '' 
  strerror = 'Access is denied.' 

2 个答案:

答案 0 :(得分:1)

线索在错误消息中:

  

&lt; class&#39; pywintypes.com_error&#39;&gt; :( - 2147024891,&#39; 访问被拒绝。&#39;,无,无)
  argerror =无
  args =( - 2147024891,&#39; 访问被拒绝。&#39;,无,无)
  excepinfo =无
  hresult = -2147024891
  message =&#39;&#39;
  strerror =&#39; 访问被拒绝。&#39;

看起来你的python应用程序运行的标识没有启动Excel实例的权限。

<强>更新

Excel将作为进程外COM服务器启动。要允许您的网站启动Excel并实例化作为工作簿的对象,您需要使用名为 dcomcnfg.exe 的工具为Excel配置启动和激活权限。

您可以从开始 - &gt;启动 dcomcnfg.exe 运行或从命令行运行。您还需要成为本地计算机管理员。

启动后,展开组件服务节点及其子节点,如下面的屏幕截图所示:

enter image description here

向下滚动 DCOM Config 节点的子节点,直到找到名为 Microsoft Excel Application 的条目:

enter image description here

右键单击此条目并选择属性,将打开一个选项卡式对话框。选择安全选项卡,然后选择启动和激活权限自定义单选按钮,然后单击编辑按钮,如下所示:

enter image description here

当您点击编辑按钮时,将打开另一个对话框,在此窗口中您可以添加您的网站运行的身份:

enter image description here

通常网站会在Application Pool Identity之下运行。它通常与站点的应用程序池名称相同(除非您更改了该名称)。

您需要向池标识授予启动和激活权限。通过单击添加按钮执行此操作,该按钮显示:

enter image description here

在文本框中输入前缀为IIS AppPool\的池标识(空格和反斜杠很重要:

    IIS AppPool\[Your Application Pool Identity]

例如:

    IIS AppPool\DefaultAppPool

enter image description here

单击“确定”,您将看到应用程序池标识已添加到用户列表中。然后确保选中本地启动本地激活允许复选框,如下所示:

enter image description here

完成后,点击确定,然后再次点击确定

希望你现在能够让你的Python应用程序启动Excel。

我应该警告您,Excel(和其他Office套件应用程序)未设计(或许可)用于Web应用程序。它可能最终导致数百个孤立的Excel(或Word)进程,这些进程将成为一场噩梦般的全面管理/资源。

答案 1 :(得分:0)

即使按照上述步骤尝试从 Jenkins 启动名为 CANoe 的应用程序时,我也面临同样的问题。如果我从 CMD 执行脚本,它工作正常,但在使用我的用户 ID 从 jenkins 启动时观察到问题

  File "C:\Program Files (x86)\Python279\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2147467238, 'The server process could not be started because the configured identity is incorrect. Check the username and password.', None, None)