导出为 XLSX 格式 python google sheet api
EXPORT AS XLSX format python google sheet api
我可以知道如何将 google sheet 中的文件导出为 xlsx 格式吗?
我下面的代码可以正常工作,但我还需要将文件保存为 xlsx 格式……:(
这是我的代码:
from oauth2client.service_account import ServiceAccountCredentials
import gsheets
pdkey = "keypd.json"
url = f"https://docs.google.com/spreadsheets/d/1MCkqb_123123123123asdasdada/edit#gid=0"
SCOPE = ["https://spreadsheets.google.com/feeds", 'https://www.googleapis.com/auth/spreadsheets',
"https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]
CREDS = ServiceAccountCredentials.from_json_keyfile_name(pdkey, SCOPE)
sheets = gsheets.Sheets(CREDS)
sheet = sheets.get(url)
sheet[0].to_csv("/root/xlsx/SAMPLE.csv")
你的情况,用DriveAPI的导出方法导出电子表格怎么样?当这反映在您的脚本中时,它会变成如下。
修改后的脚本:
发件人:
sheets = gsheets.Sheets(CREDS)
sheet = sheets.get(url)
sheet[0].to_csv("/root/xlsx/SAMPLE.csv")
收件人:
access_token = CREDS.create_delegated(CREDS._service_account_email).get_access_token().access_token
url = "https://www.googleapis.com/drive/v3/files/" + spreadsheet_id + "/export?mimeType=application%2Fvnd.openxmlformats-officedocument.spreadsheetml.sheet"
res = requests.get(url, headers={"Authorization": "Bearer " + access_token})
# If you want to create the XLSX data as a file, you can use the following script.
with open("sample.xlsx", 'wb') as f:
f.write(res.content)
- 在此脚本中,请添加
import requests
。
- 在这个修改后的脚本中,使用 Drive API 中的导出方法将电子表格导出为 XLSX 数据。访问令牌是从服务帐户中检索的。
参考:
我可以知道如何将 google sheet 中的文件导出为 xlsx 格式吗?
我下面的代码可以正常工作,但我还需要将文件保存为 xlsx 格式……:(
这是我的代码:
from oauth2client.service_account import ServiceAccountCredentials
import gsheets
pdkey = "keypd.json"
url = f"https://docs.google.com/spreadsheets/d/1MCkqb_123123123123asdasdada/edit#gid=0"
SCOPE = ["https://spreadsheets.google.com/feeds", 'https://www.googleapis.com/auth/spreadsheets',
"https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]
CREDS = ServiceAccountCredentials.from_json_keyfile_name(pdkey, SCOPE)
sheets = gsheets.Sheets(CREDS)
sheet = sheets.get(url)
sheet[0].to_csv("/root/xlsx/SAMPLE.csv")
你的情况,用DriveAPI的导出方法导出电子表格怎么样?当这反映在您的脚本中时,它会变成如下。
修改后的脚本:
发件人:
sheets = gsheets.Sheets(CREDS)
sheet = sheets.get(url)
sheet[0].to_csv("/root/xlsx/SAMPLE.csv")
收件人:
access_token = CREDS.create_delegated(CREDS._service_account_email).get_access_token().access_token
url = "https://www.googleapis.com/drive/v3/files/" + spreadsheet_id + "/export?mimeType=application%2Fvnd.openxmlformats-officedocument.spreadsheetml.sheet"
res = requests.get(url, headers={"Authorization": "Bearer " + access_token})
# If you want to create the XLSX data as a file, you can use the following script.
with open("sample.xlsx", 'wb') as f:
f.write(res.content)
- 在此脚本中,请添加
import requests
。 - 在这个修改后的脚本中,使用 Drive API 中的导出方法将电子表格导出为 XLSX 数据。访问令牌是从服务帐户中检索的。