Last modified: Jan 28, 2026 By Alexander Williams

Python Read Google Doc from URL Tutorial

You need to get data from a Google Doc into your Python script. A simple URL is not enough. You must use the official Google Docs API. This guide shows you how.

We will cover setup, authentication, and code. You will learn to extract clean text from any Doc you can access.

Prerequisites and Setup

First, ensure you have Python installed. You also need a Google Cloud project. This project gives you API access.

Install the required library. Use pip, the Python package installer.


pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib
    

Next, enable the Google Docs API. Go to the Google Cloud Console. Create a new project or select an existing one.

Navigate to "APIs & Services" > "Library". Search for "Google Docs API". Click "Enable".

Now, create credentials. Go to "APIs & Services" > "Credentials". Click "Create Credentials". Choose "OAuth client ID".

Select "Desktop app" as the application type. Name it something like "Python Doc Reader". Click "Create".

Download the JSON credentials file. Rename it to credentials.json. Place it in your project folder. Never share this file.

Understanding the Google Doc URL

A Google Doc's shareable URL looks like this: https://docs.google.com/document/d/DOCUMENT_ID/edit.

The crucial part is the DOCUMENT_ID. It is the long string of letters and numbers after /d/. Your Python code needs this ID.

You must extract it from the full URL. The API uses the ID, not the full web address.

Authenticating with Google's API

Authentication is a key step. The API needs to know who is making the request. We use OAuth 2.0.

The code below handles the authentication flow. It will open a browser window for you to log in and grant permissions.


from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import os

# Define the scope - what your app can do
SCOPES = ['https://www.googleapis.com/auth/documents.readonly']

def get_authenticated_service():
    """Handles OAuth 2.0 authentication and returns a service object."""
    creds = None
    # token.json stores the user's access and refresh tokens
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())
    return creds
    

Run this function once. It creates a token.json file. This file stores your credentials for future use.

Extracting the Document ID from the URL

You need a function to parse the URL. It finds the Document ID. Here is a simple way to do it.


def extract_document_id(doc_url):
    """Extracts the Google Document ID from its shareable URL."""
    # Common URL patterns
    import re
    patterns = [
        r'/document/d/([a-zA-Z0-9-_]+)',
        r'id=([a-zA-Z0-9-_]+)'
    ]
    for pattern in patterns:
        match = re.search(pattern, doc_url)
        if match:
            return match.group(1)
    raise ValueError("Could not extract Document ID from the provided URL.")
    

This function uses regular expressions. It searches for the ID pattern. It returns the clean ID string.

Reading the Document Content

Now for the main function. We use the get_authenticated_service function for credentials. We use the extract_document_id function for the ID.

We then call the Google Docs API. The documents().get() method fetches the document's structure.


from googleapiclient.discovery import build

def read_google_doc(doc_url):
    """Main function to read and return text from a Google Doc URL."""
    # Step 1: Get authenticated credentials
    creds = get_authenticated_service()
    
    # Step 2: Build the Google Docs API service
    service = build('docs', 'v1', credentials=creds)
    
    # Step 3: Extract the Document ID from the URL
    document_id = extract_document_id(doc_url)
    
    # Step 4: Request the document content from the API
    document = service.documents().get(documentId=document_id).execute()
    
    # Step 5: Extract and concatenate all text elements
    doc_content = document.get('body', {}).get('content', [])
    full_text = []
    for element in doc_content:
        if 'paragraph' in element:
            elements = element.get('paragraph', {}).get('elements', [])
            for elem in elements:
                text_run = elem.get('textRun', {})
                if text_run:
                    full_text.append(text_run.get('content', ''))
    return ''.join(full_text)

# Example usage
if __name__ == '__main__':
    # Replace with your Google Doc's shareable URL
    url = "https://docs.google.com/document/d/1YOUR_DOCUMENT_ID_HERE/edit"
    try:
        text = read_google_doc(url)
        print("Successfully read the document:")
        print(text[:500]) # Print first 500 characters
    except Exception as e:
        print(f"An error occurred: {e}")
    

This function connects all the steps. It authenticates, gets the ID, calls the API, and parses the response.

The API returns a complex JSON structure. Our code navigates to the textRun objects. These contain the actual text.

Example Output

When you run the script, you will see output like this. It shows the first part of your document's text.


Successfully read the document:
This is the title of my Google Doc.

This is the first paragraph. It contains some sample text that we are reading programmatically with Python.

Here is a second paragraph with more information. The API successfully extracted all this text cleanly.
    

The output is plain text. All formatting like bold or italics is removed. You get just the words.

Common Issues and Troubleshooting

You might face some common errors. Here is how to fix them.

Error: "invalid_grant" or "access_denied". Delete the token.json file. Re-run the script to re-authenticate. Ensure you are using the correct Google account.

Error: "Unable to extract Document ID". Double-check your URL. Ensure it is a shareable link with "Anyone with the link" view access at minimum.

The script returns empty text. Your document might be empty. Or the parsing logic might miss certain elements like lists or tables. The provided code handles basic paragraphs.

For more complex needs, explore the full API response. Print the document variable to see all available data.

Conclusion

Reading a Google Doc from a URL with Python is straightforward. It requires the Google Docs API and proper OAuth setup.

The key steps are: enabling the API, getting credentials, extracting the Document ID, and calling the documents().get() method.

This method is powerful for automation. You can pull reports, process notes, or sync content. Remember to handle credentials securely.

You now have a working script. You can adapt it for your projects. Explore the official Google Docs API documentation for advanced features like reading styled text or comments.