【问题标题】:Google Docs API with Python带有 Python 的 Google 文档 API
【发布时间】:2012-04-20 17:28:08
【问题描述】:

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

我正在尝试做的事情:

  1. 仅在以下情况下创建名称为 [文件夹名称] 的集合(或文件夹) 名称尚不存在
  2. 在 [文件夹名称] 中创建一个文档
  3. 仅从 [文件夹名称] 获取文档列表以及指向 文件本身

我相信我正在使用 Google Docs API 3.0 并且正在为 python 使用 gdata-2.0.16 助手。

到目前为止的代码:

导入 gdata.docs.data 导入 gdata.docs.client 类 SampleConfig(对象): APP_NAME = 'GDataDocumentsListAPISample-v1.0' 调试 = 假 客户端 = gdata.docs.client.DocsClient() client.ClientLogin('[email_address]','[password]',source=SampleConfig.APP_NAME ) col = gdata.docs.data.Resource(type='文件夹', title='文件夹名称') col = client.CreateResource(col) doc = gdata.docs.data.Resource(type='document', title='我做了这个') doc = client.CreateResource(doc, collection=col)

那么现在开始回答问题:我陷入了无望的困境:

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

我知道我离完成还有很长的路要走,但你能提供的任何帮助或建议都会很棒。

提前致谢!

【问题讨论】:

    标签: python gdata-api google-docs-api


    【解决方案1】:

    You can query for a folder or document。拥有文件夹后,您可以列出其内容。以下是 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
    

    输出

    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/...
    

    【讨论】:

    • 感谢您在回答中提供的详细信息。真的很感激,我想我已经开始根据你的例子开始工作了。但是 entry.GetSelfLink().href 给了我一个格式为:docs.google.com/feeds/default/private/full/folder%.... 的链接,当在浏览器中使用时,我得到“无效的请求 URI”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多