栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Python从Google文档下载电子表格

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Python从Google文档下载电子表格

如果有人遇到这种情况寻求快速解决方案,这是另一个(当前)不依赖gdata客户端库的解决方案:

#!/usr/bin/pythonimport re, urllib, urllib2class Spreadsheet(object):    def __init__(self, key):        super(Spreadsheet, self).__init__()        self.key = keyclass Client(object):    def __init__(self, email, password):        super(Client, self).__init__()        self.email = email        self.password = password    def _get_auth_token(self, email, password, source, service):        url = "https://www.google.com/accounts/ClientLogin"        params = { "Email": email, "Passwd": password, "service": service, "accountType": "HOSTED_OR_GOOGLE", "source": source        }        req = urllib2.Request(url, urllib.urlenpre(params))        return re.findall(r"Auth=(.*)", urllib2.urlopen(req).read())[0]    def get_auth_token(self):        source = type(self).__name__        return self._get_auth_token(self.email, self.password, source, service="wise")    def download(self, spreadsheet, gid=0, format="csv"):        url_format = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=%s&exportFormat=%s&gid=%i"        headers = { "Authorization": "GoogleLogin auth=" + self.get_auth_token(), "GData-Version": "3.0"        }        req = urllib2.Request(url_format % (spreadsheet.key, format, gid), headers=headers)        return urllib2.urlopen(req)if __name__ == "__main__":    import getpass    import csv    email = "" # (your email here)    password = getpass.getpass()    spreadsheet_id = "" # (spreadsheet id here)    # Create client and spreadsheet objects    gs = Client(email, password)    ss = Spreadsheet(spreadsheet_id)    # Request a file-like object containing the spreadsheet's contents    csv_file = gs.download(ss)    # Parse as CSV and print the rows    for row in csv.reader(csv_file):        print ", ".join(row)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/610672.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号