This is my query:
import gspread
from google.oauth2.service_account import Credentials
import csv
# Path to your service account key file
SERVICE_ACCOUNT_FILE = '/Users/xxx/Desktop/projects/importcsv.json'
# Scopes for the API
SCOPES = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive']
# Authorize the client
credentials = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
client = gspread.authorize(credentials)
# Name of the Google Sheets document
spreadsheet_name = 'test csv import'
# Open or create the Google Sheets document
try:
spreadsheet = client.open(spreadsheet_name)
except gspread.exceptions.SpreadsheetNotFound:
spreadsheet = client.create(spreadsheet_name)
spreadsheet.share('xxxxx', perm_type='user', role='writer')
# Open the first sheet
sheet = spreadsheet.sheet1
# Clear the sheet before writing new data
sheet.clear()
# Path to your local CSV file
csv_file_path = '/Users/xxxxxx/Desktop/projects/test csv import - Sheet1.csv'
# Read the CSV file and upload to Google Sheets
with open(csv_file_path, mode='r') as file:
reader = csv.reader(file)
csv_data = list(reader)
# Convert the list of lists (csv_data) to the format expected by gspread (two-dimensional list)
values = [[cell] for row in csv_data for cell in row]
# Update the sheet with the CSV data starting from cell A1
sheet.update('A1', values)
print(f"CSV data has been uploaded to Google Sheets: {spreadsheet_name}")
The problem starts with line 40, “sheet.update(‘A1’, values)” where even tho the script seems to execute as I get the print “CSV data has been uploaded to Google Sheets” I get this error message
DeprecationWarning: The order of arguments in worksheet.update() has changed. Please pass values first and range_name secondor used named arguments (range_name=, values=)
sheet.update('A1', values)
I’ve worked out how the file is read by the function but no luck, also I’ve formatted as per requested by the error message, but doing so gives me another error where it says
TypeError: update() got an unexpected keyword argument ‘range’`
And by eliminating this and only leaving the values section, then it says
TypeError: update() got multiple values for argument ‘values’`
Unsure if my approach is wrong altogether at the very end of the script, I think the other bits of connecting to the googlesheets, credentials keys and accesses are all correct, which conformts me, but at the same time im annoyed this update bit is blocking me altogether.
Any suggestions would be appreciated
Thanks
1
Either swap the order of the arguments, changing
sheet.update('A1', values)
to
sheet.update(values, 'A1')
or use keywords:
sheet.update(range_name='A1', values=values)
Your second error is because you used range=
instead of range_name=
.
1
Your script for uploading a CSV file to Google Sheets looks mostly correct, but there’s an issue with how you are formatting the data to update the sheet. heres coreectd code.
Ravindu Yasanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1