Raghavan Raghavan - 7 months ago 45
Python Question

Creating empty spreadsheets in Google Drive using Drive API (in Python)

I want to create an empty Google Sheet (created only with metadata) in Google Drive. When I referred to the Google SpreadSheet API documentation, it says to use the DocumentsList API, but it's deprecated and instead asks me to use the Google Drive API. In the Drive API docs, I could not find any way to create an empty Sheet. Anyone have a clue on how to do this?


You can do this using the Drive API by setting the MIME type to application/vnd.google-apps.spreadsheet:

To do this in Python:

from apiclient.discovery import build
service = build('drive', 'v2')

import httplib2
credentials = ... # Obtain OAuth 2.0 credentials
http = credentials.authorize(httplib2.Http())

body = {
  'mimeType': 'application/vnd.google-apps.spreadsheet',
  'title': 'Name of Spreadsheet',
file = service.files().insert(body=body).execute(http=http)

Head over to the Google APIs Explorer to try it out!