Skip to content

Instantly share code, notes, and snippets.

@ikks
Created January 14, 2015 12:11
Show Gist options
  • Save ikks/836a5bd93b46d2317dfa to your computer and use it in GitHub Desktop.
Save ikks/836a5bd93b46d2317dfa to your computer and use it in GitHub Desktop.
You have an spreadsheet in Google and need to take a look in the cli, we are covered :)
#!/usr/bin/python2.7
import argparse
import gspread # Pip install it
def print_spreadsheet(email, password, key):
"""print_spreadsheet(email, password, key)
Prints out a Google spreadsheet
"""
gc = gspread.login(email, password)
wks = gc.open_by_key(key)
rows = wks.sheet1.get_all_values()
# - figure out column widths
widths = [ len(max(columns, key=len)) for columns in zip(*rows) ]
# - print the header
header, data = rows[0], rows[1:]
print(
' | '.join( format(title, "%ds" % width) for width, title in zip(widths, header) )
)
# - print the separator
print( '-+-'.join( '-' * width for width in widths ) )
# - print the data
for row in data:
print(
" | ".join( format(cdata, "%ds" % width) for width, cdata in zip(widths, row) )
)
parser = argparse.ArgumentParser()
parser.add_argument(
"-u",
"--email",
required=True,
action='store',
help="the email"
)
parser.add_argument(
"-p",
"--password",
required=True,
action='store',
)
parser.add_argument(
"-k",
"--key",
required=True,
action='store',
help="document key"
)
args = parser.parse_args()
print_spreadsheet(**vars(args))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment