Modifying document names using Google Docs API

I’ve been struggling with the Google Docs API. I want to change the name of a document but I can’t figure out how. I’ve looked at different methods like Create and batchUpdate. These let me change the content inside the doc but not the title.

Here’s what I’ve tried:

doc_service = build('docs', 'v1', credentials=creds)

requests = [
    {
        'insertText': {
            'location': {'index': 1},
            'text': 'New content'
        }
    }
]

doc_service.documents().batchUpdate(documentId=DOC_ID, body={'requests': requests}).execute()

This updates the document content but not the title. Is there a special method for changing document names? I have the document ID already. Any help would be great!

You’re on the right track with using the Google Docs API, but you’re missing a key point. The document title isn’t actually part of the document content - it’s a property of the file in Google Drive. To change the title, you need to use the Google Drive API instead.

Here’s a quick example of how you can do it:

from googleapiclient.discovery import build

drive_service = build('drive', 'v3', credentials=creds)

file_metadata = {'name': 'New Document Title'}
drive_service.files().update(fileId=DOC_ID, body=file_metadata).execute()

This should update the document title without affecting its contents. Remember to set up the proper scopes and authentication for the Drive API as well. Hope this helps solve your issue!

Hey there! I’ve been working with the Google Docs API for a while now, and I can totally relate to your frustration. It’s not immediately obvious how to change document names.

The thing is, document titles are actually managed by the Drive API, not the Docs API. It’s a bit counterintuitive, but once you know this, it’s pretty straightforward.

Here’s what I do:

drive_service = build('drive', 'v3', credentials=creds)
file_metadata = {'name': 'Your New Title Here'}
updated_file = drive_service.files().update(fileId=DOC_ID, body=file_metadata).execute()

Just make sure you’ve got the right permissions set up for the Drive API. Also, keep in mind that this won’t affect the content of your document, just the file name in Drive.

One last tip: if you’re doing this frequently, you might want to create a helper function to make it easier. Good luck with your project!

oh yeah, i ran into this too. the docs API is kinda weird - you gotta use the drive API to change the name. it’s like:

drive_service = build(‘drive’, ‘v3’, credentials=creds)
file_stuff = {‘name’: ‘new title here’}
drive_service.files().update(fileId=DOC_ID, body=file_stuff).execute()

hope that helps! lemme know if u need anything else