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 /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 =
[
"This is the normal text line, without any itemized list format.",
"* This is the outermost list item (number)",
" * This is a first-level indented list item (alpha)",
"# Top-level Heading",
"* This is an outermost list item (number) under the heading, with new numbering",
" * This is a first-level 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("# "): # This is a heading
text = item[2:] + 'n'
end_index = start_index + len(text)
it_request = heading_request(text, start_index, end_index)
elif item.startswith("* "): # This is a bullet point
text = item[2:] + 'n'
end_index = start_index + len(text)
indent_first = 18
indent_hang = 36
it_request = list_request(text, start_index, end_index, indent_first, indent_hang)
elif item.startswith(" * "): # This is a first-level nested-indented item
text = item[4:] + 'n'
end_index = start_index + len(text)
indent_first = 36
indent_hang = 54
it_request = list_request(text, start_index, end_index, indent_first, indent_hang)
else: # This is normal text
text = item + 'n'
end_index = start_index + len(text)
it_request = text_request(text, start_index, end_index)
requests.append(it_request)
start_index = end_index
Here, heading_request, line_request, or text_request are expected to contain all the desired fields and their values. These are not included here for brevity reasons.
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?
Ait Paca is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.