如何使用Google表格API V4导入CSV文件

问题描述 投票:5回答:5

Background

我正在开发一个Python 2.7脚本,用于分析SQL表中的数据,最后生成一个CSV文件。

生成文件后,我正在登录我的Google工作表帐户并使用导入选项将我的CSV文件导入到Google电子表格中

手工劳动有点愚蠢,我希望将这种能力添加到我的脚本中。

Google Sheets API V4

所以,我按照这个指南,Python Quickstart,并能够完成所有步骤。

然后我跟着Google Sheets API reference,看着Method: spreadsheets.create。如果我理解正确,它不提供从文件导入的选项。

似乎没有用于导入功能的API。

Question

如何使用Google表格API V4导入CSV文件?他们是我失踪的榜样/参考吗?

python-2.7 google-spreadsheet-api google-sheets-api
5个回答
10
投票

您有两种导入g CSV文件的选项。您可以使用Drive API从CSV创建电子表格,或者您可以使用Sheets API将create作为空电子表格,然后使用spreadsheets.batchUpdatePasteDataRequest添加CSV数据。


4
投票

我喜欢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自动(正确)处理包含引用字符串的输入数据,这些字符串可能包含非分隔符逗号。


2
投票

我花了几个小时试图让其他任何答案都有效。图书馆不能很好地解释身份验证,也不能使用谷歌提供的处理凭据的方式。另一方面,Sam的回答没有详细说明使用API​​的细节,有时可能会令人困惑。因此,这是将CSV上传到gSheets的完整配方。它使用Sam和CapoChino的答案以及我自己的一些研究。

  1. 验证/设置。一般来说,请参阅docs 大蓝色按钮将让你credentials.json没有额外的步骤 quickstart.py很容易适应authenticate.py 范围应包含https://www.googleapis.com/auth/spreadsheets

希望现在你已经存储了你的凭据,所以让我们转到实际的代码

  1. 开箱即用的食谱:
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测试的,但是这个帖子似乎最适合提交给它。


0
投票

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

它对我来说很好。


0
投票

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)

相关问题:Upload CSV to Google Sheets using gspread

© www.soinside.com 2019 - 2024. All rights reserved.