复制文档'使用Google文档API的内容

发布于 2025-02-07 14:09:04 字数 611 浏览 3 评论 0 原文

我需要在 Google docs api in Python ,尽管它是文本,图像,表等。我找不到有关使用Google Docs API的内容类型复制内容的任何参考。有人知道如何实施它吗?

例如,假设我想通过复制和粘贴在Google文档中复制以下内容:

Lorem ipsum dolor sit amet,
[some-image]
[some-table]
consectetur adipiscing elit.

我想具有此输出:

Lorem ipsum dolor sit amet,
[some-image]
[some-table]
consectetur adipiscing elit.

Lorem ipsum dolor sit amet,
[some-image]
[some-table]
consectetur adipiscing elit.

I need to copy all content between an initial and a final index in Google Docs API in Python, despite if it is text, image, table, etc. I couldn't find any reference about copying contents regardless of the content type using Google Docs API. Does anyone know how can I implement it?

For example, suppose I want to duplicate the content below inside a Google Doc by copy and paste:

Lorem ipsum dolor sit amet,
[some-image]
[some-table]
consectetur adipiscing elit.

I want to have this output:

Lorem ipsum dolor sit amet,
[some-image]
[some-table]
consectetur adipiscing elit.

Lorem ipsum dolor sit amet,
[some-image]
[some-table]
consectetur adipiscing elit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半暖夏伤 2025-02-14 14:09:04

我已经使用2个不同的选项来完成此操作:

方法1

复制完整的Google文档,您可以使用 files()。复制驱动器API的方法。类似的内容:

copy_title = 'Copy Title'
body = {
    'name': copy_title
}
drive_response = drive_service.files().copy(
    fileId=document_id, body=body).execute()
document_copy_id = drive_response.get('id')

您可以在答案中查看并找到示例代码。 -no-copy-attribute“>此帖子。

注意:您可以使用此方法通过制作文件的完整副本来复制文档的一部分,然后编辑新的副本该文档在保持原始一个未修改的同时。您可以使用 batchupdate 为此

方法2

您可以使用文档.get 在初始索引和最终索引之间获取内容的方法。类似 this

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

        # Retrieve the documents contents from the Docs service.
        document = service.documents().get(documentId=DOCUMENT_ID).execute()

注意:注意:如果您使用documents.get。文档的原始数据。您将需要搜索“ body”>“ body” 文档。在那里,您将看到文档的内容,其中包括文档的各个首字母和最终索引。之后,您还可以使用 batchupdate 将信息粘贴到新文档或与此情况的同一文档中。

参考:

  • 复制现有文档。
  • href =“ https://developers.google.com/docs/api/reference/rest/rest/v1/documents/get”
  • documents.batchupdate.
  • ://developers.google.com/docs/api/quickstart/python“ rel =“ nofollow noreferrer”> document.get.get
  • document -copy-a-a-google-doc-using-google-docs-api-no-no-copy-attribute> stackoverflow中的另一个问题

I have worked with 2 different options to do this:

Method 1

To copy a complete Google Doc, you can use the files().copy method of Drive API. Something like this:

copy_title = 'Copy Title'
body = {
    'name': copy_title
}
drive_response = drive_service.files().copy(
    fileId=document_id, body=body).execute()
document_copy_id = drive_response.get('id')

You can review and find a sample code in the answer to this post.

Note: You can use this method to copy a part of the document by making a complete copy of the file, and after that, edit the new copy of the document while keeping the original one unmodified. You can use batchUpdate for that

Method 2

You can use the documents.get method to get the content between an initial and a final index. Something like this:

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

        # Retrieve the documents contents from the Docs service.
        document = service.documents().get(documentId=DOCUMENT_ID).execute()

Note: If you use the documents.get you will get the raw data of the document. You will need to search for the "body" of the document. There you will see the content of the document with the parts of the document with their respective initial and a final index. After that, you can also use a batchUpdate to paste the information into a new document or the same document as on this case.

Reference:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文