Skip to content Skip to sidebar Skip to footer

Is There Any Way To Import A .xlsx Saved On The Gdrive To A Gsheet With Python?

Unfortunately, I don't have any code to post here. I tried some things but none of them really work. I want to automatically import an excel (that gets refreshed every 2 hours) to

Solution 1:

You cannot directly import a .xlsx into Google Sheets

But you can:

  1. Convert the .xlsx to a Google Spreadsheet

    • If the file is already on your drive, the easiest way to convert it would be with the Drive v2 method Files: Copy specifying converttrue.

Sample:

service.files().copy(fileId=file_id,convert=true, body={"title": "MyNewGoogleSheet"}).execute()
  • If the excel file is on your local disc, the documentation provides a sample how to import it to a Google type in Python, you just need to change the mimeType from 'text/csv' to application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.

    1. Copy-paste a sheet / range from the converted spreadsheet into your already existing spreadsheet.
  • Use for it a method of the Sheets API, like e.g. spreadsheets.sheets.copyTo

Sample:

file_metadata = {
    'name': 'My Report',
    'mimeType': 'application/vnd.google-apps.spreadsheet'
}
media = MediaFileUpload('files/report.csv',
                        mimetype='text/csv',
                        resumable=True)
file = drive_service.files().create(body=file_metadata,
                                    media_body=media,
                                    fields='id').execute()
print'File ID: %s' % file.get('id')

Post a Comment for "Is There Any Way To Import A .xlsx Saved On The Gdrive To A Gsheet With Python?"