我正在开发一个Python 2.7
脚本,用于分析SQL表中的数据,最后生成一个CSV文件。
生成文件后,我正在登录我的Google工作表帐户并使用导入选项将我的CSV文件导入到Google电子表格中
手工劳动有点愚蠢,我希望将这种能力添加到我的脚本中。
所以,我按照这个指南,Python Quickstart,并能够完成所有步骤。
然后我跟着Google Sheets API reference,看着Method: spreadsheets.create。如果我理解正确,它不提供从文件导入的选项。
似乎没有用于导入功能的API。
如何使用Google表格API V4导入CSV文件?他们是我失踪的榜样/参考吗?
您有两种导入g CSV文件的选项。您可以使用Drive API从CSV创建电子表格,或者您可以使用Sheets API将create作为空电子表格,然后使用spreadsheets.batchUpdate和PasteDataRequest添加CSV数据。
我喜欢Burnash的gspread库,但他的答案中的import_csv
功能是有限的。它始终在第一个工作表(选项卡)的A1
处开始粘贴,并删除所有其他选项卡。
我需要从特定的选项卡和单元格开始粘贴,所以我采用了Sam Berlin的建议来使用PasteDataRequest。这是我的功能:
def pasteCsv(csvFile, sheet, cell):
'''
csvFile - path to csv file to upload
sheet - a gspread.Spreadsheet object
cell - string giving starting cell, optionally including sheet/tab name
ex: 'A1', 'MySheet!C3', etc.
'''
if '!' in cell:
(tabName, cell) = cell.split('!')
wks = sheet.worksheet(tabName)
else:
wks = sheet.sheet1
(firstRow, firstColumn) = gspread.utils.a1_to_rowcol(cell)
with open(csvFile, 'r') as f:
csvContents = f.read()
body = {
'requests': [{
'pasteData': {
"coordinate": {
"sheetId": wks.id,
"rowIndex": firstRow-1,
"columnIndex": firstColumn-1,
},
"data": csvContents,
"type": 'PASTE_NORMAL',
"delimiter": ',',
}
}]
}
return sheet.batch_update(body)
请注意,我使用原始的pasteData请求而不是更高级别的update_cells
方法来利用Google自动(正确)处理包含引用字符串的输入数据,这些字符串可能包含非分隔符逗号。
我花了几个小时试图让其他任何答案都有效。图书馆不能很好地解释身份验证,也不能使用谷歌提供的处理凭据的方式。另一方面,Sam的回答没有详细说明使用API的细节,有时可能会令人困惑。因此,这是将CSV上传到gSheets的完整配方。它使用Sam和CapoChino的答案以及我自己的一些研究。
credentials.json
没有额外的步骤
quickstart.py
很容易适应authenticate.py
范围应包含https://www.googleapis.com/auth/spreadsheets
希望现在你已经存储了你的凭据,所以让我们转到实际的代码
import pickle
from googleapiclient.discovery import build
SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms' # Get this one from the link in browser
worksheet_name = 'Sheet2'
path_to_csv = 'New Folder/much_data.csv'
path_to_credentials = 'Credentials/token.pickle'
# convenience routines
def find_sheet_id_by_name(sheet_name):
# ugly, but works
sheets_with_properties = API \
.spreadsheets() \
.get(spreadsheetId=SPREADSHEET_ID, fields='sheets.properties') \
.execute() \
.get('sheets')
for sheet in sheets_with_properties:
if 'title' in sheet['properties'].keys():
if sheet['properties']['title'] == sheet_name:
return sheet['properties']['sheetId']
def push_csv_to_gsheet(csv_path, sheet_id):
with open(csv_path, 'r') as csv_file:
csvContents = csv_file.read()
body = {
'requests': [{
'pasteData': {
"coordinate": {
"sheetId": sheet_id,
"rowIndex": "0", # adapt this if you need different positioning
"columnIndex": "0", # adapt this if you need different positioning
},
"data": csvContents,
"type": 'PASTE_NORMAL',
"delimiter": ',',
}
}]
}
request = API.spreadsheets().batchUpdate(spreadsheetId=SPREADSHEET_ID, body=body)
response = request.execute()
return response
# upload
with open(path_to_credentials, 'rb') as token:
credentials = pickle.load(token)
API = build('sheets', 'v4', credentials=credentials)
push_csv_to_gsheet(
csv_path=path_to_csv,
sheet_id=find_sheet_id_by_name(worksheet_name)
)
直接使用batchUpdate
的好处是它可以在一秒钟内上传数千行。在较低的水平gspread
做同样的,应该是高效的。还有gspread-pandas。
附:代码是用python 3.5
测试的,但是这个帖子似乎最适合提交给它。
作为Sam Berlin的答案的替代方案,您可以将CSV转换为列表列表并将其设置为POST有效负载。
这样的函数看起来像这样:
def preprocess(table):
table.to_csv('pivoted.csv') # I use Pandas but use whatever you'd like
_file = open('pivoted.csv')
contents = _file.read()
array = contents.split('\n')
master_array = []
for row in array:
master_array.append(row.split(','))
return master_array
该主数组被抛入以下内容:
body = {
'values': newValues
}
result2 = service.spreadsheets().values().update(spreadsheetId=spreadsheetId, range=rangeName + str(len(values) + start + 1), valueInputOption="USER_ENTERED", body=body).execute()
它对我来说很好。
Sam Berlin的另一个替代方案。如果您使用的是Python,则可以通过gspread使用Drive API导入CSV文件。这是一个例子:
import gspread
# Check how to get `credentials`:
# https://github.com/burnash/gspread
gc = gspread.authorize(credentials)
# Read CSV file contents
content = open('file_to_import.csv', 'r').read()
gc.import_csv('<SPREADSHEET_ID>', content)