Python Forum
updating Google spreadsheet with gspread
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
updating Google spreadsheet with gspread
#1
Hello,
I've been working on updating a spreadsheet in Google drive. I'm trying to use gspread to do this. I've been able to find the spreadsheet by it's ID and can clear the contents of the spreadsheet, but I'm having problems updating it with a local csv file I have been using to update for my test. on the last line, worksheet.batch_update(requests) generates this error: Python type error of 'request' object is not iterable.

I wonder if I am going about this the wrong way?
Thank you

This is the script. This script is called from a Visual Studio project.

from __future__ import print_function
from datetime import date, timedelta
import pickle
import os.path
import sys
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2 import service_account
import google.auth.transport.requests
import requests
import gspread
# gspread way ************
# authenticate to Google Sheets with a service account credentials json file
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
#SCOPES = ['https://www.googleapis.com/auth/spreadsheets']

SPREADSHEET_ID = '1dyyrjxx198sEJTOa9PA6tPGIINVi2PF8BGScuqDo093'

base = "C:/pyscrpts/"

#CREDS FILE FOR GSPREAD
gc = gspread.service_account(filename= 'c:\pyscrpts\creds.json')
csv_file_path = base + 'updates.csv'

def main():

sh = gc.open_by_key(SPREADSHEET_ID)

# get work book

worksheet = sh.sheet1
title = sh.title
#worksheet_list = sh.worksheets()
#for sheet in worksheet_list:
# print('sheetName: {}, sheetId(GID): {}'.format(sheet.title, sheet.id))
requests = google.auth.transport.requests.Request()

#clear an entire worksheet
worksheet.clear()
#Read csv and form request

with open(csv_file_path, 'r', encoding='UTF8') as csv_file:
csvContents = csv_file.read()

body = {
'requests': [{
'pasteData': {
"coordinate": {
"SheetId": SPREADSHEET_ID,
"rowIndex": "0",
"columnIndex": "0",
},
"data": csvContents,
"type": 'PASTE_NORMAL',
"delimiter": ',',
}
}]
}

worksheet.batch_update(requests)
Yoriz write Sep-30-2022, 11:44 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Returning Column and Row Data From Spreadsheet knight2000 0 449 Oct-22-2023, 07:07 AM
Last Post: knight2000
  Editing spreadsheet/csv BSDevo 6 1,042 Sep-01-2023, 05:47 PM
Last Post: BSDevo
  Looking to automate updating a spreadsheet with image from email cubangt 2 983 Feb-14-2023, 03:43 PM
Last Post: cubangt
  Import XML file directly into Excel spreadsheet demdej 0 859 Jan-24-2023, 02:48 PM
Last Post: demdej
  gspread and installing in general mgallotti 2 1,164 Sep-30-2022, 03:19 PM
Last Post: mgallotti
  Modify LibreOffice's ods spreadsheet Pavel_47 0 1,189 Jul-13-2022, 11:28 AM
Last Post: Pavel_47
  Python create a spreadsheet with column and row header ouruslife 4 1,647 Jul-09-2022, 11:01 AM
Last Post: Pedroski55
  gspread - applying ValueRenderOption to a range of cells dwassner 0 1,704 Jan-12-2022, 03:05 PM
Last Post: dwassner
  Searching for URLs and printing the corresponding row from an Excel spreadsheet johnbernard 0 1,285 Aug-20-2021, 06:43 PM
Last Post: johnbernard
  "Switch-to-spreadsheet" entry. Feasible in Python? whatspython 2 2,029 Sep-30-2020, 01:12 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020