Working with Python and Google Docs API to manage folders and documents

Need help with Google Docs API in Python

I’m pretty new to Python coding and working with the Google Docs API. I want to build something that can handle document creation and folder management automatically.

What I’m trying to accomplish:

  1. Check if a specific folder exists, and if not, create it
  2. Add new documents to that folder
  3. Get a complete list of all documents in just that folder with their URLs

I’m working with Google Docs API version 3.0 and using the gdata-2.0.16 library.

My current code:

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

class AppConfig(object):
    APPLICATION_NAME = 'MyDocsManager-v1.0'
    DEBUG_MODE = False

docs_client = gdata.docs.client.DocsClient()
docs_client.ClientLogin('[my_email]','[my_password]', source=AppConfig.APPLICATION_NAME)

folder_resource = gdata.docs.data.Resource(type='folder', title='My Project Folder')
folder_resource = docs_client.CreateResource(folder_resource)

document_resource = gdata.docs.data.Resource(type='document', title='New Document')
document_resource = docs_client.CreateResource(document_resource, collection=folder_resource)

Where I’m stuck:

  1. How can I verify if ‘My Project Folder’ already exists before creating it?
  2. What’s the best way to list only the contents of ‘My Project Folder’?
  3. How do I extract the direct URLs for each document in this specific folder?

I know my code is pretty basic right now, but I’m hoping someone can point me in the right direction. Any tips would be really helpful!

Had this exact problem 6 months ago building a document automation tool at work. The gdata library is deprecated - you should switch to Google Drive API v3 with google-api-python-client. For your current setup though: query existing resources before creating new ones. Use docs_client.GetResources() with a query parameter to search folders by title. Try q='title:"My Project Folder" and type:folder'. Check if your folder exists in the results before making a new one. To list folder contents, use the collection parameter in GetResources and pass your folder’s resource ID. Document URLs are in the GetAlternateLink() method of each resource object - that gives you the readable Google Docs URL. Seriously though, just migrate to the newer API. Google’s documentation is way better and it’s actually supported.

ClientLogin stopped working years ago - Google killed it completely. You’ll need OAuth2 even with the old gdata library. Set up credentials in Google Cloud Console and use oauth2client instead. For checking folders, loop through GetResources() and compare titles manually: existing_folders = [r for r in docs_client.GetResources().entry if r.GetDocumentType() == 'folder' and r.title.text == 'My Project Folder']. Empty list means create the folder. Watch out though - Drive allows duplicate folder names, so you might get multiple matches. Honestly, gdata’s been dead since 2015 and will keep causing problems. Use it for learning if you want, but you’ll need to rewrite with the current Drive API pretty soon.

Just hit this same issue last week. Your gdata approach might work, but it’s gonna be painful since it’s so outdated. For the folder check, try GetResourceBySelfLink() or loop through all resources and filter by title - though multiple folders with the same name will screw you over. The document URLs are in resource.link property, look for the ‘alternate’ rel type. Honestly, save yourself the headache and just use google-api-python-client with Drive API v3. Way less buggy.