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

不使用Google API如何用Python访问Google Sheets并逐列读取数据?

无需Google Sheets API的Python读取实现方案

以下是3种可落地的实现方案,适配不同的使用场景:

方案1:公共表CSV导出读取(最稳定)

适用前提:将目标表格权限设置为「所有知道链接的用户可查看」

  • 从你的Google Sheets分享链接中提取表格ID,格式为 https://docs.google.com/spreadsheets/d/[表格ID]/edit 中d/和下一个/之间的字符串
  • 拼接CSV导出链接:如果是默认工作表,格式为 https://docs.google.com/spreadsheets/d/[你的表格ID]/export?format=csv;如果需要指定其他工作表,在链接后追加工表参数 &gid=[工作表ID](工作表ID可从浏览器打开表格时地址栏的gid参数取值)
  • 直接用pandas读取即可,示例代码:
import pandas as pd

# 替换为你拼接好的CSV导出链接
csv_url = "https://docs.google.com/spreadsheets/d/xxx/export?format=csv"
df = pd.read_csv(csv_url)

# 读取第7行第3列:注意pandas索引从0开始,所以行号减1、列号减1
target_value = df.iloc[6, 2]
print(target_value)
# 逐列读取可以用df.iteritems()遍历每一列

方案2:公共表HTML解析读取

适用前提:同方案1,需要表格公开可读
用爬虫解析表格的网页结构提取内容,不需要依赖pandas,示例代码:

import requests
from bs4 import BeautifulSoup

# 替换为你的表格公开查看链接
sheet_url = "https://docs.google.com/spreadsheets/d/xxx/edit"
resp = requests.get(sheet_url + "?output=html")
soup = BeautifulSoup(resp.text, "html.parser")

# 提取所有行
rows = soup.find("table").find_all("tr")
# 取第7行
row_7 = rows[6]
# 取第7行的所有列
cols_7 = row_7.find_all("td")
# 取第3列内容
target_value = cols_7[2].text.strip()
print(target_value)

方案3:私有表Selenium模拟读取

适用前提:表格不便于公开,你自己拥有表格的读取权限
通过模拟浏览器登录Google账号后直接读取页面数据,不需要改动表格权限:

from selenium import webdriver
from selenium.webdriver.common.by import By

# 初始化Chrome驱动(需要提前配置好对应版本的ChromeDriver)
driver = webdriver.Chrome()
# 先登录Google账号(首次运行需要手动完成验证,后续可以保留缓存免登录)
driver.get("https://accounts.google.com/login")
# 手动完成登录后,跳转到目标表格页面
driver.get("https://docs.google.com/spreadsheets/d/xxx/edit")

# 定位第7行第3列的单元格,Google Sheets的单元格行号、列号从1开始计数
target_cell = driver.find_element(By.XPATH, '//tr[7]/td[3]')
target_value = target_cell.text
print(target_value)

driver.quit()

注意事项

  • 上述所有方案中的行号、列号均默认按照用户日常使用的从1开始计数规则,代码中取值时需要对应减去1适配编程索引规则
  • HTML解析和Selenium方案依赖Google Sheets的前端页面结构,如果官方调整页面结构可能需要对应修改定位逻辑
  • 所有操作需要保证网络可以正常访问Google服务

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 18:06:02