/google-objects

Wraps google-api-python-client for pythonic Google API interaction.

Primary LanguagePythonApache License 2.0Apache-2.0

Google Objects

Thin, pythonic OO wrapper around Google's "google-api-python-client" library. Currently supports Python 2.7 🐍🐍🐍

Installation

  $ pip install google-objects

Usage

Requires a valid Google API Credentials object from Google's excellent oauth2lib library. For more information, visit here.

Google Drive

  • Retrieve drive 'About' information:
  from google_objects import DriveAPI

  gdrive = DriveAPI(OAUTH2LIB_CREDS)
  about = gdrive.get_about()

  print about.email
  print about.name

  # prints link to profile photo
  print about.photo

  # ...
  • List files in drive by type:
  files_by_type = {
      'slides': gdrive.list_files('presentation'),
      'folders': gdrive.list_files('folder'),
      'spreadsheets': gdrive.list_files('spreadsheets'),
  }

  for file in files_by_type['folders']:
    print file.id
    print file.name

  for file in files_by_type['spreadsheets']:
    # prints list of parent folder IDs
    print file.parents

  # ...
  • Copy and share file:
	file = gdrive.get_file('FILE_ID')
	new_file = file.copy('NEW_FILE_NAME', ['PARENT_FOLDER_1', 'PARENT_FOLDER_2'])
	
	# allow myfriend@hotmail.com to view
	permission = new_file.add_permission('myfriend@hotmail.com')

	# print newly created permission information
	print permission.role, permission.type, permission.email

Google Slides

  • Retrieve presentation and loop through elements:
	from google_objects import SlidesAPI

	gslides = SlidesAPI(OAUTHLIB_CREDS)
	presentation = gslides.get_presentation('PRESENTATION_ID')

	# print slides attributes
	for slide in presentation:
		print slide.id

		for element in slide: # equivalent to 'for element in presentation.elements()' 	
			print element.type 
			# Shape, Table, etc
	
  • Check text in shape:
	shape = presentation.get_element_by_id('SHAPE_ID')
	for segment in shape.text:
		print segment.text
  • Batch update every cell in table:
	# use with to perform batch updates in block
	with presentation as pres:
		table = pres.get_element_by_id('TABLE_ID')
		for cell in table:
			print cell.location # tuple containing cell location
			for segment in cell.text:
				# update cell
				segment.text = 'UPDATED_VALUE'

Google Sheets

  • Retrieve spreadsheet and loop through sheets:
	from google_objects import SheetsAPI

	gsheets = SheetsAPI(OAUTHLIB_CREDS)
	spreadsheet = gsheets.get_spreadsheet('SPREADSHEET_ID')

	for sheet in spreadsheet:
		print sheet.id, sheet.title
  • Get sheet by ID and return its full block of values:
	sheet = spreadsheet['SHEET_ID']
	values = sheet.values()	
  • Get named range value block:
	named_ranges = spreadsheet.named_ranges('SHEET_NAME!A:C')
	for rng in named_range:
		values = named_range.get_block()
  • Loop through values block:
	values = spreadsheet.get_range('SHEET_NAME!A:C')
	for row in values:
		for cell in row:
			print cell.value