Search code examples
pythongoogle-apigoogle-slides-api

How to remove highlight in the google slides API?


I am using the Google API Python Client to replace text placeholders with generated data. In this example, I detect all instances of "bar" and replace them with "foo", in all slides. slides_service is instantiated with apiclient.discovery.build(...)

batch_requests_array = [
            {
                "replaceAllText": {
                    "replaceText": "foo",
                    "containsText": {
                        "text": "bar",
                        "matchCase": False
                    }
                }
            }
        ]

        batch_requests = {"requests": batch_requests_array}
        request = slides_service.presentations().batchUpdate(presentationId=slides_id, body=batch_requests)
        res = request.execute()

Now if bar has a highlight color, how can I remove that when I replace it with foo? I think I need to add a separate request to my batch requests array, but I have been scrolling up and down here without finding any clue.

For clarity, this is the highlight option I am talking about as it's presented in the UI enter image description here


Solution

  • To remove the highlight color of a text, you will have to update the backgroundColor. To give you an idea, this is how the request should look, this will set the background fully transparent:

    highlightedTextRequest = [
        {
            "updateTextStyle": {
                "objectId": "objectId",
                "style": {
                    "backgroundColor": {
                    }
                },              
            "fields": "*"
            }
        }
    ]
    

    Note: The objectId in the request, is the ID of the shape or table with the text to be styled.

    References: