Created
January 14, 2015 12:11
-
-
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 :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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