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

2024-06-16 12:32:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写python程序从googlespreedsheets下载一个电子表格并将其保存为.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错误

^{pr2}$

我正在使用gdata库。 我一整天都在挣扎,似乎搞不清到底发生了什么。 有谁能帮帮忙吗? 任何其他能达到我上述目的的最低限度的脚本将不胜感激。 谢谢你


Tags: thepathimportclientiddocsdocos
3条回答

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

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

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

(*)-TL;DR:将纯文本文件上载到驱动器,导入/转换为Google Docs格式,然后将该文档导出为PDF。上面的文章使用了driveapiv2;this follow-up post描述了将其迁移到driveapiv3,这里是一个developer video组合了两个“穷人的转换器”帖子。在

该操作的解决方案是执行与上面“将Google工作表导出为CSV”帖子中相同的操作,但将export MIMEtype从text/csv更改为application/vnd.openxmlformats-officedocument.spreadsheetml.sheet。有关驱动器的其他导入/导出格式,请参见this related question SO answer以及downloading files from Drive docs page。在

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

您的错误确实表明存在登录问题。也许你需要改变你的谷歌帐户设置或尝试另一种登录方式。在

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

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

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

问候

您也可以尝试使用库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)

相关问题 更多 >