import_ball_config.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. BallController配置导入工具
  5. 从标准Excel表格导入配置到JSON文件
  6. """
  7. import pandas as pd
  8. import json
  9. import os
  10. from pathlib import Path
  11. def import_ball_config_from_excel(excel_path, json_output_path):
  12. """
  13. 从Excel文件导入BallController配置并生成JSON文件
  14. Args:
  15. excel_path: Excel文件路径
  16. json_output_path: 输出JSON文件路径
  17. """
  18. try:
  19. # 读取Excel文件
  20. df = pd.read_excel(excel_path)
  21. # 验证表格结构
  22. if len(df.columns) != 2:
  23. raise ValueError(f"Excel表格应该只有2列,实际有{len(df.columns)}列")
  24. if '参数名' not in df.columns or '数值' not in df.columns:
  25. raise ValueError("Excel表格应该包含'参数名'和'数值'两列")
  26. # 转换为字典
  27. config_dict = {}
  28. for index, row in df.iterrows():
  29. param_name = row['参数名']
  30. param_value = row['数值']
  31. # 处理布尔值
  32. if isinstance(param_value, str):
  33. if param_value.lower() == 'true':
  34. param_value = True
  35. elif param_value.lower() == 'false':
  36. param_value = False
  37. elif param_name == 'sensor' and param_value == 0.0:
  38. param_value = False
  39. elif param_name == 'sensor' and param_value == 1.0:
  40. param_value = True
  41. config_dict[param_name] = param_value
  42. # 写入JSON文件
  43. with open(json_output_path, 'w', encoding='utf-8') as f:
  44. json.dump(config_dict, f, indent=2, ensure_ascii=False)
  45. print(f"✅ 配置导入成功!")
  46. print(f"📁 Excel文件: {excel_path}")
  47. print(f"📄 JSON文件: {json_output_path}")
  48. print(f"📊 导入了 {len(config_dict)} 个配置参数")
  49. return config_dict
  50. except Exception as e:
  51. print(f"❌ 导入失败: {str(e)}")
  52. return None
  53. def main():
  54. """主函数"""
  55. # 文件路径
  56. excel_file = "BallController标准配置表.xlsx"
  57. json_file = "../ballController.json"
  58. # 检查Excel文件是否存在
  59. if not os.path.exists(excel_file):
  60. print(f"❌ Excel文件不存在: {excel_file}")
  61. return
  62. # 导入配置
  63. config = import_ball_config_from_excel(excel_file, json_file)
  64. if config:
  65. print("\n📋 导入的配置参数:")
  66. for key, value in config.items():
  67. print(f" {key}: {value}")
  68. if __name__ == "__main__":
  69. main()