简体   繁体   English

如何使用 python 和 Google Docs API 获取创建的文件的 ID?

[英]How would I get the ID of a created file using python and the Google Docs API?

I am writing a simple python program to collect information and format it into a google doc.我正在编写一个简单的 python 程序来收集信息并将其格式化为谷歌文档。 I want the program to create the document, then add text to it.我希望程序创建文档,然后向其中添加文本。 I can create a program easily, and I've had no trouble adding text to existing documents.我可以轻松地创建一个程序,而且我在向现有文档中添加文本没有任何问题。 However, I can't access the ID of the created document within the program, so I can't add text to it.但是,我无法在程序中访问所创建文档的 ID,因此无法向其中添加文本。 How do I get the ID of a document that I've created within the same program I want to edit it in?如何获取我在要编辑的同一程序中创建的文档的 ID?

Follow steps 1 and 2 as documented on the following link:按照以下链接中的说明执行步骤 1 和 2:
https://developers.google.com/docs/api/quickstart/python https://developers.google.com/docs/api/quickstart/python

And then try the following code:然后尝试以下代码:

import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
SCOPES = ['https://www.googleapis.com/auth/documents']
creds = None

if os.path.exists('token.pickle'):
  with open('token.pickle', 'rb') as token:
    creds = pickle.load(token)

# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
  if creds and creds.expired and creds.refresh_token:
    creds.refresh(Request())
  else:
    flow = InstalledAppFlow.from_client_secrets_file('/content/credentials.json', SCOPES)
    #creds = flow.run_local_server(port=0)
    creds = flow.run_console()
  # Save the credentials for the next run
  with open('token.pickle', 'wb') as token:
      pickle.dump(creds, token)

service = build('docs', 'v1', credentials=creds)

title = 'My Test Document'
body = {
    'title': title
}
doc = service.documents().create(body=body).execute()
docId = doc.get('documentId')
print( 'Id of new Document is : ' + docId )


NOTE: Place the file credentials.json at appropriate location and set the path in code accordingly.注意:将文件credentials.json放在适当的位置并相应地在代码中设置路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 google api 和 python 在 google docs 中打开文件? - How do I open a file in google docs using google api and python? 如何使用google docs api更新google doc(Python) - How to update google doc using google docs api (Python) 如何使用谷歌文档 API 和 Python 将本地图像添加到谷歌文档? - How to add local image to google docs using Google docs API and Python? 我们如何使用谷歌文档 API 和 python 为谷歌文档中的页脚应用“不同的首页” - How can we apply "different first page" for footers in the google docs using google docs API and python 如何使用 Python API 在 Google Docs 中找到子字符串的 startIndex - How can I find the startIndex of a substring in a Google Docs using the the Python API 文件未出现在 Google 云端硬盘中 - 使用 Python 创建的 Google 文档 - File does not appear in Google Drive - Google docs created with Python 谷歌文档 API 和 Python - Google Docs API with Python 如何使用带有 Python 的 ID 获取 Google Drive 上文件的 url - How to get the url of a file on Google Drive using its ID with Python 如何使用mailchimp3 python API获取mailchimp中新创建列表的列表ID - how to get list id of newly created list in mailchimp using mailchimp3 python api 如何使用Python访问Google文档 - How to access Google Docs using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM