簡體   English   中英

訪問谷歌表並通過 Python 更新它

[英]Accessing google sheet and update it via Python

我正在使用以下代碼使用 PostgreSQL 表中的數據更新我擁有的谷歌表。 表格經常刷新,我需要用表格的最新數據更新 Google 表格。

我是谷歌 API 的新手,瀏覽了護目鏡帖子,做了所有的步驟,比如與 client_email 共享谷歌表格,但它不起作用。

有3列,如下圖,

在此處輸入圖像描述

header 列位於第 3 行,我需要從第 4 行開始更新值。

以下是當前代碼,

import psycopg2
import gspread

from oauth2client.service_account import ServiceAccountCredentials
import pprint
#Create scope
scope =  ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']

cnx_psql = psycopg2.connect(host="xxx.xxx.xxx.xx", database="postgres", user="postgres",
                         password="**********", port="5432")
psql_cursor = cnx_psql.cursor()

meta_query = '''select * from dl.quantity;'''
psql_cursor.execute(meta_query)
results = psql_cursor.fetchall()
cell_values = (results)

creds = ServiceAccountCredentials.from_json_keyfile_name('/Users/User_123/Documents/GS/gsheet_key.json',scope)
client = gspread.authorize(creds)

sheet = client.open('https://docs.google.com/spreadsheets/d/***************').sheet1
pp = pprint.PrettyPrinter()

result = sheet.get_all_record()

for i, val in enumerate(cell_values):  
    cell_list[i].value = val  
    sheet.update_cells(cell_list) 

psql_cursor.close()

cnx_psql.close()

收到以下錯誤,

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/gspread/client.py", line 123, in open self.list_spreadsheet_files() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/gspread/utils.py", line 37, in finditem return next((item for item in seq if func(item))) StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Users/User_123/Documents/Googlesheet_update.py", line 30, in sheet = client.open('https://docs.google.com/spreadsheets/d/********************').sheet1 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/gspread/client.py", line 131, in open raise SpreadsheetNotFound gspread.exceptions.SpreadsheetNotFound

您的代碼和評論表明您正在嘗試使用完整的 URL 打開電子表格,但您使用的是僅適用於標題的open function。

文檔

您可以按其在 Google 文檔中顯示的標題打開電子表格:

 sh = gc.open('My poor gym results')

如果您想具體一點,請使用一個鍵(可以從電子表格的 url 中提取):

 sht1 = gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE')

或者,如果您真的懶得提取該密鑰,請粘貼整個電子表格的 url

 sht2 = gc.open_by_url('https://docs.google.com/spreadsheet/ccc?key=0Bm...FE&hl')

在您的情況下,最后一個示例是 go 的方式,因此請使用client.open_by_url而不是client.open

此代碼片段將允許您連接,從那里您可以查看文檔以完成您的操作的 rest!

 from oauth2client.service_account import ServiceAccountCredentials import gspread scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.file', 'https://www.googleapis.com/auth/spreadsheets'] #Generate a json file by using service account auth in google developer console ''' Link: https://console.developers.google.com/ 1) Enable API Access for a Project if you haven't done it yet. 2) Go to “APIs & Services > Credentials” and choose “Create credentials > Service account key”. 3) Fill out the form 4) Click “Create” and “Done”. 5) Press “Manage service accounts” above Service Accounts. 6) Press on ⋮ near recently created service account and select “Manage keys” and then click on “ADD KEY > Create new key”. 7) Select JSON key type and press “Create”. 8) Go to the google sheet and share the sheet with the email from service accounts. ''' creds = ServiceAccountCredentials.from_json_keyfile_name('mod.json', scope) client = gspread.authorize(creds) sheet = client.open_by_url("#Paste yout google sheet url here").sheet1 data = sheet.get_all_records() sheet.update_cell(1, 1, "You made it") #Write this message in first row and first column print(data)

暫無
暫無

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

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