https://developers.google.com/people/quickstart/python
轉到上面的鏈接,並創建按照您的谷歌帳戶給出的步驟的項目。
從上面的鏈接你將下載OAuth客戶端ID這將給你client_secret.json。
在此示例中未使用OAuth客戶端ID,並且在使用其他API時將會需要。
在addtion以上,你必須下載服務帳戶的關鍵將低於OAuth用戶端ID。將它下載爲json。
服務帳戶密鑰的路徑必須在以下代碼的第四行中提及。
代碼:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
#scope is necessary if we are accesing gdrive
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('/home/username/Downloads/MyProject.json', scope)
gc = gspread.authorize(credentials)
#to create a new gsheet
sh = gc.create('Sheet_1')
#to open existing gsheet
sh = gc.open('LYKE')
# to share to other users
sh.share('[email protected]', perm_type='user', role='writer')
#to create a worksheet in the gsheet
worksheet = sh.add_worksheet(title='title', rows="500", cols="50")
#Define a cell range and update cells
cell_list = worksheet.range('A1:A10')
for i,cell in enumerate(cell_list):
cell.value=i
worksheet.update_cells(cell_list)
請澄清「訪問谷歌片,並嘗試使用python和硒來編輯它在自動測試」 – nithin