Background
I have a Python project where I need to insert different types of content into a Google Doc. I already have my service account set up and the document ID ready to use.
What I’m trying to do
I want to take a list that has regular text mixed with different levels of bullet points and put it into a Google Doc. Here’s what my data looks like:
content_array = [
"Regular paragraph text goes here first.",
"• Top level bullet point one",
"• Top level bullet point two",
" • Second level item A",
" • Third level sub-item i",
" • Third level sub-item ii",
" • Second level item B",
"Another regular paragraph in between.",
"• One more top level bullet",
]
I want this to show up in the document as normal paragraphs mixed with properly formatted numbered lists that have the right indentation levels.
My current approach
I tried processing the list by detecting the bullet markers and converting them:
processed_text = ""
for line in content_array:
if line.startswith('• '):
processed_text += line[2:] + '\n'
elif line.startswith(' • '):
processed_text += '\t' + line[4:] + '\n'
elif line.startswith(' • '):
processed_text += '\t\t' + line[6:] + '\n'
else:
regular_text = line + '\n'
processed_text += regular_text
end_pos = start_pos + len(processed_text) + 1
start_pos_regular = end_pos - len(regular_text)
final_end = start_pos + len(processed_text) + 1
update_requests = [
{
"insertText": {
"text": processed_text,
'location': {'index': start_pos},
}
},
{
"createParagraphBullets": {
'range': {
'startIndex': start_pos,
'endIndex': final_end,
},
"bulletPreset": "NUMBERED_DECIMAL_ALPHA_ROMAN",
}
},
{
"deleteParagraphBullets": {
'range': {
'startIndex': start_pos_regular,
'endIndex': end_pos,
},
}
},
]
try:
docs_service.documents().batchUpdate(documentId=document_id, body={'requests': update_requests}).execute()
except HttpError as err:
print(f"Error occurred: {err}")
The problem is that this applies bullet formatting to everything, including the regular paragraphs. I need a way to only apply the list formatting to the actual list items while keeping the normal text as regular paragraphs. How can I fix this?