I have a document on Google Docs, for example: https://docs.google.com/document/d/1U_Be02ViQp0UKPYNQo184ks4Vv-ey8mNoawJtTyC1R8
I want to rename a specific version of the document to some name like "Version 1.0 - 20240101". I want to do it programmatically using Python/JS/... or REST API.
Rename only latest version is also ok.
Please help!
This is code to get all revisions of a document but I want to edit and rename a revision. Just keep it here to let you start faster.
import os
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import urllib.request
def get_revision_history(fileId):
# don't commit your token file to the repo
KEY_FILE = 'token.json'
DOWNLOAD_DIR = 'download'
curDir = os.path.join(DOWNLOAD_DIR, fileId)
if not os.path.isdir(curDir):
os.mkdir(curDir)
# authenticate with the Google APIs using your service account
credentials = service_account.Credentials.from_service_account_file(
KEY_FILE,
scopes=['https://www.googleapis.com/auth/documents',
'https://www.googleapis.com/auth/drive']
)
# initialize the Google Drive API client, only v2 works (not v3)
drive_service = build('drive', 'v2', credentials=credentials)
try:
# get the revision history of the Google Docs
revisions = drive_service.revisions().list(fileId=fileId).execute()
print(f"{fileId}: {len(revisions['items'])} revisions found")
except HttpError as error:
print(f"An error occurred: {error}")
raise error
return None
if __name__ == '__main__':
# fileId = # it's in the Google Docs link, https://docs.google.com/document/d/fileId/
fileId = "1U_Be02ViQp0UKPYNQo184ks4Vv-ey8mNoawJtTyC1R8"
get_revision_history(fileId)
I couldn't find API to rename a version.
rename a specific version of the document to some nameis currently not possible with the Google Drive API. Both Service: googleapis.com/drive/v2 and Service: googleapis.com/drive/v3 are able to access version histories. However, they lack the functionality to allow individuals to rename them.