【问题标题】:How to extract or read Image from Google Doc using Python如何使用 Python 从 Google Doc 中提取或读取图像
【发布时间】:2020-10-21 09:54:11
【问题描述】:

我正在尝试从我的谷歌文档中读取数据。所以我现在正在使用python,我已经实现了Google Docs API 并使用了python。我只是复制粘贴了谷歌提供的代码并进行了一些修改,我成功地读取了数据LINE BY LINE,但是TEXT ONLY!现在我正在尝试一些新的东西并插入了一张图片。这是它的样子。

Google Doc Link

非常简单对...它有一个项目符号和包含图像和“你好”文本的子项目符号。现在,当我读取数据(它逐行读取)时,我尝试打印出 API 返回的内容,它再次返回包含 dictionariesdictionary。这是它的样子。

{'startIndex': 1, 'endIndex': 41, 'paragraph': {'elements': [{'startIndex': 1, 'endIndex': 41, 'textRun': {'content': 'This is the Python Programming Language\n', 'textStyle': {}}}], 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT', 'direction': 'LEFT_TO_RIGHT', 'indentFirstLine': {'magnitude': 18, 'unit': 'PT'}, 'indentStart': {'magnitude': 36, 'unit': 'PT'}}, 'bullet': {'listId': 'kix.y7w314ij0ywy', 'textStyle': {'underline': False}}}}


{'startIndex': 41, 'endIndex': 43, 'paragraph': {'elements': [{'startIndex': 41, 'endIndex': 42, 'inlineObjectElement': {'inlineObjectId': 'kix.o4cuh6wash2n', 'textStyle': {}}}, {'startIndex': 42, 'endIndex': 43, 'textRun': {'content': '\n', 'textStyle': {}}}], 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT', 'direction': 'LEFT_TO_RIGHT', 'indentFirstLine': {'magnitude': 54, 'unit': 'PT'}, 'indentStart': {'magnitude': 72, 'unit': 'PT'}}, 'bullet': {'listId': 'kix.y7w314ij0ywy', 'nestingLevel': 1, 'textStyle': {'underline': False}}}}


{'startIndex': 43, 'endIndex': 49, 'paragraph': {'elements': [{'startIndex': 43, 'endIndex': 49, 'textRun': {'content': 'Hello\n', 'textStyle': {}}}], 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT', 'direction': 'LEFT_TO_RIGHT', 'indentFirstLine': {'magnitude': 54, 'unit': 'PT'}, 'indentStart': {'magnitude': 72, 'unit': 'PT'}}, 'bullet': {'listId': 'kix.y7w314ij0ywy', 'nestingLevel': 1, 'textStyle': {'underline': False}}}}

如您所见,有 3 个字典包含它们的 keyvalue 对。请注意,这三个是文档中的每一行。正如您还可以观察到的那样,键 content 和它的 value(s) 是文档中的文本。

如果您查看嵌套字典,它就是这些:

{'content': 'This is the Python Programming Language\n', 'textStyle': {}}
{'content': '\n', 'textStyle': {}}
{'content': 'Hello\n', 'textStyle': {}}

现在我注意到它为图像包含的行返回了一个\n。此外,我已经寻找至少它可能有一个key,它的值将是图像的临时 url,但它似乎没有。所以我的问题是有没有办法使用我正在使用的这个 API 以某种方式读取这个图像(也可以提取它)?可能我只是错过了一些东西......有人可以帮我吗?任何其他替代解决方案将不胜感激!谢谢!

顺便说一下,这里是 google 提供的源代码,我已经对 read_strucutural_elements 函数进行了修改,说明它将如何为我的个人目的读取数据,但正如您所看到的那样,这就是 API 返回时的工作方式每行数据的字典。我还注意到 API 确实以某种方式确实逐行读取并返回了 dictionary

def main():
    """Shows basic usage of the Docs API.
    Prints the title of a sample document.
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    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(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

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

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

    #print('The title of the document is: {}'.format(document.get('title')))
    data = read_strucutural_elements(document.get("body").get("content"))

这是read_strucutural_elements 函数,我只是打印出elements 参数中的元素,其中该参数逐行包含这些数据。

def read_strucutural_elements(elements):

    for value in elements:
        print(value) #the value of the value variable is the nested dictionaries I've shown above
        print()

非常感谢!

【问题讨论】:

  • 欢迎来到 SO!也许您可以提供指向公开共享的 google 文档的链接而不是图片。
  • 您好!好确定!就一秒
  • 完成! :) 我已经添加了链接示例文档。
  • 好的,很好,现在人们可以直接在上面测试代码了。请注意,链接在到达 google 文档之前通过 facebook,您可能希望避免重定向...
  • 完成:) 谢谢!我从共享链接本身的链接更改了它。我希望你们能帮助我。谢谢!

标签: python python-3.x api google-api google-docs


【解决方案1】:

查看字典输出,图像是具有特定 id 的 inlineObject。您应该能够使用其 url 检索图像。要获取网址,请参阅相关问题:How to get the url to Google doc image

【讨论】:

  • 谢谢,我开始了,是的,它确实通过您提供的链接回答了我的问题,但是与那里提出的问题相比,我的问题有所不同。但是,是的,这也是相同的解决方案!谢谢
猜你喜欢
  • 2013-01-21
  • 2020-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-06
相关资源
最近更新 更多