[Python] UnboundLocalError when trying to access a sharepoint file using xw.Book

Discussão em 'Python' iniciado por Stack, Maio 12, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to access an excel which is stored in sharepoint. I am opening that particular file runs_excel after closing all excel instances from Task Manager. This used to work since the last six months I tried to do this way but now I am getting UnboundLocalError: cannot access local variable 'root' where it is not associated with a value

    import xlwings as xw
    wb = xw.Book('runs_excel.xlsm')
    sht = wb.sheets['macro']
    wb.sheets['macro'].select()


    UnboundLocalError Traceback (most recent call last) Cell In[5], line 2
    1 #os.chdir(filepath_main)
    ----> 2 wb = xw.Book('ecp_runs_excel_api.xlsm')
    3 sht = wb.sheets['macro']
    4 wb.sheets['macro'].select()

    File C:\Program Files (x86)\temp\python\env_local\Lib\site-packages\xlwings\main.py:924, in Book.__init__(self, fullname, update_links, read_only, format, password, write_res_password, ignore_read_only_recommended, origin, delimiter, editable, notify, converter, add_to_mru, local, corrupt_load, impl, json, mode, engine, **kwargs)
    919 candidates.append((app, wb))
    920 # Comparing by name first saves us from having to compare the
    921 # fullname for non-candidates, which can get around issues in
    922 # case the fullname is a problematic URL (GH 1946)
    923 elif wb.name.lower() == os.path.split(fullname)[1].lower() and (
    --> 924 wb.fullname.lower() == fullname.lower()
    925 or wb.name.lower() == fullname.lower()
    926 ):
    927 candidates.append((app, wb))
    929 app = apps.active

    File C:\Program Files (x86)\temp\python\env_local\Lib\site-packages\xlwings\main.py:1178, in Book.fullname(self) 1171 @property 1172 def fullname(self): 1173 """ 1174 Returns the name of the object, including its path on disk, as a string. 1175 Read-only String. 1176 1177 """
    -> 1178 return self.impl.fullname

    File C:\Program Files (x86)\temp\python\env_local\Lib\site-packages\xlwings\_xlwindows.py:903, in Book.fullname(self)
    901 if "://" in self.xl.FullName:
    902 config = read_config_sheet(xlwings.Book(impl=self))
    --> 903 return fullname_url_to_local_path(
    904 url=self.xl.FullName,
    905 sheet_onedrive_consumer_config=config.get("ONEDRIVE_CONSUMER_WIN"),
    906 sheet_onedrive_commercial_config=config.get("ONEDRIVE_COMMERCIAL_WIN"),
    907 sheet_sharepoint_config=config.get("SHAREPOINT_WIN"),
    908 )
    909 else:
    910 return self.xl.FullName

    File C:\Program Files (x86)\temp\python\env_local\Lib\site-packages\xlwings\utils.py:626, in fullname_url_to_local_path(url, sheet_onedrive_consumer_config, sheet_onedrive_commercial_config, sheet_sharepoint_config)
    622 return str(local_path)
    623 # Horrible fallback
    624 return search_local_sharepoint_path(
    625 url,
    --> 626 root if not mount_point else mount_point,
    627 sharepoint_config,
    628 sharepoint_config_name,
    629 )
    630 raise xlwings.XlwingsError(
    631 f"URL {url} not recognized as valid OneDrive/SharePoint link."
    632 )

    UnboundLocalError: cannot access local variable 'root' where it is not associated with a value

    Continue reading...

Compartilhe esta Página