使用Python的Google Docs API

use*_*793 5 python gdata-api google-docs-api

使用Google Document API,我正在尝试创建新文档,并在我的Google文档中提供特定文件夹中所有当前文档的列表.我开始使用python开发,所以我仍然有点粗糙.

我想做的事情:

  1. 如果该名称尚不存在,则仅创建名称为[文件夹名称]的集合(或文件夹)
  2. 在[文件夹名称]中创建文档
  3. 仅从[文件夹名称]获取文档列表以及文档本身的链接

我相信我正在使用Google Docs API 3.0并且正在使用gdata-2.0.16 helper for python.

代码到目前为止:


    import gdata.docs.data
    import gdata.docs.client

    class SampleConfig(object):
        APP_NAME = 'GDataDocumentsListAPISample-v1.0'
        DEBUG = False

    client = gdata.docs.client.DocsClient()
    client.ClientLogin('[email_address]','[password]',source=SampleConfig.APP_NAME )

    col = gdata.docs.data.Resource(type='folder', title='Folder Name')
    col = client.CreateResource(col)

    doc = gdata.docs.data.Resource(type='document', title='I did this')
    doc = client.CreateResource(doc, collection=col)

所以现在回答问题:我无可救药地陷入困境:

  1. 如何检查[文件夹名称]是否存在?
  2. 如何检索仅[文件夹名称]的内容?
  3. 如何获取我在此文件夹中创建的所有文档的绝对链接?

我知道我离这里已经很远了,但是你能提供的任何帮助或建议都会很棒.

提前致谢!

Ali*_*har 3

您可以查询文件夹或文档。获得文件夹后,您可以列出其内容。下面是一个使用 Python 库的示例:

# Create a query matching exactly a title, and include collections
q = gdata.docs.client.DocsQuery(
    title='EFD',
    title_exact='true',
    show_collections='true'
)

# Execute the query and get the first entry (if there are name clashes with
# other folders or files, you will have to handle this).
folder = client.GetResources(q=q).entry[0]

# Get the resources in the folder
contents = client.GetResources(uri=folder.content.src)

# Print out the title and the absolute link
for entry in contents.entry:
    print entry.title.text, entry.GetSelfLink().href
Run Code Online (Sandbox Code Playgroud)

输出

My posted doc https://docs.google.com/...
subtestcoll2 https://docs.google.com/...
guestimates_1 https://docs.google.com/...
phase 2 delivery plan - draft https://docs.google.com/...
Meeting agenda June 09 https://docs.google.com/...
Phase 2 spec for Graeme 2 March 2009 https://docs.google.com/...
EFD Meeting 2nd June https://docs.google.com/...
Run Code Online (Sandbox Code Playgroud)