要使用Python复制Google工作表,可以使用Google Sheets API和Google API客户端库来实现。下面是一个基本的步骤指南:
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
spreadsheets().sheets().copyTo()
方法来复制工作表。下面是一个示例代码:
import os
import google.auth
from googleapiclient.discovery import build
# 认证配置
credentials, project = google.auth.default()
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/your/credentials.json"
# 创建服务对象
service = build('sheets', 'v4', credentials=credentials)
# 复制工作表
spreadsheet_id = 'your-source-spreadsheet-id'
sheet_id = 'your-source-sheet-id'
destination_spreadsheet_id = 'your-destination-spreadsheet-id'
request = service.spreadsheets().sheets().copyTo(
spreadsheetId=spreadsheet_id,
sheetId=sheet_id,
body={'destinationSpreadsheetId': destination_spreadsheet_id}
)
response = request.execute()
print('复制成功!')
请注意替换示例代码中的以下值:
path/to/your/credentials.json
:替换为您下载的JSON密钥文件的路径。your-source-spreadsheet-id
:替换为源工作表的ID。your-source-sheet-id
:替换为要复制的源工作表的ID。your-destination-spreadsheet-id
:替换为目标工作表的ID。领取专属 10元无门槛券
手把手带您无忧上云