【问题标题】:How to read .doc and .docx files through the google drive link?如何通过 google drive 链接读取 .doc 和 .docx 文件?
【发布时间】:2022-08-17 15:58:27
【问题描述】:

我想通过 google drive 的链接阅读 doc 和 docx 文件。我曾尝试使用 google drive API,但无法读取。它只是显示驱动器中的所有 docx 文件。我可以在本地读取文件,但不能通过链接。

谷歌驱动API的代码。

from __future__ import print_function

import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def search_file():
    \"\"\"Search file in drive location

    Load pre-authorized user credentials from the environment.
    TODO(developer) - See https://developers.google.com/identity
    for guides on implementing OAuth2 for the application.
    \"\"\"
    creds, _ = google.auth.default()

    try:
        # create drive api client
        service = build(\'drive\', \'v3\', credentials=creds)
        files = []
        page_token = None
        while True:
            # pylint: disable=maybe-no-member
            response = service.files().list(q=\"mimeType=\'text/plain\'\" and \"mimeType=\'application/vnd.openxmlformats-officedocument.wordprocessingml.document\'\",
                                            spaces=\'drive\',
                                            fields=\'nextPageToken, \'
                                                   \'files(id, name)\',
                                            pageToken=page_token).execute()
            for file in response.get(\'files\', []):
                # Process change
                print(F\'Found file: {file.get(\"name\")}, {file.get(\"id\")}\')
            files.extend(response.get(\'files\', []))
            page_token = response.get(\'nextPageToken\', None)
            print(files)
            if page_token is None:
                break

    except HttpError as error:
        print(F\'An error occurred: {error}\')
        files = None

    return files


if __name__ == \'__main__\':
    search_file()

那么如何阅读 doc 和 docx 文件呢?

    标签: python docx doc


    【解决方案1】:

    我认为您必须提供文档 ID 才能阅读特定文档。我希望你没有浏览过文档。如果没有,请阅读下面的文档,它可能会给你一些想法。

    https://developers.google.com/docs/api/quickstart/python

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      相关资源
      最近更新 更多