json_to_excel.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python3
  2. """将 data.json 中的 models 模块导出为 Excel 文件。"""
  3. import json
  4. import sys
  5. from pathlib import Path
  6. import openpyxl
  7. from openpyxl.styles import Alignment, Font, PatternFill
  8. DATA_FILE = Path(__file__).parent / "data.json"
  9. OUTPUT_FILE = Path(__file__).parent / "models_output.xlsx"
  10. def parse_prices(prices: dict) -> list[str]:
  11. """解析价格字典,返回可读的价格行列表。"""
  12. rows = []
  13. for tier_label, tier_data in prices.items():
  14. if not isinstance(tier_data, dict):
  15. continue
  16. # 判断是否本身就有 price(简单计价)
  17. if "price" in tier_data:
  18. # 简单计价:tier_data = {"raw": "...", "price": 2.0, ...}
  19. price = tier_data.get("price", "")
  20. unit = tier_data.get("unit", "")
  21. price_original = tier_data.get("price_original")
  22. note = tier_data.get("note", "")
  23. row = f"{tier_label}: {price} {unit}"
  24. if price_original:
  25. row += f" (原价{price_original})"
  26. if note:
  27. row += f" [{note}]"
  28. rows.append(row)
  29. else:
  30. # 阶梯计价:tier_data = {"输入": {"price": ...}, "输出": {"price": ...}}
  31. for label, info in tier_data.items():
  32. if isinstance(info, dict) and "price" in info:
  33. price = info.get("price", "")
  34. unit = info.get("unit", "")
  35. price_original = info.get("price_original")
  36. note = info.get("note", "")
  37. row = f"{tier_label} | {label}: {price} {unit}"
  38. if price_original:
  39. row += f" (原价{price_original})"
  40. if note:
  41. row += f" [{note}]"
  42. rows.append(row)
  43. return rows
  44. def main():
  45. with open(DATA_FILE, "r", encoding="utf-8") as f:
  46. data = json.load(f)
  47. models = data.get("models", [])
  48. if not models:
  49. print("没有找到 models 数据")
  50. sys.exit(1)
  51. wb = openpyxl.Workbook()
  52. ws = wb.active
  53. ws.title = "Models"
  54. # 表头
  55. headers = ["模型名称", "价格", "价格单位", "介绍", "分组", "标签"]
  56. header_fill = PatternFill(start_color="4472C4", end_color="4472C4", fill_type="solid")
  57. header_font = Font(bold=True, color="FFFFFF")
  58. for col_idx, header in enumerate(headers, 1):
  59. cell = ws.cell(row=1, column=col_idx, value=header)
  60. cell.fill = header_fill
  61. cell.font = header_font
  62. cell.alignment = Alignment(horizontal="center")
  63. # 设置列宽
  64. ws.column_dimensions["A"].width = 35
  65. ws.column_dimensions["B"].width = 80
  66. ws.column_dimensions["C"].width = 15
  67. ws.column_dimensions["D"].width = 80
  68. ws.column_dimensions["E"].width = 20
  69. ws.column_dimensions["F"].width = 25
  70. for row_idx, model in enumerate(models, 2):
  71. model_name = model.get("model_name", "")
  72. group_name = model.get("group_name", "")
  73. # 价格信息
  74. prices = model.get("prices", {})
  75. price_lines = parse_prices(prices)
  76. price_str = "\n".join(price_lines)
  77. # 介绍
  78. model_info = model.get("model_info", {})
  79. description = model_info.get("description", "")
  80. # 标签
  81. display_tags = ", ".join(model_info.get("display_tags", []))
  82. ws.cell(row=row_idx, column=1, value=model_name)
  83. ws.cell(row=row_idx, column=2, value=price_str)
  84. ws.cell(row=row_idx, column=3, value="")
  85. ws.cell(row=row_idx, column=4, value=description)
  86. ws.cell(row=row_idx, column=5, value=group_name)
  87. ws.cell(row=row_idx, column=6, value=display_tags)
  88. # 自动换行
  89. for col in (2, 4):
  90. ws.cell(row=row_idx, column=col).alignment = Alignment(wrap_text=True, vertical="top")
  91. wb.save(OUTPUT_FILE)
  92. print(f"导出完成,共 {len(models)} 条模型数据 -> {OUTPUT_FILE}")
  93. if __name__ == "__main__":
  94. main()