【问题标题】:Download Google Spreadsheet and save as xls下载 Google 电子表格并另存为 xls
【发布时间】:2016-07-25 11:29:48
【问题描述】:

我正在尝试编写 python 程序以从 google spreedsheets 下载电子表格并将其保存为 .xls。 这是我的代码

import os
import sys
from getpass import getpass

import gdata.docs.service
import gdata.spreadsheet.service



'''
    get user information from the command line argument and 
    pass it to the download method
'''
def get_gdoc_information():
    email ="mygmailaccount"
    password ="mypassword"
    gdoc_id = ['google_id1','googleid2','googleidn']
    for doc_id in gdoc_id:
        try:
            download(doc_id, email, password)
        except Exception, e:
            raise e

#python gdoc.py 1m5F5TXAQ1ayVbDmUCyzXbpMQSYrP429K1FZigfD3bvk#gid=0
def download(doc_id, email, password, download_path=None, ):
    print "Downloading the XLS file with id %s" % doc_id

    gd_client = gdata.docs.service.DocsService()


    #auth using ClientLogin
    gs_client = gdata.spreadsheet.service.SpreadsheetsService()
    gs_client.ClientLogin(email, password)

    #getting the key(resource id and tab id from the ID)

    resource    = doc_id.split('#')[0]
    tab         = doc_id.split('#')[1].split('=')[1]
    resource_id = 'spreadsheet:'+resource

    if download_path is None:
        download_path = os.path.abspath(os.path.dirname(__file__))

    file_name = os.path.join(download_path, '%s.xls' % (doc_id))

    print 'Downloading spreadsheet to %s...' % file_name

    docs_token = gd_client.GetClientLoginToken()
    gd_client.SetClientLoginToken(gs_client.GetClientLoginToken())
    gd_client.Export(resource_id, file_name, gid=tab)
    gd_client.SetClientLoginToken(docs_token)

    print "Download Completed!"


if __name__=='__main__':
    get_gdoc_information()

每当我尝试运行它时,我都会在下面收到 gdata 错误

gdata.service.RequestError: {'status': 401, 'body': '<HTML>\n<HEAD>\n<TITLE>Unauthorized</TITLE>\n</HEAD>\n<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n<H1>Unauthorized</H1>\n<H2>Error 401</H2>\n</BODY>\n</HTML>\n', 'reason': 'Unauthorized'}

我正在使用 gdata 库。 我整天都在挣扎,似乎无法弄清楚发生了什么。 任何人都可以弄清楚并提供帮助吗? 任何其他可以实现上述目的的最小脚本将不胜感激。 谢谢

【问题讨论】:

    标签: python google-sheets gdata


    【解决方案1】:

    您的错误确实表明存在登录问题。也许您需要更改您的 Google 帐户设置或尝试其他登录方式。

    试试看这里: SyntaxError using gdata-python-client to access Google Book Search Data API

    或在这里: Download a spreadsheet from Google Docs using Python

    很抱歉将其发布为答案,但我还不能发布 cmets。

    问候

    【讨论】:

      【解决方案2】:

      你也可以试试库pygsheets

      import pygsheets
      
      gc = pygsheets.authorize()
      
      # Open spreadsheet and then workseet
      sh = gc.open('my new ssheet')
      wks = sh.sheet1
      
      #export as csv
      wks.export(pygsheets.ExportType.MS_Excel)
      

      【讨论】:

        【解决方案3】:

        (2017 年 2 月) 大多数答案(包括 OP 中的代码)现在已过时,因为 ClientLogin authentication was deprecated 早在 2012 年(!),GData APIs 是上一代谷歌 API。虽然并非所有 GData API 都已弃用,但all newer Google APIs使用the Google Data protocol,包括最新的Google Sheets API (v4),它比旧的 API 版本更强大、更灵活。 p>

        但是,请注意,Sheets API 主要用于以编程方式访问电子表格操作和功能(格式化单元格、单元格验证、调整列大小、创建图表、数据透视表等),但要执行 文件 -level 访问,例如导出到 XLS(X),请改用 Google Drive API。 Drive API 使用示例:

        • 将 Google 表格导出为 CSV (blog post)
        • “穷人的纯文本到 PDF”转换器 (blog post) (*)

        (*) - TL;DR:将纯文本文件上传到云端硬盘,导入/转换为 Google 文档格式,然后将该文档导出为 PDF。上面的帖子使用 Drive API v2; this follow-up post 描述了将其迁移到 Drive API v3,这是一个 developer video 结合了两个“穷人的转换器”帖子。

        OP 的解决方案是执行与您在上面的“将 Google 表格导出为 CSV”帖子中看到的相同的操作,但将导出 MIMEtype 从text/csv 更改为application/vnd.openxmlformats-officedocument.spreadsheetml.sheet。如需其他导入/导出到云端硬盘的格式,请参阅this related question SO answer 以及downloading files from Drive docs page

        要了解有关如何在 Python 中使用 Google API 的更多信息,请查看my blog 以及我正在制作的各种 Google 开发人员视频(series 1series 2)。

        【讨论】:

          猜你喜欢
          • 2017-10-16
          • 1970-01-01
          • 2020-11-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多