如何使用Google docs API Python检索节ID
例如,我们有这样一个文档- 目录如何使用Google docs API Python检索节ID,python,google-docs-api,Python,Google Docs Api,例如,我们有这样一个文档- 目录 导言 1.1背景 1.2问题陈述 接近 2.1.1算法概述 我需要在文档中的所有文本中匹配一个“字符串”。例如,我的搜索字符串可以是“REQ-”。可将“REQ-1”、“REQ-2”与“REQ-10”匹配 假设“REQ-1”位于text:C中,“REQ-2”位于text:E中,那么我要查找的输出是 (“要求-1”、“1.2”)、(“要求-2”、“2.1.1”)等 本质上,它匹配搜索字符串,标识所有匹配项,对于每个匹配项,返回匹配字符串的2元组和包含匹配字
def get_creds():
credentials = service_account.Credentials.from_service_account_file(
"cred_new.json", scopes=SCOPES
)
return credentials
def search_paragraph_element(element, search_str):
text_run = element.get('textRun')
if not text_run:
return False
res = text_run.get('content').find(search_str)
if res != -1:
return True
return False
def search_structural_elements(elements, search_str):
text = ''
hd_1 = 0
hd_2 = 0
hd_3 = 0
for value in elements:
if 'paragraph' in value:
if value['paragraph']['paragraphStyle']['namedStyleType'] == 'HEADING_1':
hd_1 = hd_1 + 1
hd_2 = 0
hd_3 = 0
elif value['paragraph']['paragraphStyle']['namedStyleType'] == 'HEADING_2':
hd_2 = hd_2 + 1
hd_3 = 0
elif value['paragraph']['paragraphStyle']['namedStyleType'] == 'HEADING_3':
hd_3 = hd_3 + 1
elements = value.get('paragraph').get('elements')
for elem in elements:
res = search_paragraph_element(elem, search_str)
if res is True:
return str(hd_1) + '.' + str(hd_2) + '.' + str(hd_3)
return text
def main():
"""Uses the Docs API to print out the text of a document."""
credentials = get_creds()
service = build("docs", "v1", credentials=credentials).documents()
properties = service.get(documentId=REQ_DOCUMENT_ID).execute()
doc_content = properties.get('body').get('content')
print(search_structural_elements(doc_content, "MySearchString"))
if __name__ == '__main__':
main()
``
你有正在编写的代码吗?大家好,已经添加了代码。你能解释一下你当前的代码在做什么吗?你有什么具体问题吗?@lamblichus谢谢你的跟进。假设我有一个结构元素(使用googledocspythonapi),我想提取到包含该元素的部分的“链接”,并以字符串的形式获取链接。我该怎么做?你能提供一份样本文件以确保我们在同一页上吗?