1
votes

I uploaded .xlsx successfully from my local PC to google drive using this simple function. The mimetype I am using should convert .xlsx to normal google sheet

def writeToGDrive(filename):
    file_metadata = {'name': filename,'parents': [FOLDER_ID]}
    media = MediaFileUpload(filename,mimetype='application/vnd.openxmlformats- 
    officedocument.spreadsheetml.sheet', resumable=True)
    file = service.files().create(body=file_metadata,media_body=media,fields='id').execute()
    return file.get('id')

but when I am trying to read or edit

mySpread=service_sheet.spreadsheets().get(spreadsheetId=spreadSheet_Id).execute()

It gives me this error

returned "This operation is not supported for this document">

Also when tried to use gspread it gives me this error

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

I want to edit this .xlsx on google drive then export it as .xlsx again to local drive

1

1 Answers

0
votes

I believe your goal as follows.

  • You want to upload a XLSX file by converting as Google Spreadsheet.
  • You want to achieve this using googleapis for python.

Modification point:

  • In this case, how about including the mimeType of the Google Spreadsheet in the file metadata?

Modified script:

Please modify as follows and test it again.

file_metadata = {'name': filename,'parents': [FOLDER_ID]}
file_metadata = {'name': filename, 'parents': [FOLDER_ID], 'mimeType': 'application/vnd.google-apps.spreadsheet'}
  • By above modification, the returned file ID can be used as the Google Spreadsheet.

Note:

  • When you exported it as XLSX file, please use the method of "Files: export" as follows.

      request = service.files().export_media(fileId=fileId, mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
      fh = io.FileIO('exportedSample.xlsx', mode='wb')
      downloader = MediaIoBaseDownload(fh, request)
      done = False
      while done is False:
          status, done = downloader.next_chunk()
          print('Download %d%%.' % int(status.progress() * 100))
    

References: