I’m trying to figure out how to convert an Excel file, such as document.xlsx, into Google Sheets format for editing with the gspread library.
When I attempt to access the sheet like a typical Google Sheet, it returns an error. Here’s my code snippet:
import gspread
from google.auth import default
creds, _ = default()
from oauth2client.client import GoogleCredentials
import pandas as pd
gc = gspread.authorize(creds)
workbook = gc.open_by_url('https://docs.google.com/spreadsheets/d/19vWu-OSjAKSvfh8FgvZClk4aGsGxx5ej/edit#gid=198588977')
sheet = workbook.get_worksheet(0)
list_of_values = sheet.get_all_values()
I encountered this error:
APIError: {'code': 400, 'message': 'This operation is not supported for this document', 'status': 'FAILED_PRECONDITION'}
What steps should I take to properly convert this file? Should I upload the Excel document beforehand, or is there a different method I should consider?
I hit this exact problem with financial reports. Google Drive and Sheets are separate services - your file’s in Drive but isn’t in the format gspread needs. After uploading your xlsx to Drive, you’ve got to convert it. Open the file and go File > Save as Google Sheets. This creates a new doc with a different URL that actually works with the Sheets API. Your original xlsx stays untouched, but now you’ve got a proper Google Sheets file that gspread can read. Don’t forget to update your code with the new URL. Most formatting and formulas carry over, though some Excel-specific stuff might not transfer perfectly.
totally get it! had the same prob couple days ago. that error means your file’s still in xlsx format. just upload it to Drive, right-click and open with Google Sheets, then use that new link for gspread. it worked like a charm for me!
You’re trying to access an Excel file through Google Sheets API, but it won’t work without converting it first. That URL points to an xlsx file on Google Drive - not a Google Sheets document. I’ve hit this same issue before. You need to convert the Excel file to Google Sheets format. Easiest way: upload your xlsx to Google Drive, right-click it, and choose ‘Open with > Google Sheets’. This creates a new Google Sheets version and keeps your original Excel file. Or you can use the Drive API to convert it programmatically by copying with the right MIME type (application/vnd.google-apps.spreadsheet). Once it’s converted, you’ll get a proper Google Sheets URL that works with gspread. The problem is Google Sheets has a different structure than Excel files, even when they’re stored on Google Drive. Your current URL is pointing to a binary Excel file, which the Sheets API can’t process.