You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

macOS Sonoma下xlwings调用活动工作簿报错UnboundLocalError求助

xlwings调用SharePoint同步的OneDrive工作簿报错UnboundLocalError

环境信息

  • 系统:macOS Sonoma 14.5
  • xlwings版本:v0.31.10
  • 文件位置:Excel文件存储于SharePoint,已同步至本地OneDrive文件夹
  • 已配置环境变量(在~/.bash_profile中):
    • ONEDRIVE_CONSUMER_MAC=/Users/John/OneDrive - MyCompany
    • ONEDRIVE_COMMERCIAL_MAC=/Users/John/OneDrive - MyCompany 2

错误提示

运行.py文件时持续出现以下错误:

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

测试代码

尝试了以下简单代码但无法正常运行:

import xlwings as xw

def getWB():
    try:
        wb =xw.Book.caller() #never works
    except:
        wb = xw.Book("/Users/John/Library/CloudStorage/OneDrive-MyCompany/Project XYZ/2. Excel/4. Master/Name of Excel file.xlsm").set_mock_caller()
        wb =xw.Book.caller()
    return wb

def main():
    wb = getWB()
    sheet = wb.sheets[0]
    if sheet["E4"].value == "Hello xlwings!":
        sheet["E4"].value = "Bye xlwings!"
    else:
        sheet["E4"].value = "Hello xlwings!"


if __name__ == "__main__":
    app = xw.App()
    main()

目前无法打开或调用当前活动工作簿,恳请提供解决帮助。

内容的提问来源于stack exchange,提问作者cocos2dbeginner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 17:42:43