如何通过Google Sheets API获取表格图片?现有代码未返回图片
解决Google Sheets API获取表格图片的问题
问题根源
你当前调用spreadsheets().get()仅开启了includeGridData=True,但Google Sheets中的图片(无论浮动图还是嵌入单元格的图)不属于网格数据(gridData)范畴,而是归类为绘图资源(drawings),需要单独指定参数才能获取。
修正方案
方式1:获取目标区域内的所有绘图资源
在请求中添加includeDrawings=True参数,响应会返回表格内的绘图对象,包含图片的位置、访问链接等信息:
import os import pprint import httplib2 from googleapiclient.discovery import build from oauth2client.service_account import ServiceAccountCredentials # 构建授权服务 creds = ServiceAccountCredentials.from_json_keyfile_name( os.path.dirname(__file__) + "/creds/sacc1.json", ['https://www.googleapis.com/auth/spreadsheets'] ) service = build('sheets', 'v4', http=creds.authorize(httplib2.Http())) # 请求表格数据,同时获取绘图资源 response = service.spreadsheets().get( spreadsheetId="17sm8mLGok1WzADZJxaeTCEFaiXj4CFpZk0cIQPYIu5Y", ranges="Под-системы!I14:I14", includeGridData=True, includeDrawings=True # 关键参数:开启绘图数据获取 ).execute() # 打印目标工作表中的绘图资源 pprint.pprint(response['sheets'][0].get('drawings', []))
方式2:定位目标单元格的图片
绘图资源的position字段包含图片的位置信息(索引从0开始),你可以通过rowIndex=13(对应第14行)、columnIndex=8(对应第I列)筛选出目标单元格的图片,然后提取contentUrl字段获取图片的直接访问链接。
内容的提问来源于stack exchange,提问作者Jr Lcawokado
相关产品推荐
相关产品推荐

