weixin_39558221
weixin_39558221
2020-12-02 08:03

AttributeError: Unknown property, element or command: 'run_VB_macro'

Getting the following bug when trying to run a python file through an excel macro, but have Excel installed and working.


Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/Guest/Desktop/BulkTool_Installer/BulkTool/bulktool.py", line 210, in bulk_upload
    wb = Workbook()
  File "/Users/Guest/Envs/virt1/lib/python2.7/site-packages/xlwings/main.py", line 63, in __init__
    self.xl_app, self.xl_workbook = xlplatform.get_workbook(self.fullname)
  File "/Users/Guest/Envs/virt1/lib/python2.7/site-packages/xlwings/_xlmac.py", line 58, in get_workbook
    xl_workbook = app('Microsoft Excel').workbooks[filename]
  File "/Users/Guest/Envs/virt1/lib/python2.7/site-packages/aeosa/appscript/reference.py", line 579, in __getattr__
    raise AttributeError("Unknown property, element or command: %r" % name)
AttributeError: Unknown property, element or command: 'workbooks'
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/Users/Guest/Envs/virt1/lib/python2.7/site-packages/xlwings/_xlmac.py", line 29, in clean_up
    app('Microsoft Excel').run_VB_macro('CleanUp')
  File "/Users/Guest/Envs/virt1/lib/python2.7/site-packages/aeosa/appscript/reference.py", line 579, in __getattr__
    raise AttributeError("Unknown property, element or command: %r" % name)
AttributeError: Unknown property, element or command: 'run_VB_macro'
Error in sys.exitfunc:
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/Users/Guest/Envs/virt1/lib/python2.7/site-packages/xlwings/_xlmac.py", line 29, in clean_up
    app('Microsoft Excel').run_VB_macro('CleanUp')
  File "/Users/Guest/Envs/virt1/lib/python2.7/site-packages/aeosa/appscript/reference.py", line 579, in __getattr__
    raise AttributeError("Unknown property, element or command: %r" % name)
AttributeError: Unknown property, element or command: 'run_VB_macro'
</module></string>

该提问来源于开源项目:xlwings/xlwings

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

