0

I have a Python application using the Google Docs API where I need to perform an operation such as the following. Imagine I have this structure on a document:

1. Text 1
    1.1 Internal text
2. Text 2

I want to insert a text below but inside the bullet of Text 1, and have the resulting structure as:

1. Text 1
    1.1 New text
    1.2 Internal text
2. Text 2

I have tried to use the insertText text request to insert \n\tNew text on the location after Text 1, but it resulted in:

1. Text 1
2.    New text
    2.1 Internal text

I would like to know what I can do to have the desired structure using the Google Docs API. Could anyone tell me how to do that?

0

1 Answer 1

1

Try this:

You can use the insertText from Docs API in inserting text in the sub-bullet. You just need to locate the index of the first bullet line.

Based from your existing document structure you can use the code below

Code:

from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
from google.oauth2 import service_account

#Authentication
#Service Credentials
SERVICE_ACCOUNT_FILE = '/content/credentials.json'
SCOPES = ['https://www.googleapis.com/auth/documents']
creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)

#Document API build
service = build('docs', 'v1', credentials=creds)

# Document Id
document_id = "Document ID here"
new_text = 'New Text'

#Retrieve the current content of the document
document = service.documents().get(documentId=document_id).execute()
#Search for the last index of first line
content_index = document['body']['content'][2]['startIndex']

requests = [
    {
        'insertText': {
            'location': {
                'index': content_index
            },
            'text': new_text + '\n'
        }
    }
]
result = service.documents().batchUpdate(documentId=document_id, body={'requests': requests}).execute()

Note: We may differ on authentication steps since I am using service account in connecting the google docs API

Output:

enter image description here

References:

Inserting text

Google Docs API

Sign up to request clarification or add additional context in comments.

2 Comments

In fact I would like to insert a text with one more level of nesting despite the presence or not of another bullet with the desired level. So, I would like to know how to insert New text even if Internal text doesn't exist. I modified my question to make that clear. But thanks for your solution anyway, it gave me a new option that I will try to adapt to achieve what I want. I clarified my question to make it clearer.
Thank you for replying. I'm glad my solution gave you an option to your issue. I would like to support you. But the issue of your comment is a new document structure, and that is different from your initial question. So can you post it as a new question by including the detailed information? Because when your initial question is changed, other users who see your question are confused. By posting it as a new question, other users including me can try to solve it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.