Author Topic: Message "PermissionError: 13 Access is denied" on simple menu example  (Read 1560 times)

jonesrl

  • Newbie
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Im a novice Python programmer and am working my way through the Pyxll examples. The Pyxll example spreadsheet seems to say  the right things to show the correct pieces are there. (Running EXCEL 2007).

Ive used the code in automation.py as a guide to the following

import pyxll
import win32api
import win32com.client
from pyxll import xl_menu, xl_func, xl_macro

def xl_app2():
    xl_window=pyxll.get_active_object()
    xl_app2=win32com.client.Dispatch(xl_window).Application
    win32com.client.gencache.EnsureDispatch(xl_app2)
    return xl_app2()
@xl_menu("Start",menu="EncompassXSD")
def on_hello():
    # Format sheets
    win32api.MessageBox(0,"Hello from here!","New Menu button eg")

@xl_menu("Select Provider",menu="EncompassXSD",sub_menu="Start")
#@xl_macro()
def win32com_menu_test():
    #print (xl_app().Selection)
    select=xl_app2().Selection
    select.Value="ABCD"
    pyxll.xlcAlert("Some text in current cell")
    win32api.MessageBox(0,"Hello from RLJ!","sub Menu button eg")
   
When i click on the "select Provider" submenu I get a box "PermissionError (13,"Access is denied")

The log shows

2014-12-03 15:25:14,027 - ERROR : Traceback (most recent call last):
2014-12-03 15:25:14,027 - ERROR :   File "C:\Users\Richard\Dropbox\RLJ stuff\Python\Pyxll\PyXLLTest\RLJTest2.py", line 20, in win32com_menu_test
2014-12-03 15:25:14,028 - ERROR :     select=xl_app2().Selection
2014-12-03 15:25:14,028 - ERROR :   File "C:\Users\Richard\Dropbox\RLJ stuff\Python\Pyxll\PyXLLTest\RLJTest2.py", line 9, in xl_app2
2014-12-03 15:25:14,028 - ERROR :     win32com.client.gencache.EnsureDispatch(xl_app)
2014-12-03 15:25:14,028 - ERROR :   File "C:\Program Files\Python\Lib\site-packages\win32com\client\gencache.py", line 536, in EnsureDispatch
2014-12-03 15:25:14,029 - ERROR :     mod = EnsureModule(tla[0], tla[1], tla[3], tla[4], bForDemand=bForDemand)
2014-12-03 15:25:14,029 - ERROR :   File "C:\Program Files\Python\Lib\site-packages\win32com\client\gencache.py", line 520, in EnsureModule
2014-12-03 15:25:14,029 - ERROR :     module = MakeModuleForTypelib(typelibCLSID, lcid, major, minor, progressInstance, bForDemand = bForDemand, bBuildHidden = bBuildHidden)
2014-12-03 15:25:14,029 - ERROR :   File "C:\Program Files\Python\Lib\site-packages\win32com\client\gencache.py", line 287, in MakeModuleForTypelib
2014-12-03 15:25:14,030 - ERROR :     makepy.GenerateFromTypeLibSpec( (typelibCLSID, lcid, major, minor), progressInstance=progressInstance, bForDemand = bForDemand, bBuildHidden = bBuildHidden)
2014-12-03 15:25:14,030 - ERROR :   File "C:\Program Files\Python\Lib\site-packages\win32com\client\makepy.py", line 268, in GenerateFromTypeLibSpec
2014-12-03 15:25:14,030 - ERROR :     os.mkdir(full_name)
2014-12-03 15:25:14,030 - ERROR : PermissionError: [WinError 5] Access is denied: 'C:\\Program Files\\Python\\Lib\\site-packages\\win32com\\gen_py\\00020813-0000-0000-C000-000000000046x0x1x6'

I suspect this is a trivial EXCEL permissions problem.... Any thoughts gratefully received.

Richard

Tony

  • Administrator
  • Hero Member
  • *****
  • Posts: 610
  • Karma: +14/-1
    • View Profile
Re: Message "PermissionError: 13 Access is denied" on simple menu example
« Reply #1 on: December 03, 2014, 10:02:43 AM »
Hi,

when win32com encounters a new typelib (like the Excel API) it generates a python file to allow you to call into it.

The problem you're having is that win32com is trying to create that file in the site-packages folder under you python install, which is read-only.

There are a couple of solutions you can try. First, you don't actually need to generate that file as you can use the plain dispatch object itself without creating that specific wrapper class, ie:

def xl_app2():
    xl_window=pyxll.get_active_object()
    xl_app2=win32com.client.Dispatch(xl_window).Application
    # win32com.client.gencache.EnsureDispatch(xl_app2)  --- not strictly necessary
    return xl_app2 # () --- no need to call xl_app2

The other option is to move where win32com writes these files. If you delete (or rename) C:\Program Files\Python\Lib\site-packages\win32com\gen_py then win32com will write its files to %TEMP% instead, where you will have permission to create files and so you shouldn't get this error.

It's unusual to have this gen_py folder included in a python distribution, and if it was there when you installed python it would seem to be an error in how the distribution was built.

Best regards,
Tony

jonesrl

  • Newbie
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Message "PermissionError: 13 Access is denied" on simple menu example
« Reply #2 on: December 04, 2014, 04:07:40 AM »
Thank you very much!!!! That works