Skip to content

Instantly share code, notes, and snippets.

@prafuld3
Last active November 6, 2023 15:32
Show Gist options
  • Save prafuld3/920b33f036aef145c3135fb13baba80d to your computer and use it in GitHub Desktop.
Save prafuld3/920b33f036aef145c3135fb13baba80d to your computer and use it in GitHub Desktop.
Reading google sheet using python
import pandas as pd
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow,Flow
from google.auth.transport.requests import Request
import os
import pickle
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
# here enter the id of your google sheet
SAMPLE_SPREADSHEET_ID_input = '1cvZswLiDo3LfhnA7RcS8vFqacx73RGor-OZ_FtvyLE8'
SAMPLE_RANGE_NAME = 'A1:AA1000'
def main():
global values_input, service
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'my_json_file.json', SCOPES) # here enter the name of your downloaded JSON file
creds = flow.run_local_server(port=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
result_input = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID_input,
range=SAMPLE_RANGE_NAME).execute()
values_input = result_input.get('values', [])
if not values_input and not values_expansion:
print('No data found.')
main()
df=pd.DataFrame(values_input[1:], columns=values_input[0])
@flytrotter
Copy link

what does a:aa100 mean?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment