Albert Braden
Albert Braden

Reputation: 27

Create Google Sheet in Python

I am using the Google Drive API to create a .csv, and I see it in my "Drive" display but I don't see it in "Sheets." How can I make it show in Sheets? Here is my code:

credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v3', http=http)

results = service.files().create(body={"name":"Test7.csv"}, media_body='/tmp/inputfile.csv', keepRevisionForever=None, useContentAsIndexableText=None, supportsTeamDrives=None, ocrLanguage=None, ignoreDefaultVisibility=None).execute()

Upvotes: 0

Views: 3564

Answers (1)

Chris Lam
Chris Lam

Reputation: 3614

Looks like you are just adding a CSV file to Drive.

You need to specify the mime type as Google Spreadsheet:

from apiclient.http import MediaFileUpload

file_metadata = {
  'name' : 'My Report',
  'mimeType' : 'application/vnd.google-apps.spreadsheet'
}

media = MediaFileUpload('test7.csv',
                        mimetype='text/csv',
                        resumable=True)

file = drive_service.files().create(body=file_metadata,
                                    media_body=media,
                                    fields='id').execute()

https://developers.google.com/drive/v3/web/manage-uploads#importing_to_google_docs_types_wzxhzdk18wzxhzdk19

Upvotes: 1

Related Questions