首页 > 解决方案 > 有什么方法可以使用python通过api删除完整的电子表格

问题描述

我有我从 python 创建的电子表格的谷歌电子表格 ID,但我找不到任何方法从 python API 中删除该文件 我正在使用这里的代码

“我期待一种方法可以传递 spreadSheetId,它将删除包含该 ID 的电子表格”

标签: python-3.xgoogle-sheets-api

解决方案


# Authentication to Google Sheet API & Google Drive API
scopes = [
          "https://www.googleapis.com/auth/drive", 
          "https://www.googleapis.com/auth/drive.file", 
          "https://www.googleapis.com/auth/spreadsheets",
        "https://www.googleapis.com/auth/script.external_request"
          ]


credentials = service_account.Credentials.from_service_account_file(my_secret_file, scopes=scopes)
sheets_service = discovery.build('sheets', 'v4', credentials=credentials, cache_discovery=False)
drive_service = discovery.build('drive', 'v3', credentials=credentials, cache_discovery=False)

# Now delete. We only need the drive_service
drive_service.files().delete(fileId='your spreadsheet id').execute()

推荐阅读