Prerequisites:
Assume that I've created a Google service account, and I've a Google document ID doc_ID.
Problem:
When I've only 2 types of elements (nested list and normal text), subsequent to the suggestion of @Tanaike https://stackoverflow.com/a/78541593/25276165, I successfully wrote the contents to a Google document. Now I want to generalize my list to include various other kind of elements. Thereby, my lists looks like the following:
my_list =
[
"# Top-level Heading",
"This is the first normal text line, without any itemized list format.",
"* This is the first outermost list item (number)",
" * This is the first level-one indented list item (alpha)",
"This is the final normal text line, without any itemized list format.",
"# Top-level Heading",
"* This is the second outermost list item (number) under the heading, with new numbering",
" * This is the second level-one indented list item (alpha) under the heading",
"This is the final normal text line, without any itemized list format.",
]
My Attempts:
To cater various types of list elements, my first line of thought is to use a for loop with if conditions, as shown below:
requests = []
for item in my_list:
if item.startswith("# "):
text = item[2:] + '\n'
end_index = start_index + len(text)
item_request = heading_request(text, start_index, end_index)
elif item.startswith("* "):
text = item[2:] + '\n'
end_index = start_index + len(text)
item_request = list_request(text, start_index, end_index)
elif item.startswith(" * "):
text = item[4:] + '\n'
end_index = start_index + len(text)
item_request = list_request(text, start_index, end_index)
else:
text = item + '\n'
end_index = start_index + len(text)
item_request = text_request(text, start_index, end_index)
requests.append(item_request)
start_index = end_index
Here, heading_request, list_request, or text_request are expected to contain all the desired fields and their values. For brevity reasons, I just list a brief hint of the code snippet for list_requests:
{"insertText": {"text": list_text, 'location': {'index': start_list_index}}},
{
"createParagraphBullets": {
'range': {'startIndex': start_list_index, 'endIndex': end_list_index},
"bulletPreset": "NUMBERED_DECIMAL_ALPHA_ROMAN",
}
}
Expected Result
How can this be successfully programmed so that using Google Docs API, I can write to a Google document with desired nested indentation and text types?

indent_first = 18,indent_hang = 36andindent_first = 36,indent_hang = 54are used. But, it seems that in your showing image, the indents of each paragraph are the same for each nest. What areindent_firstandindent_hang?it_request = list_request(text, start_index, end_index, indent_first, indent_hang)? And, in your showing image, it seems that the font size ofTop-level Headinghas changed. But, in your script, it is not reflected. So, I cannot still understand your question. I apologize for my poor English skill. First, I would like to correctly understand your question.indent_firstandindent_hangare the parameters for setting the magnitude values for the fieldsindentFirstLine(where the bullet or number is placed) andindentStart(the space between the bullet/number and the text). These can be set inparagraphStyle. In the image that I attached, the number 1 level hasindent_startat 18, andindent_hangat 36. For thealpha-level nested list, these values are 36 and 54, respectively. Both the nested lists are placed according to these settings.