问题
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.'
回答1:
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:
Scroll down through the children of the DCOM Config node until you find an entry called Microsoft Excel Application:
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:
When you click the Edit button another dialogue box will open, in this window you can add the identity that your website runs under:
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:
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
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:
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.
来源:https://stackoverflow.com/questions/28195793/win32com-codes-not-working-on-iis