52 lines
981 B
Python
52 lines
981 B
Python
|
|
||
|
|
||
|
import gspread
|
||
|
from gspread.utils import GridRangeType
|
||
|
|
||
|
import os
|
||
|
|
||
|
# 获取当前工作目录
|
||
|
current_dir = os.getcwd()
|
||
|
|
||
|
# 拼接文件路径
|
||
|
file_path = os.path.join(current_dir, 'service_account.json')
|
||
|
|
||
|
print(file_path)
|
||
|
|
||
|
gc = gspread.service_account(filename=file_path)
|
||
|
print("gc")
|
||
|
print(gc)
|
||
|
sheetkey = '1fmZc0DsjusQwJ6giUKW_sFElugpDLR1yRs2KT9RhuJo'
|
||
|
print("sheetkey")
|
||
|
print(sheetkey)
|
||
|
print(type(sheetkey))
|
||
|
|
||
|
sh = gc.open_by_key(sheetkey)
|
||
|
print("sh")
|
||
|
print(sh)
|
||
|
# Select worksheet by index. Worksheet indexes start from zero
|
||
|
worksheet = sh.get_worksheet(0)
|
||
|
print("worksheet")
|
||
|
print(worksheet)
|
||
|
|
||
|
list_of_lists = worksheet.get(return_type=GridRangeType.ListOfLists)
|
||
|
#print(list_of_lists)
|
||
|
|
||
|
lenth = len(list_of_lists)
|
||
|
|
||
|
# output
|
||
|
import json
|
||
|
|
||
|
# 指定JSON文件名
|
||
|
json_filename = "output.json"
|
||
|
|
||
|
# 写入JSON文件
|
||
|
with open(json_filename, 'w', encoding='utf-8') as file:
|
||
|
json.dump(list_of_lists, file, ensure_ascii=False, indent=4)
|
||
|
|
||
|
print(f"数据已导出到 {json_filename}")
|
||
|
|
||
|
|
||
|
|
||
|
|