2016-04-05 16 views
1

Googleスプレットシートからスプレッドシートをダウンロードし、.xlsとして保存するためのpython手順を書いていますか?私はそれを実行しようとするたびに は、ここで私はGDATAライブラリを使用してGoogleスプレッドシートをダウンロードしてxlsとして保存

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エラーを取得し、私のコード

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() 

です。 私はこの一日中苦労して何が起こっているのか理解していないようです。 誰でも分かりますか? 上記のように私の目的を達成できる他の最小のスクリプトは、大いに感謝します。 ありがとうございます

答えて

0

あなたのエラーはログインの問題を示唆しています。 Googleアカウントの設定を変更するか、別の方法でログインする必要があるかもしれません。

はここを見てみます SyntaxError using gdata-python-client to access Google Book Search Data API

かここに: Download a spreadsheet from Google Docs using Python

私は答えとしてこれを投稿して申し訳ございませんが、私はまだコメントを投稿することはできません。

よろしく

0

また、ライブラリpygsheetsを試みることができます。 (OP内のコードを含む)

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) 
0

(2017年2月)ほとんどの答えは古く2012年に戻ってClientLogin authentication was deprecated(!)となりました、とGData APIsは、GoogleのAPIの以前の世代です。 all newer Google APIs do the Google Data protocolは最新のGoogle Sheets API(v4)を使用していますが、これは以前のAPIリリースよりもはるかに強力な&です。

しかし、シートのAPIをプログラム(チャート、ピボットテーブル、等を作成、列のサイズ変更、書式のセル、セルの検証)スプレッドシート操作を&機能にアクセスするための主であるが、ファイルレベルのアクセスを実行することに注意してくださいXLS(X)にエクスポートするなどの場合は、代わりにGoogle Drive APIを使用してください。ドライブのAPIを使用しての例:CSV(blog post

  • "貧しい人のプレーンテキストPDFへの" コンバータ(blog post)(*)
  • としてGoogleスプレッドシートをエクスポート

    • (*) - TL:DR:プレーンテキストファイルをドライブにアップロードし、Googleドキュメント形式にインポート/コンバートした後、PDFとしてエクスポートします。上記の記事では、Drive API v2を使用しています。 this follow-up postはドライブAPI v3に移行することを説明しています。ここには「貧乏人のコンバータ」の両方の投稿を組み合わせたdeveloper videoがあります。

      上記の「GoogleシートをCSV形式でエクスポートする」の操作と同じ操作を行いますが、エクスポートMIMEタイプをtext/csvからapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetに変更してください。ドライブとの間でインポート/エクスポートする他のフォーマットについては、this related question SO answerdownloading files from Drive docs pageを参照してください。

      一般的にGoogle APIをPythonで使用する方法の詳細については、my blogと私が製作しているさまざまなGoogle開発者向けビデオ(series 1と)をご覧ください。

    関連する問題