标签: gdata-python-client

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

您能否根据其密钥和工作表ID(gid)生成一个如何下载Google Docs电子表格的Python示例?我不能.

我已经搜索了API的第1版,第2版和第3版.我没有运气,我无法弄清楚他们编译的类似ATOM的API,gdata.docs.service.DocsService._DownloadFile私有方法说我是未经授权的,而且我不想自己写一个完整的Google登录认证系统.由于沮丧,我准备将自己捅到脸上.

我有一些电子表格,我想这样访问它们:

username = 'mygooglelogin@gmail.com'
password = getpass.getpass()

def get_spreadsheet(key, gid=0):
    ... (help!) ...

for row in get_spreadsheet('5a3c7f7dcee4b4f'):
    cell1, cell2, cell3 = row
    ...
Run Code Online (Sandbox Code Playgroud)

请保存我的脸.


更新1:我尝试了以下,但没有组合Download()Export()似乎工作.(DocsService 这里的文件)

import gdata.docs.service
import getpass
import os
import tempfile
import csv

def get_csv(file_path):
  return csv.reader(file(file_path).readlines())

def get_spreadsheet(key, gid=0):
  gd_client = gdata.docs.service.DocsService()
  gd_client.email = 'xxxxxxxxx@gmail.com'
  gd_client.password = getpass.getpass()
  gd_client.ssl = False
  gd_client.source = "My Fancy Spreadsheet Downloader"
  gd_client.ProgrammaticLogin()

  file_path = tempfile.mktemp(suffix='.csv')
  uri …
Run Code Online (Sandbox Code Playgroud)

python google-docs google-docs-api gdata-python-client

32
推荐指数
4
解决办法
3万
查看次数

如何将Google电子表格的工作表字符串ID转换为整数索引(GID)?

要将Google电子表格的单个工作表导出为CSV,需要传递整数工作表索引(GID).

https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=%s&gid=%d&exportFormat=csv

但是,那些信息在哪里?使用gdata.spreadsheets.client,我可以找到工作表的一些字符串id,如"oc6,ocv,odf".

client = gdata.spreadsheets.client.SpreadsheetsClient()
feed = client.GetWorksheets(spreadsheet, auth_token=auth_token)
Run Code Online (Sandbox Code Playgroud)

它返回原子XML下面.(一部分)

<entry gd:etag="&quot;URJFCB1NQSt7ImBoXhU.&quot;">
    <id>https://spreadsheets.google.com/feeds/worksheets/0AvhN_YU3r5e9dGpTWGx3UVU3MTczaXJuNEFKQjMwN2c/ocw</id>
    <updated>2012-06-21T08:19:46.587Z</updated>
    <app:edited xmlns:app="http://www.w3.org/2007/app">2012-06-21T08:19:46.587Z</app:edited>
    <category scheme="http://schemas.google.com/spreadsheets/2006" term="http://schemas.google.com/spreadsheets/2006#worksheet"/>
    <title>AchievementType</title>
    <content type="application/atom+xml;type=feed" src="https://spreadsheets.google.com/feeds/list/0AvhN_YU3r5e9dGpTWGx3UVU3MTczaXJuNEFKQjMwN2c/ocw/private/full"/>
    <link rel="http://schemas.google.com/spreadsheets/2006#cellsfeed" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/0AvhN_YU3r5e9dGpTWGx3UVU3MTczaXJuNEFKQjMwN2c/ocw/private/full"/>
    <link rel="http://schemas.google.com/visualization/2008#visualizationApi" type="application/atom+xml" href="https://spreadsheets.google.com/tq?key=0AvhN_YU3r5e9dGpTWGx3UVU3MTczaXJuNEFKQjMwN2c&amp;sheet=ocw"/>
    <link rel="self" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/worksheets/0AvhN_YU3r5e9dGpTWGx3UVU3MTczaXJuNEFKQjMwN2c/private/full/ocw"/>
    <link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/worksheets/0AvhN_YU3r5e9dGpTWGx3UVU3MTczaXJuNEFKQjMwN2c/private/full/ocw"/>
    <gs:rowCount>280</gs:rowCount>
    <gs:colCount>28</gs:colCount>
