使用 Python 使用 URL 将 Google 工作表转换为 CSV
Convert Google Sheets to CSV with URL using Python
我想弄清楚如何让这个程序(在线找到)使用 URL 或 Google 表格 ID 而不是名称来查找文档并转换为 CSV。目前它需要一个文件名并正常运行。这是我第一次使用 API,我已经有一段时间没有使用 python,所以非常感谢您的帮助!
from __future__ import print_function
import os
from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
SCOPES = 'https://www.googleapis.com/auth/drive.readonly'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
FILENAME = 'No Name'
SRC_MIMETYPE = 'application/vnd.google-apps.spreadsheet'
DST_MIMETYPE = 'text/csv'
files = DRIVE.files().list(
q='name="%s" and mimeType="%s"' % (FILENAME, SRC_MIMETYPE),
orderBy='modifiedTime desc,name').execute().get('files', [])
if files:
fn = '%s.csv' % os.path.splitext(files[0]['name'].replace(' ', '_'))[0]
print('Exporting "%s" as "%s"... ' % (files[0]['name'], fn), end='')
data = DRIVE.files().export(fileId=files[0]['id'], mimeType=DST_MIMETYPE).execute()
if data:
with open(fn, 'wb') as f:
f.write(data)
print('DONE')
else:
print('ERROR (could not download file)')
else:
print('!!! ERROR: File not found')
编辑:增加清晰度
我认为您的目标和现状如下。
- 您想使用电子表格 ID 而不是电子表格名称将 Google 电子表格导出为 CSV 文件。
- 您想使用 googleapis 实现此目的 python。
- 您已经能够使用驱动器 API 从 Google 驱动器获取值。
修改点:
- 在您的脚本中,为了检索电子表格 ID,使用了使用电子表格名称的“文件:列表”方法,并使用“文件:导出”方法将电子表格导出为 CSV。
- 为了达到你的目的,你可以直接使用“文件:导出”方法的电子表格ID。
- 并且,在您的脚本中,文件名是从电子表格名称中检索的。在这种情况下,“Files:get”方法用于从电子表格 ID 中检索电子表格名称。
当以上几点反映到你的脚本中,就会变成如下。请将您的脚本DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
修改如下。
修改后的脚本:
DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
spreadsheetId = '###' # Please set the Spreadsheet ID.
DST_MIMETYPE = 'text/csv'
# Retrieve Spreadsheet name from Spreadsheet ID.
res1 = DRIVE.files().get(fileId=spreadsheetId).execute()
# Export Spreadsheet as CSV.
fn = '%s.csv' % os.path.splitext(res1['name'].replace(' ', '_'))[0]
print('Exporting "%s" as "%s"... ' % (res1['name'], fn), end='')
data = DRIVE.files().export(
fileId=spreadsheetId, mimeType=DST_MIMETYPE).execute()
if data:
with open(fn, 'wb') as f:
f.write(data)
print('DONE')
else:
print('ERROR (could not download file)')
参考文献:
我想弄清楚如何让这个程序(在线找到)使用 URL 或 Google 表格 ID 而不是名称来查找文档并转换为 CSV。目前它需要一个文件名并正常运行。这是我第一次使用 API,我已经有一段时间没有使用 python,所以非常感谢您的帮助!
from __future__ import print_function
import os
from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
SCOPES = 'https://www.googleapis.com/auth/drive.readonly'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
FILENAME = 'No Name'
SRC_MIMETYPE = 'application/vnd.google-apps.spreadsheet'
DST_MIMETYPE = 'text/csv'
files = DRIVE.files().list(
q='name="%s" and mimeType="%s"' % (FILENAME, SRC_MIMETYPE),
orderBy='modifiedTime desc,name').execute().get('files', [])
if files:
fn = '%s.csv' % os.path.splitext(files[0]['name'].replace(' ', '_'))[0]
print('Exporting "%s" as "%s"... ' % (files[0]['name'], fn), end='')
data = DRIVE.files().export(fileId=files[0]['id'], mimeType=DST_MIMETYPE).execute()
if data:
with open(fn, 'wb') as f:
f.write(data)
print('DONE')
else:
print('ERROR (could not download file)')
else:
print('!!! ERROR: File not found')
编辑:增加清晰度
我认为您的目标和现状如下。
- 您想使用电子表格 ID 而不是电子表格名称将 Google 电子表格导出为 CSV 文件。
- 您想使用 googleapis 实现此目的 python。
- 您已经能够使用驱动器 API 从 Google 驱动器获取值。
修改点:
- 在您的脚本中,为了检索电子表格 ID,使用了使用电子表格名称的“文件:列表”方法,并使用“文件:导出”方法将电子表格导出为 CSV。
- 为了达到你的目的,你可以直接使用“文件:导出”方法的电子表格ID。
- 并且,在您的脚本中,文件名是从电子表格名称中检索的。在这种情况下,“Files:get”方法用于从电子表格 ID 中检索电子表格名称。
当以上几点反映到你的脚本中,就会变成如下。请将您的脚本DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
修改如下。
修改后的脚本:
DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
spreadsheetId = '###' # Please set the Spreadsheet ID.
DST_MIMETYPE = 'text/csv'
# Retrieve Spreadsheet name from Spreadsheet ID.
res1 = DRIVE.files().get(fileId=spreadsheetId).execute()
# Export Spreadsheet as CSV.
fn = '%s.csv' % os.path.splitext(res1['name'].replace(' ', '_'))[0]
print('Exporting "%s" as "%s"... ' % (res1['name'], fn), end='')
data = DRIVE.files().export(
fileId=spreadsheetId, mimeType=DST_MIMETYPE).execute()
if data:
with open(fn, 'wb') as f:
f.write(data)
print('DONE')
else:
print('ERROR (could not download file)')