Grab GoogleSpreadsheet with a Python

From GersteinInfo

Jump to: navigation, search

Example Usage

from GoogleSpreadsheet import GoogleSpreadsheet

spreadsheet_id = "o04764524430027910313.8987538912905796708" # Unique Spreadsheet ID
worksheet_id = "od6"

spreadsheet = GoogleSpreadsheet(spreadsheet_id, worksheet_id)
for row in spreadsheet:
  print row["lastname"]


  from xml.etree import ElementTree
except ImportError:
  from elementtree import ElementTree
import gdata.spreadsheet.service
import gdata.service
import atom.service
import gdata.spreadsheet
import atom

class GoogleSpreadsheet:
	''' An iterable google spreadsheet object.  Each row is a dictionary with an entry for each field, keyed by the header.  GData libraries from Google must be installed.'''
	def __init__(self, spreadsheet_id, worksheet_id, user='**********', password='************', source=''):
		gd_client = gdata.spreadsheet.service.SpreadsheetsService() = user
		gd_client.password = password
		gd_client.source = source
		self.count = 0
		self.rows = self.formRows(gd_client.GetListFeed(spreadsheet_id, worksheet_id))
	def formRows(self, ListFeed):
		rows = []
		for entry in ListFeed.entry:
			d = {}
			for key in entry.custom.keys():
				d[key] = entry.custom[key].text
		return rows
	def __iter__(self):
		return self
	def next(self):
		if self.count >= len(self.rows):
			self.count = 0
			raise StopIteration
			self.count += 1
			return self.rows[self.count - 1]
	def __getitem__(self, item):
		return self.rows[item]
	def __len__(self):
		return len(self.rows)
Personal tools