python 如何编写python脚本来操作谷歌电子表格数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2377301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:30:25  来源:igfitidea点击:

How to write a python script to manipulate google spreadsheet data

pythongoogle-sheetsgspread

提问by GeminiDNK

I am able to get the feed from the spreadsheet and worksheet ID. I want to capture the data from each cell.

我能够从电子表格和工作表 ID 中获取提要。我想从每个单元格中捕获数据。

i.e, I am able to get the feed from the worksheet. Now I need to get data(string type?) from each of the cells to make a comparison and for input.

即,我能够从工作表中获取提要。现在我需要从每个单元格中获取数据(字符串类型?)以进行比较和输入。

How exactly can I do that?

我怎么能做到这一点?

采纳答案by Lakshman Prasad

Google data api has a Python binding including for spreadsheets: http://code.google.com/apis/spreadsheets/data/1.0/developers_guide_python.html

谷歌数据 api 有一个 Python 绑定,包括电子表格:http: //code.google.com/apis/spreadsheets/data/1.0/developers_guide_python.html

回答by Warwick

There's another spreadsheet libraryworth to look at: gspread. I've used Google's data libary mentioned above and to me the provided api is weird. You need to extract spreadsheet's key to start working with this spreadsheet.

还有另一个值得一看的电子表格库:gspread。我使用了上面提到的 Google 的数据库,对我来说提供的 api 很奇怪。您需要提取电子表格的密钥才能开始使用此电子表格。

Here it's a way simpler. If you need to fetch the data from a cell you can just open a worksheet and get the value:

这是一种更简单的方法。如果您需要从单元格中获取数据,您只需打开一个工作表并获取值:

g = gspread.login('[email protected]', 'password')

worksheet = g.open('name_of_the_spreadsheet').get_worksheet(0)

# Say, you need A2
val = worksheet.cell(2, 1).value

# And then update
worksheet.update_cell(2, 1, '42') 

回答by andrewwowens

gspread is probably the fastest way to begin this process, however there are some speed limitations on updating data using gspread from your localhost. If you're moving large sets of data with gspread - for instance moving 20 columns of data over a column, you may want to automate the process using a CRON job.

gspread 可能是开始此过程的最快方法,但是使用本地主机上的 gspread 更新数据存在一些速度限制。如果您使用 gspread 移动大量数据 - 例如在列上移动 20 列数据,您可能希望使用 CRON 作业自动化该过程。

回答by Nithin

There's another spreadsheet library : pygsheets. Its similar to gspread, but uses google api v4. Hence provides more options.

还有另一个电子表格库:pygsheets。它类似于 gspread,但使用 google api v4。因此提供了更多的选择。

import pygsheets

gc = pygsheets.authorize()

# Open spreadsheet and then workseet
sh = gc.open('my new ssheet')
wks = sh.sheet1

# Update a cell with value (just to let him know values is updated ;) )
wks.update_cell('A1', "Hey yank this numpy array")

# update the sheet with array
wks.update_cells('A2', my_nparray.to_list())

# share the sheet with your friend
sh.share("[email protected]")