</entry>
Run Code Online (Sandbox Code Playgroud)

我也尝试使用sheet参数但是因"Invalid Sheet"错误而失败.

https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=%s&sheet=XXX&exportFormat=csv

我想应该有一些神奇的功能,但找不到它.我怎样才能将它们转换为整数id?或者我可以导出带有字符串ID的工作表吗?

编辑:我刚刚用python制作了转换表.很脏但工作:-(

GID_TABLE = {
    'od6': 0, 
    'od7': 1, 
    'od4': 2, 
    'od5': 3, 
    'oda': 4, 
    'odb': 5, 
    'od8': 6, 
    'od9': 7, 
    'ocy': 8, 
    'ocz': 9, 
    'ocw': 10, 
    'ocx': 11, 
    'od2': 12, 
    'od3': 13, 
    'od0': …
Run Code Online (Sandbox Code Playgroud)

gdata-api google-sheets gdata-python-client google-spreadsheet-api

17
推荐指数
3
解决办法
1万
查看次数

使用gdata python客户端在博客上批量发布

我正在尝试将我的所有Livejournal帖子复制到blogger.com上的新博客.我通过使用gdata python客户端附带的略微修改的示例来实现.我有一个json文件,其中包含从Livejournal导入的所有帖子.问题是blogger.com每天发布新博客条目的每日限制 - 50,所以你可以想象我将在一个月内复制1300多个帖子,因为我无法在50次导入后以编程方式输入验证码.

我最近了解到gdata中还有批处理操作模式,但我无法弄清楚如何使用它.谷歌搜索并没有真正帮助.

任何建议或帮助将受到高度赞赏.

谢谢.

更新

为了以防万一,我使用以下代码

#!/usr/local/bin/python
import json
import requests

from gdata import service
import gdata
import atom
import getopt
import sys

from datetime import datetime as dt
from datetime import timedelta as td
from datetime import tzinfo as tz

import time

allEntries = json.load(open("todays_copy.json", "r"))

class TZ(tz):
    def utcoffset(self, dt): return td(hours=-6)

class BloggerExample:
    def __init__(self, email, password):
        # Authenticate using ClientLogin.
        self.service = service.GDataService(email, password)
        self.service.source = "Blogger_Python_Sample-1.0"
        self.service.service = …
Run Code Online (Sandbox Code Playgroud)

python blogger batch-processing gdata-python-client

7
推荐指数
2
解决办法
1442
查看次数

如何在 Google 电子表格中的空行之后读取行?

我正在使用gdata-python-client从 Google 电子表格中读取数据。我读取行的代码如下:

import gdata.speadsheet.text_db

gd_client = gdata.spreadsheet.text_db.DatabaseClient(
                     username=setting['account_name'],
                     password=setting['account_pass'])

xls_db = gd_client.GetDatabases(spreadsheet_key=setting['spreadsheet_id'])
first_sheet = xls_db[0].GetTables()[0]
entries = first_sheet.GetRecords(1, 200)
Run Code Online (Sandbox Code Playgroud)

比方说,电子表格有160行和12行是空的。当我尝试使用上述代码读取所有 160 行时,它只读取前 11 行(即,直到它获得空的12行)。如果电子表格没有任何空行,代码将读取所有 160 行。

当我尝试从空行读取下一行时,它不返回任何内容。例如:

entries = first_sheet.GetRecords(50, 55) # entries is None
Run Code Online (Sandbox Code Playgroud)

如何从包含空行的 Google 电子表格中读取所有行。

任何帮助,将不胜感激。

gdata-api gdata-python-client google-spreadsheet-api

2
推荐指数
1
解决办法
3303
查看次数