簡體   English   中英

Python:閱讀Google電子表格

[英]Python: Reading a google Spreadsheet

我對Python的經驗不是很豐富,所以請原諒我可能的愚蠢行為。 我想通過python閱讀Google電子表格。 我不是電子表格的所有者,但所有者與我共享了該電子表格(只讀,不寫)。

這就是我現在所擁有的:

import json
import gspread
from oauth2client.client import SignedJwtAssertionCredentials

json_key = json.load(open('MyProject-ab2f3b9e6d60.json'))
scope = ['https://spreadsheets.google.com/feeds']
credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)
client = gspread.authorize(credentials)
client.login()
client.open_by_key('11Z_6NMLa0e3QDNozj6kExij8TG6bWRsxPT_ZppV13w')

不幸的是,我總是得到:“ gspread.exceptions.SpreadsheetNotFound”

因此,登錄/授權似乎運行良好,但gspread某種程度上無法找到電子表格。 它不適用於open_by_url或打開其中任何一個。

因此,是否有可能閱讀我不擁有的Google電子表格?

我正在使用python 3.5

如果您的登錄按預期運行,則問題可能是電子表格密鑰:

為此,您可以將GET請求發送到以下鏈接:

https://spreadsheets.google.com/feeds/spreadsheets/private/full

這將返回以JSOn或XML(無論您選擇哪種)的所有電子表格的列表,然后查找包含所需電子表格的整個URL的id字段,最后驗證您使用的密鑰是否相同作為請求返回的內容。

在JSON中:

entry: {
    id: https://spreadsheets.google.com/feeds/spreadsheets/(this-is-where-the-key-will-be)
    ...
}

在XML中:

<entry gd:etag=... >
    <id> https://spreadsheets.google.com/feeds/spreadsheets/(this-is-where-the-key-will-be) </id>
    ...
</entry>

這是Sheets API的鏈接

請注意,您不必與自己的帳戶共享文檔,而應與json_key['client_email']帳戶(這是很長且很奇怪的電子郵件地址)共享文檔。 可能是個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM