2

Google drive api does provide some samples to create and update spreadsheets.

However, since we have different csvs and there will be more we'll be designing. what I'm looking for is a generic solution which will take the url of the csv stored in the blobstore and save those files on Google drive.

Kara
  • 6,115
  • 16
  • 50
  • 57

2 Answers2

2

My solution is like this, took help of drEdit samples:

  def get_data(self):
    blob_reader = blobstore.BlobReader(self.__blob_key__)
    data = blob_reader.read()
    fh = io.BytesIO(data)
    return fh

  def save_file(self, dest_filename, description, mime_type):
    service = self.create_drive()
    resource = {
      'title': dest_filename,
      'description': description,
      'mimeType': mime_type,
    }
    try:
      file_data = self.get_file_data()
      resource = service.files().insert(
          body=resource,
          media_body=MediaIoBaseUpload(
              file_data,
              mime_type,
              resumable=False)
      ).execute()
    except AccessTokenRefreshError:
      pass

calling save_file:

  drive.save_file('testing_drive', 'testing....', 'text/csv')

or

drive.save_file('testing_drive.csv', 'testing....', 'text/csv')

However, there one small issue with this code. Google drive API doesn't convert this file to spreadsheet while uploading hence preview doesn't work. To view the file i have to open this doc in spreadsheets which converts the file to google spreadsheet. I've seen in API documentation regarding the convert option. However, I couldn't find any way of converting the files while using RPC calls. Please suggest how can I achieve this.

  • I've followed [this](http://stackoverflow.com/questions/12741303/create-a-empty-spreadsheet-in-google-drive-using-google-spreadsheet-api-python) and used mime type as application/vnd.google-apps.spreadsheet. However I'm getting following error: HttpError: – user1421400 Jan 22 '13 at 11:08
0

There is no generic solution that I know of, but we are really talking about 4 or 5 lines of Python to achieve this.

The DrEdit Python sample saves files to Drive from App Engine Python, using the blobstore will be a simple addition to that.

Ali Afshar
  • 40,967
  • 12
  • 95
  • 109