Last active
December 14, 2021 13:22
-
-
Save wjkoh/b8b72f36b0b211f3232fe3d664022f34 to your computer and use it in GitHub Desktop.
Export CSV from Google Spreadsheet using Python
This file contains 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
import requests | |
import argparse | |
import sys | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--spreadsheet_id', type=str, required=True) | |
parser.add_argument('--gid', type=int, required=True) | |
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), default=sys.stdout) | |
args = parser.parse_args() | |
# The given spreadsheet should be publicly accessible. | |
url = f'https://docs.google.com/spreadsheets/d/{args.spreadsheet_id}/export?gid={args.gid}&format=csv' | |
print(f'Reading {url}...') | |
r = requests.get(url) | |
args.outfile.write(r.text) | |
args.outfile.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment