0

嘿,我一直在尝试使用 Python 访问 Google 电子表格但没有成功

如果可能的话,有人可以建议如何在 Eclipse 中使用 python 访问谷歌电子表格

4

1 回答 1

0

你看过谷歌数据 python 库吗?

您可以在此处找到下载页面获取最新版本将其 CD 解压到文件夹中并运行./setup.py install

文档可在线获取。

这是一篇使用 python api 从电子表格中获取数据的博客文章。

如果帖子的内容在以后被删除,我已经复制了下面包含的代码:

import socket
import logging
import atom.service
import gdata.service
import gdata.spreadsheet
import gdata.spreadsheet.service
import gdata.spreadsheet.text_db

class SampleFetcher():
    def run(self, from_date=None, to_date=None):
        self.getGoogleEntries(from_date, to_date)

    def getGoogleEntries(self, from_date=None, to_date=None):                
        """
        Sample code to fetch data from a Google Spreadsheet using a query and a sort, followed
        by example of how to get the data out by column name.

        This code assumes you have a spreadsheet that looks something like this:

        Timestamp       |   First Name  |   Last Name
        8/16/2010 12:15:00  |   Michael     |   Woof
        8/17/2010 14:25:35  |   John        |   Doe          

        Google Spreadsheets normalizes the column names for the purposes of the API by stripping all non-alphanumerics and lower-casing,
        hence the column names used in the code as "timestamp", "firstname", and "lastname".
        """
        gd_client = gdata.spreadsheet.service.SpreadsheetsService()
        gd_client.email = 'YOUR_EMAIL'
        gd_client.password = 'YOUR_PASSWORD'
        gd_client.source = 'YOUR_APP_NAME'

        try:                    
        # log in
            gd_client.ProgrammaticLogin()
        except socket.sslerror, e:
            logging.error('Spreadsheet socket.sslerror: ' + str(e))
            return False

    key = 'YOUR_SPREADSHEET_KEY'
    wksht_id = 'YOUR_WORKSHEET_ID'

        q = gdata.spreadsheet.service.ListQuery()
        q.orderby = 'column:firstname'
        q.reverse = 'true'

        # Here's the actual query
        if from_date and to_date:
            q.sq = 'timestamp >= %s and timestamp <= %s' % (from_date, to_date)
        elif from_date:
            q.sq = 'timestamp >= %s' % from_date
        elif to_date:
            q.sq = 'timestamp <= %s' % to_date

        try:
        # fetch the spreadsheet data
            feed = gd_client.GetListFeed(key, wksht_id, query=q)
        except gdata.service.RequestError, e:
            logging.error('Spreadsheet gdata.service.RequestError: ' + str(e))
            return False
        except socket.sslerror, e:
            logging.error('Spreadsheet socket.sslerror: ' + str(e))
            return False

        # Iterate over the rows
        for row_entry in feed.entry:
        # to get the column data out, you use the text_db.Record class, then use the dict record.content
            record = gdata.spreadsheet.text_db.Record(row_entry=row_entry)
            print "%s,%s,%s" % (record.content['timestamp'], record.content['firstname'], record.content['lastname'])

if __name__ == "__main__":
    SampleFetcher().run()
于 2013-05-30T07:23:12.013 回答