Converting Excel XLSX files to Google Sheets format - getting API error

I’m having trouble importing an Excel file (.xlsx format) into Google Sheets using Python. When I try to access the spreadsheet through the API, I keep getting an error message.

Here’s the code I’m using:

import gspread
from google.auth import default
from oauth2client.client import GoogleCredentials
import pandas as pd

authentication, _ = default()
client = gspread.authorize(authentication)

spreadsheet = client.open_by_url('https://docs.google.com/spreadsheets/d/1AbCdEfGhIjKlMnOpQrStUvWxYz/edit#gid=123456789')
worksheet = spreadsheet.get_worksheet(0)
data_values = worksheet.get_all_values()

The error I’m getting is:

APIError: {'code': 400, 'message': 'This operation is not supported for this document', 'status': 'FAILED_PRECONDITION'}

I thought I could just read the Excel file like any other spreadsheet but it’s not working. Does anyone know the right way to handle this conversion? Maybe I need to upload the file differently first?

yeah, you’re right! can’t just pull xlsx files directly. best way is to upload it to google sheets and use that link. another option is to read it locally with pandas and then upload the dataframe to a new sheet using set_dataframe(). hope that helps!

You can’t directly access Excel files through the Google Sheets API - they need to be in Google Sheets format first. I hit this same issue last year during a data migration project. Upload your Excel file to Google Drive and set the convert parameter to true. This automatically converts your .xlsx file to Google Sheets format, then you can use gspread normally. Or read the Excel file locally with pandas first, create a new Google Sheet, and populate it with that data. This gives you more control and handles any formatting issues that pop up during automatic conversion.

Google Sheets API can’t read Excel files directly - that’s why you’re getting the error. I hit this same issue and solved it by using Google Drive API first. Upload your xlsx file with supportsAllDrives=True and convert=True parameters. Once it converts, you’ll get a new file ID for the Google Sheets version. Use that ID with gspread instead of the original Excel URL. Just heads up - conversion keeps most formatting but sometimes breaks complex formulas or pivot tables, so check your data afterward.