2

I am trying to deploy a Python app on IIS webserver whenever there is a code that uses win32com objects is encoutered, it throws error, but the code is working fine on Python built-in webserver Here is the code:

xlapp = win32com.client.Dispatch(r"Excel.Application")

and here is the error:

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.' 
halfer
  • 18,701
  • 13
  • 79
  • 158

2 Answers2

2

The clue is in the error message:

<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.'

It looks like the identity your python app runs under doesn't have permission to launch an Excel instance.

Update:

Excel will be launched as an out of process COM server. To allow your website permission to launch Excel and instantiate objects which as workbooks you need to configure the Launch and Activation permissions for Excel using a tool called dcomcnfg.exe.

You can launch dcomcnfg.exe from Start -> Run or from the command line. You need to be a local machine administrator as well.

Once launched expand the Component Services node and it's children just like in the screenshot below:

enter image description here

Scroll down through the children of the DCOM Config node until you find an entry called Microsoft Excel Application:

enter image description here

Right click on this entry and select Properties, a tabbed dialogue box will open. Select the Security tab then select the Launch and Activation Permissions Customize radio button, then click the Edit button, just like this:

enter image description here

When you click the Edit button another dialogue box will open, in this window you can add the identity that your website runs under:

enter image description here

Usually the site will run under what's known as the Application Pool Identity. It will normally be the same name as the site's application pool (unless you changed that).

You need to grant both Launch and Activate permissions to the pool identity. Do this by clicking the Add button, which displays:

enter image description here

Into the text box enter the pool identity prefixed with IIS AppPool\ (the space and backslash are important:

    IIS AppPool\[Your Application Pool Identity]

For example:

    IIS AppPool\DefaultAppPool

enter image description here

Click OK and you'll see your application pool identity added to the users list. Then make sure that the Local Launch and Local Activation Allow tick boxes are checked, just like this:

enter image description here

Once done, click OK, then click OK again.

Hopefully now you should be able to have your Python app launch Excel.

I should warn you that Excel (and other Office suite applications) are not designed (or licensed) to be used in web applications. It's possible to end up with hundreds of orphaned Excel (or Word) processes which will become a total management/resource hogging nightmare.

Kev
  • 112,868
  • 50
  • 288
  • 373
  • @AvinashNirankari what version of Windows/IIS are you deploying to? – Kev Jan 28 '15 at 22:15
  • :: On Windows Server 2008 R2 Standard The version of IIS is IIS 7 – Avinash Nirankari Jan 29 '15 at 07:20
  • :: I dont see any of the Office applications in my DCOM config manager. I have office installed on my server, but still... – Avinash Nirankari Feb 06 '15 at 13:31
  • I have managed to work around excel. But now Outlook is giving me problems. Whenever i am trying to dispatch Outlook it is throwing me error. The code is: olapp = win32com.client.gencache.EnsureDispatch("Outlook.Application") The error is as follows: : (-2146959355, 'Server execution failed', None, None) argerror = None args = (-2146959355, 'Server execution failed', None, None) excepinfo = None hresult = -2146959355 message = '' strerror = 'Server execution failed' Please help – Avinash Nirankari Feb 09 '15 at 11:59
0

Am facing the same problem when am trying to launch application named CANoe from Jenkins even after following the steps mentioned. If I execute script from CMD it works fine but problem is observed on launching from jenkins with my user ID

  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)
Ds Arjun
  • 349
  • 2
  • 6
  • 16