通过 xlwings 使用来自 excel 的 Gspread 启动 Python 函数
Launch Python function using Gspread from excel via xlwings
我正在尝试通过 xlwings 使用来自 excel 的 Gspread 启动一个 python 函数。对于上下文,我这样做是为了能够将数据从 google sheet 'database' 导入到我的硬拷贝 excel 文件中。当我从 Python 控制台启动它时该功能完美运行,但当我使用 运行Python.[= 通过宏从 Excel 启动它时似乎不起作用14=]
更准确地说,我添加了标志以查看问题出在哪里:
def main():
wb = xw.Book.caller()
wb.sheets['Dashboard'].range('J3').value=0#used as a flag
import gspread
from oauth2client.service_account import ServiceAccountCredentials
wb.sheets['Dashboard'].range('J3').value=1.5#used as a flag
# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds']
wb.sheets['Dashboard'].range('J3').value=1.6#used as a flag
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
wb.sheets['Dashboard'].range('J3').value=1.7#used as a flag
client = gspread.authorize(creds)
wb.sheets['Dashboard'].range('J3').value=1#used as a flag
尝试 运行 程序后我在 J3 单元格中得到的值为 1.6,这意味着导致问题的行是:
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
文件 client_secret.json 与 python 脚本和 excel 文件位于同一工作目录中。如果您对可能出现的问题以及我如何解决它有任何想法,那就太好了!
感谢您的帮助!
如@Felix Zumstein 所指定,解决方案是指定 'client_secret' json 文件的完整路径,可能使用 os.path.dirname(os.path.abspath(file) )
我正在尝试通过 xlwings 使用来自 excel 的 Gspread 启动一个 python 函数。对于上下文,我这样做是为了能够将数据从 google sheet 'database' 导入到我的硬拷贝 excel 文件中。当我从 Python 控制台启动它时该功能完美运行,但当我使用 运行Python.[= 通过宏从 Excel 启动它时似乎不起作用14=]
更准确地说,我添加了标志以查看问题出在哪里:
def main():
wb = xw.Book.caller()
wb.sheets['Dashboard'].range('J3').value=0#used as a flag
import gspread
from oauth2client.service_account import ServiceAccountCredentials
wb.sheets['Dashboard'].range('J3').value=1.5#used as a flag
# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds']
wb.sheets['Dashboard'].range('J3').value=1.6#used as a flag
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
wb.sheets['Dashboard'].range('J3').value=1.7#used as a flag
client = gspread.authorize(creds)
wb.sheets['Dashboard'].range('J3').value=1#used as a flag
尝试 运行 程序后我在 J3 单元格中得到的值为 1.6,这意味着导致问题的行是:
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
文件 client_secret.json 与 python 脚本和 excel 文件位于同一工作目录中。如果您对可能出现的问题以及我如何解决它有任何想法,那就太好了!
感谢您的帮助!
如@Felix Zumstein 所指定,解决方案是指定 'client_secret' json 文件的完整路径,可能使用 os.path.dirname(os.path.abspath(file) )