下载Google电子表格并另存为xls

时间:2016-04-05 13:29:28

标签: python google-sheets gdata

我试图编写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库。 我一整天都在苦苦挣扎,似乎无法弄清楚发生了什么。 任何人都可以弄明白并帮助吗? 我们将非常感谢能够实现上述目的的任何其他最小脚本。 谢谢

3 个答案:

答案 0 :(得分:1)

(2017年2月)大多数答案(包括OP中的代码)现在已经过时了{2012年的ClientLogin authentication was deprecated(!)和GData APIs是上一代Google API。虽然并非所有GData API都已被弃用,但all newer Google APIs 使用the Google Data protocol,包括最新的Google Sheets API(v4),这是更强大的&amp;比旧版API更灵活。

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

  • 将Google表格导出为CSV(blog post
  • &#34;穷人的纯文字PDF和#34;转换器(blog post)(*)

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

OP的解决方案是执行与您在&#34;将Google表格导出为CSV&#34;在上面发布,但将导出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)我&#39;生产。

答案 1 :(得分:0)

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

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

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

我很抱歉发布此答案,但我还不能发表评论。

此致

答案 2 :(得分:0)

您也可以尝试使用库pygsheets

-DEIGEN_MALLOC_ALREADY_ALIGNED=0
相关问题