2017-07-18 59 views
0

我使用Python 3運行Pycharm和模塊gspread來讀取我的谷歌驅動器中的谷歌工作表文件。那麼,我已經遵循了這個link關於如何讀取文件的所有步驟。不幸的是,我在下面的代碼還沒有工作。gspread閱讀谷歌工作表文件使用Python 3

import gspread 
from oauth2client.service_account import ServiceAccountCredentials 


scope =['https://docs.google.com/spreadsheets/d/1xnaOZMd2v93tY28h_hsuMnZYXC9YqCfFpQX70lwpN94/edit?usp=sharing'] 
credentials = ServiceAccountCredentials.from_json_keyfile_name('distech-c1e26e7150b2.json',scope) 
gc = gspread.authorize(credentials) 

wks = gc.open("POC").sheet1 

for temp in wks: 
    print(temp) 

我怎樣才能使用這個模塊閱讀谷歌表格文件球員?非常感謝

回答

1

經過深入研究,我明白了兩件事。

  1. 在我的代碼範圍是錯誤的原因的範圍僅僅是一個由谷歌提供的API授予在電子表格正確的訪問權限。

    的權利範圍對我來說是:scope =['https://spreadsheets.google.com/feeds']

  2. 它只是打開,將我的檔案內返回工作表中的電子表格中的開門紅。

所以感謝解決@Pedro洛比託在他的崗位here

解決方案:

我有隻是一對夫婦對我的帳戶的電子表格的同一個問題,這個問題被解決:

  1. 打開JSON密鑰文件(項目名稱,ca997255dada.json)

  2. 找到client_email的值,即:採用t client_email": "[email protected]

  3. 分享您的板材(S)帽子電子郵件

現在我的代碼如下所示:

import gspread 
from oauth2client.service_account import ServiceAccountCredentials 


scope =['https://spreadsheets.google.com/feeds'] 
credentials = ServiceAccountCredentials.from_json_keyfile_name('xxx.json',scope) 
gc = gspread.authorize(credentials) 

spreadsheet = gc.open("POC") 
wks = spreadsheet.worksheet('test1') 
wks2 = spreadsheet.worksheet('test2') 
out = list() 
out = wks.col_values(1) 
for temp in out: 
    print(out)