如何使用 Google 文档 API 和 Python 将本地图像添加到 google 文档?

How to add local image to google docs using Google docs API and Python?

我想使用 google 文档 API 从本地系统向我的 google 文档添加图像,但我了解到它只接受来自 http servers[= 的图像17=]

我尝试将图像上传到 google 驱动器,然后在我的代码中使用了驱动器图像 url,但出现错误。

我不想让 Internet 从我的本地系统访问图像,因为它不安全。

是否有任何其他方法可以从本地向 google 文档添加图像?

示例代码 ():

{
            "insertInlineImage": {
                "location": {
                    "segmentId": header_id,
                    "index": 0
                },
                "uri": "https://Whosebug.design/assets/img/logos/so/logo-Whosebug.png",
                # This is a sample image.
                "objectSize": {
                    "width": {
                        "magnitude": 100,
                        "unit": "PT"
                    }
                }
            }
        }

您可以按照以下步骤操作:

  1. 上传图片到驱动器。
  2. 双击它。然后转到 Get-Link
  3. Anyone with the link 设置为查看者。并复制文件 ID。例如:https://drive.google.com/file/d/FILE_ID/view?usp=sharing
  4. 转到您的 python 脚本并将其添加为 uri
"uri":"https://drive.google.com/uc?export=view&id=FILE_ID",
  1. 运行添加图片的函数

这也可以通过 Drive API 上传文件并更改上传请求的权限来实现。

更多文档请参考以下内容: