ai_member_xiaobian/scripts/parse_sheet.py
2026-06-19 08:10:01 +08:00

33 lines
1.3 KiB
Python

import json
import csv
import sys
with open('output/u27_5_sheet.json', 'r', encoding='utf-8') as f:
data = json.load(f)
values = data.get('data', {}).get('valueRange', {}).get('values', [])
with open('output/u27_5_sheet.csv', 'w', encoding='utf-8', newline='') as f:
writer = csv.writer(f)
for row in values:
# Each cell is a dictionary or a list sometimes in Feishu API v2, wait, no, in v2 it's usually just list of dicts with type/text or strings.
# Actually in Sheets v2 API, values are arrays of arrays containing objects like {"type": "string", "text": "..."} or just strings if unformatted.
row_str = []
for cell in row:
if isinstance(cell, dict):
# if it's a rich text object
if cell.get('type') == 'url':
row_str.append(cell.get('text', ''))
else:
# just extract text if possible
# typical: [{"type":"text","text":"foo"}]
pass
elif isinstance(cell, list):
# rich text is usually a list of dicts
text = "".join([c.get('text', '') if isinstance(c, dict) else str(c) for c in cell])
row_str.append(text)
else:
row_str.append(str(cell))
writer.writerow(row_str)