用Python创建Google表格

时间:2017-06-13 01:58:38

标签: python google-sheets-api

我正在使用Google Drive API创建.csv,我在“云端硬盘”显示屏中看到它,但我没有在“表格”中看到它。如何在表格中显示?这是我的代码:

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()

1 个答案:

答案 0 :(得分:1)

您似乎只是在向云端硬盘添加CSV文件。

您需要将mime类型指定为Google电子表格:

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