简体   繁体   English

使用 gspread python 发送到 Google 表格的图像看起来很小

[英]Image sent to Google Sheets with gspread python appears tiny

I need to send image to google sheets using gspread .我需要使用gspread将图像发送到谷歌表格。 I am not finding documentation for a function that will work appropriately for it on docs.gspread.org .我没有在 docs.gspread.org 上找到适用于function的文档。 As shown in my code snippet below, I am currently using the command如下面我的代码片段所示,我目前正在使用该命令

worksheet.append_row(['testpic.png', '=IMAGE(\"{}\")'.format(
    'https://raw.githubusercontent.com/eliasdon/picturetest/main/download.png')], value_input_option='USER_ENTERED')

but the picture that gets appended is very tiny and cannot be resized even manually as shown in the attached picture但是附加的图片非常小,甚至无法手动调整大小,如附图所示

在此处输入图像描述

I found this guide on google sheets我在谷歌表格上找到了这个指南在此处输入图像描述 but I wasn't able to get it to work.但我无法让它工作。 I tried to implement it like so:我试图像这样实现它:

worksheet.append_row(['IMAGE("https://www.google.com/images/srpr/logo3w.png", 4, 50, 100)'], value_input_option='USER_ENTERED')
worksheet.append_row(['IMAGE("https://www.google.com/images/srpr/logo3w.png", 4, 50, 100)'])

Code:代码:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

# google scope
scope = ["https://spreadsheets.google.com/feeds", 'https://www.googleapis.com/auth/spreadsheets',
         "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]

# read credentials from json
creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)

# get access using creds
client = gspread.authorize(creds)

# open sheet
sheet = client.open("test")

# open worksheet witin sheet
worksheet = sheet.worksheet('check')

# append pic
worksheet.append_row(['testpic.png', '=IMAGE(\"{}\")'.format(
    'https://raw.githubusercontent.com/eliasdon/picturetest/main/download.png')], value_input_option='USER_ENTERED')

How do I send picture to google sheets and make it scale either to original size or to a set dimension (x,y)?如何将图片发送到谷歌表格并使其缩放到原始尺寸或设定尺寸(x,y)?

I thought that in your situation, in order to change the size of image, it is required to change the size of the cell.我认为在你的情况下,为了改变图像的大小,需要改变单元格的大小。 When this is reflected to your script, it becomes as follows.当这反映到您的脚本中时,它变成如下。

Modified script:修改后的脚本:

From:从:

worksheet.append_row(['testpic.png', '=IMAGE(\"{}\")'.format(
    'https://raw.githubusercontent.com/eliasdon/picturetest/main/download.png')], value_input_option='USER_ENTERED')

To:至:

res = worksheet.append_row(['testpic.png', '=IMAGE(\"{}\")'.format(
    'https://raw.githubusercontent.com/eliasdon/picturetest/main/download.png')], value_input_option='USER_ENTERED')

width = 300 # The cell width. By this, the width of image is changed.
height = 200 # The cell height. By this, the height of image is changed.
row = int(re.findall("\\w+![A-Z]+([0-9]+)", res['updates']['updatedRange'], re.S)[0])
requests = [{
    "updateDimensionProperties": {
        "properties": {
            "pixelSize": height
        },
        "range": {
            "sheetId": worksheet.id,
            "dimension": "ROWS",
            "startIndex": row - 1,
            "endIndex": row
        },
        "fields": "pixelSize"
    }
},
    {
    "updateDimensionProperties": {
        "properties": {
            "pixelSize": width
        },
        "range": {
            "sheetId": worksheet.id,
            "dimension": "COLUMNS",
            "startIndex": 1,
            "endIndex": 2
        },
        "fields": "pixelSize"
    }
}
]
spreadsheet.batch_update({"requests": requests})
  • In this modification, import re is also used.在此修改中,还使用了import re

  • When you use this script, the cell size of 2nd column of the appended row is changed.当您使用此脚本时,附加行的第 2 列的单元格大小会更改。

References:参考:

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

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