10条回答

  • weixin_39922147 weixin_39922147 5月前

    Does it work when you run things from Python directly?

    点赞 评论 复制链接分享
  • weixin_39922147 weixin_39922147 5月前

    Hi - did you find the reason for this?

    点赞 评论 复制链接分享
  • weixin_39922147 weixin_39922147 5月前

    Hi , somebody on StackOverflow had the same issue - can you please let me know if and how you resolved this? thanks!

    点赞 评论 复制链接分享
  • weixin_39793564 weixin_39793564 5月前

    Hi , I'm experiencing the same problem. Has it been solved yet?

    I'm on a Mac with OS X 10.10.2, Python 3.4, xlwings 0.3.4 (installed via pip3 for Python 3) and both Excel 2016 Preview (15.8.0) and Excel 2011 (14.2.4) installed. Excel 2016 opens with a blank worksheet, but nothing else happens.

    My code (from IPython, but same happens on regular Python3 console):

    Input

     python
    from xlwings import Workbook, Sheet, Range, Chart 
    wb = Workbook()
    

    Output

     python
    ---------------------------------------------------------------------------
    KeyError                                  Traceback (most recent call last)
    /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/aeosa/appscript/reference.py in __getattr__(self, name)
        574                 try:
    --> 575                         selectortype, code = self.AS_appdata.referencebyname()[name]
        576                 except KeyError as e:
    
    KeyError: 'workbooks'
    
    The above exception was the direct cause of the following exception:
    
    AttributeError                            Traceback (most recent call last)
    <ipython-input-4-4a3c36eb9bf9> in <module>()
    ----> 1 wb = Workbook()
    
    /usr/local/lib/python3.4/site-packages/xlwings/main.py in __init__(self, fullname, xl_workbook, app_visible)
        139         else:
        140             # Open Excel if necessary and create a new workbook
    --> 141             self.xl_app, self.xl_workbook = xlplatform.new_workbook()
        142 
        143         self.name = xlplatform.get_workbook_name(self.xl_workbook)
    
    /usr/local/lib/python3.4/site-packages/xlwings/_xlmac.py in new_workbook()
        117         xl_workbook = xl_app.make(new=kw.workbook)
        118     else:
    --> 119         xl_workbook = xl_app.workbooks[1]
        120 
        121     return xl_app, xl_workbook
    
    /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/aeosa/appscript/reference.py in __getattr__(self, name)
        575                         selectortype, code = self.AS_appdata.referencebyname()[name]
        576                 except KeyError as e:
    --> 577                         raise AttributeError("Unknown property, element or command: {!r}".format(name)) from e
        578                 if selectortype == kProperty:
        579                         return Reference(self.AS_appdata, self.AS_aemreference.property(code))
    
    AttributeError: Unknown property, element or command: 'workbooks'
    </module></ipython-input-4-4a3c36eb9bf9>
    点赞 评论 复制链接分享
  • weixin_39922147 weixin_39922147 5月前

    Hi - this is a slightly different error msg than the original issue (run_VB_macro vs. workbooks). What happens if you open a blank Workbook manually and try to connect doing wb = Workbook('Workbook1') (or whatever the name of the new Workbook)? Thanks for the Office 2016 hint - wasn't aware of it already being out!

    点赞 评论 复制链接分享
  • weixin_39793564 weixin_39793564 5月前

    When using wb = Workbook('Workbook1'), Excel shows an error: "'/WORKBOOK1' could not be found."

    Python shows the following error:

     python
    MacOSError                                Traceback (most recent call last)
    /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/aeosa/aem/aemsend.py in send(self, timeout, flags)
         73                 try:
    ---> 74                         replyevent = self._sendproc(self.AEM_event, flags, timeout)
         75                 except MacOSError as err: # an OS-level error occurred
    
    /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/aeosa/aem/aemsend.py in sendappleevent(evt, flags, timeout)
         22     """
    ---> 23         return evt.send(flags, timeout)
         24 
    
    MacOSError: -1712
    
    The above exception was the direct cause of the following exception:
    
    EventError                                Traceback (most recent call last)
    /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/aeosa/appscript/reference.py in __call__(self, *args, **kargs)
        459                 try:
    --> 460                         return self.AS_appdata.target().event(self._code, params, atts, codecs=self.AS_appdata).send(timeout, sendflags)
        461                 except aem.EventError as e:
    
    /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/aeosa/aem/aemsend.py in send(self, timeout, flags)
         76                         if not (self._eventcode == b'aevtquit' and err.args[0] == -609): # Ignore invalid connection error (-609) when quitting
    ---> 77                                 raise EventError(err.args[0]) from err
         78                 else: # decode application's reply, if any
    
    EventError: Command failed: Apple event timed out. (-1712)
    
    The above exception was the direct cause of the following exception:
    
    CommandError                              Traceback (most recent call last)
    <ipython-input-5-9df8b35f8ddc> in <module>()
    ----> 1 wb = Workbook("Workbook1")
    
    /usr/local/lib/python3.4/site-packages/xlwings/main.py in __init__(self, fullname, xl_workbook, app_visible)
        136             else:
        137                 # Open Excel and the Workbook
    --> 138                 self.xl_app, self.xl_workbook = xlplatform.open_workbook(self.fullname)
        139         else:
        140             # Open Excel if necessary and create a new workbook
    
    /usr/local/lib/python3.4/site-packages/xlwings/_xlmac.py in open_workbook(fullname)
         97     filename = os.path.basename(fullname)
         98     xl_app = app('Microsoft Excel')
    ---> 99     xl_app.open(fullname)
        100     xl_workbook = xl_app.workbooks[filename]
        101     return xl_app, xl_workbook
    
    /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/aeosa/appscript/reference.py in __call__(self, *args, **kargs)
        494                                                 return
        495                                         raise CommandError(self, (args, kargs), e, self.AS_appdata) from e
    --> 496                         raise CommandError(self, (args, kargs), e, self.AS_appdata) from e
        497 
        498         def AS_formatcommand(self, args):
    
    CommandError: Command failed:
            OSERROR: -1712
            MESSAGE: Apple event timed out.
            COMMAND: app('/Applications/Microsoft Excel.app').open('Workbook1')
    </module></ipython-input-5-9df8b35f8ddc>

    Saving that Workbook in the same folder as the IPython file and then using wb = Workbook('Workbook1.xlsm') has the same result.

    Providing the full path with `wb = Workbook('/Users/fullpath/Workbook1.xlsm')`` returns the original error message:

     python
    ---------------------------------------------------------------------------
    KeyError                                  Traceback (most recent call last)
    /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/aeosa/appscript/reference.py in __getattr__(self, name)
        574                 try:
    --> 575                         selectortype, code = self.AS_appdata.referencebyname()[name]
        576                 except KeyError as e:
    
    KeyError: 'workbooks'
    
    The above exception was the direct cause of the following exception:
    
    AttributeError                            Traceback (most recent call last)
    <ipython-input-7-64e983d83a92> in <module>()
    ----> 1 wb = Workbook("/Users/fullpath/Workbook1.xlsm")
    
    /usr/local/lib/python3.4/site-packages/xlwings/main.py in __init__(self, fullname, xl_workbook, app_visible)
        133             if xlplatform.is_file_open(self.fullname):
        134                 # Connect to an open Workbook
    --> 135                 self.xl_app, self.xl_workbook = xlplatform.get_workbook(self.fullname)
        136             else:
        137                 # Open Excel and the Workbook
    
    /usr/local/lib/python3.4/site-packages/xlwings/_xlmac.py in get_workbook(fullname)
         64     """
         65     filename = os.path.basename(fullname)
    ---> 66     xl_workbook = app('Microsoft Excel').workbooks[filename]
         67     xl_app = app('Microsoft Excel')
         68     return xl_app, xl_workbook
    
    /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/aeosa/appscript/reference.py in __getattr__(self, name)
        575                         selectortype, code = self.AS_appdata.referencebyname()[name]
        576                 except KeyError as e:
    --> 577                         raise AttributeError("Unknown property, element or command: {!r}".format(name)) from e
        578                 if selectortype == kProperty:
        579                         return Reference(self.AS_appdata, self.AS_aemreference.property(code))
    
    AttributeError: Unknown property, element or command: 'workbooks'
    </module></ipython-input-7-64e983d83a92>

    I suspect this is because of Office 2016. Any experience with dual Excel installations and forcing xlwings to use a certain one?

    PS: I'll most likely be at the xlwings meetup in Berlin tonight, could have a look at the problem there as well if you like.

    点赞 评论 复制链接分享
  • weixin_39922147 weixin_39922147 5月前

    Right, this will probably be the issue as I've never tested this. I've opened a new issue for this with a possible hack for now: https://github.com/ZoomerAnalytics/xlwings/issues/169

    点赞 评论 复制链接分享
  • weixin_39554891 weixin_39554891 5月前

    Hi, I caught the original issue ('run_VB_macro') with the exact same stack trace. I'm using: - xlwings 0.3.4 - Excel for Mac 2016 15.8.1 (FYI: I removed Excel 2011 before install this version) - Python 2.7.6 - OS X 10.10

    I was running the fibonacci example directly from CLI

    点赞 评论 复制链接分享
  • weixin_39554891 weixin_39554891 5月前

    It seems Office 2016 stopped working with appscript

    点赞 评论 复制链接分享
  • weixin_39922147 weixin_39922147 5月前

    See issue #170 for Office 2016: if you need something now, work with the excel2016 branch. Please add any further comments regarding Office 2016 directly to #170

    点赞 评论 复制链接分享

相关推荐