简体   繁体   English

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

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

I want to add images in my google doc from my local system using google docs API but I read that it only accepts images from http servers我想使用 google docs API 从我的本地系统在我的 google doc 中添加图像,但我读到它只接受来自http servers的图像

I tried uploading the image on google drive then used the drive image url in my code but it was throwing an error.我尝试在谷歌驱动器上上传图像,然后在我的代码中使用驱动器图像 url 但它抛出了一个错误。

I don't want to allow the Internet to access the image from my local system as it won't be secure.我不想让 Internet 从我的本地系统访问图像,因为它不安全。

Is there any other way to add images to google docs from my local?有没有其他方法可以从我的本地将图像添加到谷歌文档?

sample code:示例代码:

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

You can follow this steps:您可以按照以下步骤操作:

  1. Upload the image to drive.将图像上传到驱动器。
  2. Double click on it.双击它。 And go to Get-Link和 go 到Get-Link
  3. Set Anyone with the link as Viewer.Anyone with the link的任何人设置为查看者。 and copy the file ID.并复制文件 ID。 For example: https://drive.google.com/file/d/FILE_ID/view?usp=sharing例如: https://drive.google.com/file/d/FILE_ID/view?usp=sharing
  4. Go to your python script and add this as an uri Go 到您的 python 脚本并将其添加为 uri
"uri":"https://drive.google.com/uc?export=view&id=FILE_ID",
  1. Run the function that adds the image.运行添加图像的 function。

This can also be achievable by uploading the file by Drive API and changing the permissions on the upload request.这也可以通过Drive API上传文件并更改上传请求的权限来实现。

Refer the following for more documentation:有关更多文档,请参阅以下内容:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM