I’m working on a Python automation script that generates Google Docs content using their API. I can successfully create headers but I’m struggling with adding bullet lists.
My Goal
I want to create a document with this structure:
Header One
• list item
Header Two
• list item
What Works
Creating headers works fine with this approach:
operations = []
current_op = {}
for item in reversed(my_data["items"]):
# Add header text
current_op = {
"insertText": {
"location": {"index": 1},
"text": f"{item['title']}\n"
}
}
operations.append(current_op)
# Style as header
current_op = {
"updateParagraphStyle": {
"range": {"startIndex": 1, "endIndex": len(item['title'])},
"paragraphStyle": {
"namedStyleType": "HEADING_2"
}
}
}
operations.append(current_op)
response = service.documents().batchUpdate(documentId=doc_id, body={'requests': operations}).execute()
The Problem
When I try to add bullets, I get an error about “oneof field ‘request’ is already set”:
list_text = "sample text"
for item in reversed(my_data["items"]):
# Insert bullet text and create bullets in same request
current_op = {
"insertText": {"location": {"index": len(item['title']) + 2}, "text": list_text},
"createParagraphBullets": {
"range": {"startIndex": len(item['title']) + 2, "endIndex": len(list_text) + 2},
"bulletPreset": "BULLET_ARROW_DIAMOND_DISC"
}
}
operations.append(current_op)
I think I’m misunderstanding how to structure the batch requests properly. Should text insertion and bullet formatting be separate operations? Any help would be great!