0

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

expected formatting in Google docs

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?

17
  • In order to correctly understand your question, can you provide your expected result? Commented May 28, 2024 at 23:03
  • @Tanaike: Thank you Sir. I've updated the question with expected result (and some minor formatting). Looking forward to your guidance/help. Commented May 29, 2024 at 6:10
  • Thank you for replying. In your script, indent_first = 18, indent_hang = 36 and indent_first = 36, indent_hang = 54 are used. But, it seems that in your showing image, the indents of each paragraph are the same for each nest. What are indent_first and indent_hang? Commented May 29, 2024 at 6:18
  • Also, what is it_request = list_request(text, start_index, end_index, indent_first, indent_hang)? And, in your showing image, it seems that the font size of Top-level Heading has 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. Commented May 29, 2024 at 6:18
  • @Tanaike: Thank you very much for your prompt response Sir. indent_first and indent_hang are the parameters for setting the magnitude values for the fields indentFirstLine (where the bullet or number is placed) and indentStart (the space between the bullet/number and the text). These can be set in paragraphStyle. In the image that I attached, the number 1 level has indent_start at 18, and indent_hang at 36. For the alpha-level nested list, these values are 36 and 54, respectively. Both the nested lists are placed according to these settings. Commented May 29, 2024 at 6:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.