如何使用 Google API 和身份验证/服务帐户从 https://docs.google.com/spreadsheets 下载文件?

问题描述 投票:0回答:1

我想每天从

https://docs.google.com/spreadsheets
帐户(服务帐户)自动下载文件。

我有一个 cred.json 文件,其中包含:

{
  "type": "service_account",
  "project_id": "id_1234",
  "private_key_id": "12345678901234567890",
  "private_key": "-----BEGIN PRIVATE KEY-----\n1234567890\n-----END PRIVATE KEY-----\n",
  "client_email": "id_1234@id_1234.iam.gserviceaccount.com",
  "client_id": "1234567890",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/id_1234%40id_1234.iam.gserviceaccount.com",
  "universe_domain": "googleapis.com"
}

到目前为止我已经:

import os

import io
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaIoBaseDownload

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = ".env"

with build(serviceName="drive", version="v3", credentials=os.environ) as service:
    ???

我在 Google API 文档中找不到适合我的用例的完整示例?

python google-api-python-client
1个回答
0
投票

您可以使用 gspread lib 构建简单的解析器

import pandas as pd
import gspread as gs

gc = gs.service_account(filename = 'cred.json')
sh = gc.open_by_url('place here url to your google sheet file')

ws = sh.worksheet('paste here your sheet name')
df = pd.DataFrame(ws.get_all_records())
df.to_excel('excel_name.xlsx')

如果您有很多工作表,则可以循环处理工作表,例如将它们合并到一个数据帧中,或将其另存为单独的数据帧。

记得以用户身份分享您的 Google Sheet 以生成令牌

这里有完整的文档:https://docs.gspread.org/en/v5.12.0/

© www.soinside.com 2019 - 2024. All rights reserved.