首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从excel中通过xlwing使用Gspread启动Python函数

从excel中通过xlwing使用Gspread启动Python函数
EN

Stack Overflow用户
提问于 2017-06-06 22:44:48
回答 1查看 202关注 0票数 0

我正在尝试通过xlwing使用excel中的Gspread来启动python函数。对于上下文,我这样做是为了能够将数据从google工作表‘数据库’导入到我的硬拷贝excel文件中。当我从Python控制台启动它时,这个函数运行得很好,但当我使用RunPython通过一个宏从Excel启动它时,它似乎就不起作用了。

更准确地说,我添加了一些标志来查看问题出在哪里:

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文件位于相同的工作目录中。如果你有任何想法的问题可能是什么,我可以如何解决它,这将是伟大的!

谢谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-20 05:19:06

正如@Felix Zumstein所指定的,解决方案是指定'client_secret‘json文件的完整路径,可能使用os.path.dirname(os.path.abspath( file ))

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44393129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档