[how] Connect Colab To Google Sheet

Open Google Sheet By Name

# 1. Authorizing google colab
from google.colab import auth
auth.authenticate_user()

# 2. credentials for google sheets
import gspread
from google.auth import default
creds, _ = default()

# 3. authotizing the connection
gc = gspread.authorize(creds)

#4. Connecting 
worksheet = gc.open('gsht-sample').sheet1

#5. Exporting data to get_all_values gives a list of rows.
rows = worksheet.get_all_values()

# 6. Using pandas to convert to a DataFrame and render.
import pandas as pd
df = pd.DataFrame.from_records(rows)
#creating columns name
df.columns = df.iloc[0]
df = df.iloc[1:]
df

Open Google Sheet By ID

# 1. Authorizing
from google.colab import auth
auth.authenticate_user()

# 2. Credentials
import gspread
from google.auth import default
creds, _ = default()

# 3. Authorizing Credentials
gc = gspread.authorize(creds)

# 4. Importing Google Sheet using google sheet key
gsht= gc.open_by_key('1Nm1B8zl9j2w-kAo8-_PmUKNdwuBRj1kPbv-G4QJdueU')

# 5. Bring the tab you want to update 
wsht = gsht.worksheet('Sheet1')
Update Cell
cell_list=wsht.range('E1:E2')

for cell in cell_list:
  cell.value="test"

wsht.update_cells(cell_list)

cell_list

Post a Comment

0 Comments