You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过API导出Google Spreadsheets单元格内的二进制图片

可行提取方案

K列直接粘贴的图片不属于单元格值,属于单元格嵌入式对象,因此values.get接口无法读取,可选用以下两种方案实现提取:

方案1:直接通过Google Sheets API提取文本+K列图片

该方案不需要下载完整表格,仅拉取需要的字段数据,对大体积表格友好,不会触发大小限制。
需调用spreadsheets.get接口,指定拉取embeddedObject资源字段,示例代码如下:

from googleapiclient.discovery import build
from google.oauth2.service_account import Credentials
import base64

SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
SPREADSHEET_ID = '替换为你的表格ID'
TARGET_RANGE = 'Brands!A2:K'

# 初始化认证
creds = Credentials.from_service_account_file('替换为你的服务账号密钥文件路径.json', scopes=SCOPES)
service = build('sheets', 'v4', credentials=creds)

# 拉取包含嵌入式对象的行数据
result = service.spreadsheets().get(
    spreadsheetId=SPREADSHEET_ID,
    ranges=TARGET_RANGE,
    fields="sheets.data.rowData.values.effectiveValue,sheets.data.rowData.values.embeddedObject"
).execute()

# 解析数据
sheet_data = result.get('sheets', [])[0].get('data', [])[0].get('rowData', [])
for row_index, row in enumerate(sheet_data):
    current_row_data = []
    # 提取A-J列文本数据
    for col_index in range(10):
        cell_val = row.get('values', [])[col_index].get('effectiveValue', '')
        if isinstance(cell_val, dict):
            cell_val = list(cell_val.values())[0]
        current_row_data.append(cell_val)
    # 提取K列嵌入式图片
    k_col_cell = row.get('values', [])[10] if len(row.get('values', [])) > 10 else {}
    if 'embeddedObject' in k_col_cell:
        # 解码base64格式的图片内容并保存
        img_base64 = k_col_cell['embeddedObject']['imageContent']['content']
        img_bytes = base64.b64decode(img_base64)
        save_path = f'row_{row_index+2}_k_image.png'
        with open(save_path, 'wb') as f:
            f.write(img_bytes)
        current_row_data.append(f'图片已保存至{save_path}')
    # 此处可将current_row_data写入csv、数据库等存储介质
    print(current_row_data)

注意:使用该方案需要确保服务账号有对应表格的读取权限,若表格内有浮动图片需额外调整字段拉取规则

方案2:分块导出xlsx后本地解析

Drive API的10M限制仅针对单请求同步导出,开启分块下载后可支持最大GB级文件的导出,导出后可本地匹配文本和图片:

  1. 先用Drive API分块导出完整xlsx文件,示例代码如下:
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
from google.oauth2.service_account import Credentials
import io

SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
SPREADSHEET_ID = '替换为你的表格ID'

creds = Credentials.from_service_account_file('替换为你的服务账号密钥文件路径.json', scopes=SCOPES)
service = build('drive', 'v3', credentials=creds)

request = service.files().export_media(
    fileId=SPREADSHEET_ID,
    mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
with io.FileIO('exported_brands_sheet.xlsx', 'wb') as f:
    # 按10M每块分块下载,避开单请求大小限制
    downloader = MediaIoBaseDownload(f, request, chunksize=10*1024*1024)
    done = False
    while not done:
        status, done = downloader.next_chunk()
        print(f"下载进度:{int(status.progress()*100)}%")
  1. 本地解析xlsx文件:
  • xlsx本质为zip压缩包,解压后xl/media目录下存储了所有嵌入式图片
  • 用openpyxl加载xlsx文件,遍历worksheet._images列表,通过每个图片对象的anchor._from属性获取图片所在的行号、列号,匹配到K列对应行的A-J列文本数据即可完成关联。

内容的提问来源于stack exchange,提问作者sevatster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 22:54:09