在Google Drive上使用Google文档byapi

问题描述 投票:0回答:1

doc_metadata = { 'name': 'docs_name', 'parents': [f'{folder_id}'], 'mimeType': 'application/vnd.google-apps.document' } try: doc = drive_service.files().create(body=doc_metadata).execute() documentId = doc.get("id") except HttpError as error: print(f'An error occurred: {error}')

try:

#Authentication SCOPES = ['https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/documents'] doc_metadata = { 'name': 'docs_name', 'parents': [f'{folder_id}'], #FolderID 'mimeType': 'application/vnd.google-apps.document' } #Service Builder service_docs = build('docs', 'v1', credentials=creds) service_drive = build('drive', 'v3', credentials=creds) #Creating document file using the drive API try: doc = service_drive.files().create(body=doc_metadata).execute() DOCUMENT_ID = doc.get("id") except HttpError as error: print(f'An error occurred: {error}') new_text = 'This is a sample sentence for the newly created document' requests = [ { 'insertText': { 'location': { 'index': 1, }, 'text': new_text } } ] #Inserting text using insertText request from Google docs API result = service_docs.documents().batchUpdate( documentId = DOCUMENT_ID, body={'requests': requests}).execute()
要将文本插入文档,使用
python google-drive-api google-docs google-docs-api
1个回答
0
投票
方法,并将文本和位置作为有效载荷包含一个。

,您需要

驱动API才能创建文档
和Docs API来编辑文档。

参考:
ininsert,删除和移动文本

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.