【问题标题】:How do I insert images into a Google Sheet in Python using gspread (or any other package)?如何使用 gspread(或任何其他包)将图像插入 Python 中的 Google 表格?
【发布时间】:2020-07-27 13:44:28
【问题描述】:

我需要一种将图像插入特定 Google 表格中特定单元格的方法。如果有任何 python 包和/或代码片段可以实现这一点,请告诉我。据我所知,Gspread 没有任何帮助。

【问题讨论】:

  • 您能否提供更多代码的详细信息以及哪些代码不适合您?要提供更多详细信息,您可以查看本指南 - stackoverflow.com/help/how-to-ask
  • 对不起,如果我让您感到困惑,我的代码没有问题。我想编写一段新的代码,将图像添加到单元格中。但是,我不知道该怎么做,这就是我问的原因。我有一个 Google Sheets API 设置和工作,如果我现在需要一种插入图像的方法。

标签: image google-sheets gspread


【解决方案1】:

总体而言,将图像添加到 Google 表格中可以使用 Python 完成,方法是首先使用 Google Drive API 将图像文件插入 Google Drive:

这里是如何将其插入谷歌驱动器。 -https://developers.google.com/drive/v2/reference/files/insert#examples,

其次,使用 Google Sheets API 链接到 Google Sheets 中的图片,这里是 Google Sheets API - https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#CellData

注意:在 CellData 下查看

【讨论】:

    【解决方案2】:

    您可以使用如下公式从 URL 中简单地插入图像:

    '=image("https://drive.google.com/uc?export=view&id={}")'.format(file_id)
    

    当您插入/上传数据时,请务必选择 USER_ENTERED 作为值输入选项。 https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption

    使用 gspred:

    sheet_instance.insert_rows(rows, value_input_option='USER_ENTERED')
    

    【讨论】:

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