forked from driscollis/automating_excel_with_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_gsheet.py
37 lines (30 loc) · 1.08 KB
/
read_gsheet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# read_gsheet.py
import gspread
from oauth2client.service_account import ServiceAccountCredentials
def authenticate(credentials):
scope = [
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive",
]
creds = ServiceAccountCredentials.from_json_keyfile_name(credentials, scope)
client = gspread.authorize(creds)
return client
def main():
client = authenticate("pyspread.json")
try:
workbook = client.open("test")
print("Test Sheet opened")
except:
print("Error opening test spreadsheet")
return
sheet = workbook.sheet1
print("Worksheets: " + str(workbook.worksheets()))
print(f"All values on row 1: {sheet.row_values(1)}")
print(f"All values in column 1: {sheet.col_values(1)}")
print(f"All values in worksheet: {sheet.get_all_values()}")
# Get all values from worksheet as a list of dictionaries
values = sheet.get_all_records()
print(f"All records: {values=}")
if __name__ == "__main__":
main()