Предварительные условия:
Предположим, что я создал учетную запись службы Google и получил учетные данные. (JSON).
Я создал документ Google, и у меня есть его doc_ID.
Мой язык программирования — Python.
Проблема:
Предположим, что у меня есть список элементов смешанного типа, например. (обычный текст, несколько вложенных элементов списка, обычный текст, еще один вложенный элемент списка) и т. д. См. пример ниже:
Код: Выделить всё
my_list = [
"This is the normal text line, without any itemized list format.",
"* This is the outermost list item (number)",
"* This is another outermost list item (number)",
" * This is a first-level indented list item (alpha)",
" * This is a second-level indented list item (roman)",
" * This is another second-level indented list item (roman)",
" * This is another first-level indented list item (alpha)",
"This is the normal text line, without any itemized list format.",
"* This is one more outermost list item (number)",
]
Я попробовал несколько подходов и получил максимально близкий результат с предложением @tanaike в следующем сообщении.
Как сделать отступ в маркированном списке с помощью Google Docs API
Вот мой пример кода:
Код: Выделить всё
text_insert = ""
for text in my_list:
if text.startswith('* '):
text_insert += text[2:] + '\n'
elif text.startswith(' * '):
text_insert += '\t' + text[4:] + '\n'
elif text.startswith(' * '):
text_insert += '\t\t' + text[6:] + '\n'
else:
text_normal = text + '\n'
text_insert += text_normal
end_index_normal = start_index + len(text_insert) + 1 - indent_corr
start_index_normal = end_index_normal - len(text_normal)
end_index = start_index + len(text_insert) + 1
indented_requests = [
{
"insertText": {
"text": text_insert,
'location': {'index': start_index},
}
},
{
"createParagraphBullets": {
'range': {
'startIndex': start_index,
'endIndex': end_index, # Add 2 for the newline character
},
"bulletPreset": "NUMBERED_DECIMAL_ALPHA_ROMAN",
}
},
{
"deleteParagraphBullets": {
'range': {
'startIndex': start_index_normal,
'endIndex': end_index_normal,
},
}
},
]
try:
u_service.documents().batchUpdate(documentId=doc_id, body={'requests': indented_requests}).execute()
except HttpError as error:
print(f"An error occurred: {error}")
введите здесь описание изображения
Однако моя цель состоит в следующем (получено после редактирования вручную):
введите сюда описание изображения
Как этого добиться? Любая помощь будет принята с благодарностью.
Подробнее здесь: https://stackoverflow.com/questions/785 ... t-using-go