#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ BallController配置导入工具 从标准Excel表格导入配置到JSON文件 """ import pandas as pd import json import os from pathlib import Path def import_ball_config_from_excel(excel_path, json_output_path): """ 从Excel文件导入BallController配置并生成JSON文件 Args: excel_path: Excel文件路径 json_output_path: 输出JSON文件路径 """ try: # 读取Excel文件 df = pd.read_excel(excel_path) # 验证表格结构 if len(df.columns) != 2: raise ValueError(f"Excel表格应该只有2列,实际有{len(df.columns)}列") if '参数名' not in df.columns or '数值' not in df.columns: raise ValueError("Excel表格应该包含'参数名'和'数值'两列") # 转换为字典 config_dict = {} for index, row in df.iterrows(): param_name = row['参数名'] param_value = row['数值'] # 处理布尔值 if isinstance(param_value, str): if param_value.lower() == 'true': param_value = True elif param_value.lower() == 'false': param_value = False elif param_name == 'sensor' and param_value == 0.0: param_value = False elif param_name == 'sensor' and param_value == 1.0: param_value = True config_dict[param_name] = param_value # 写入JSON文件 with open(json_output_path, 'w', encoding='utf-8') as f: json.dump(config_dict, f, indent=2, ensure_ascii=False) print(f"✅ 配置导入成功!") print(f"📁 Excel文件: {excel_path}") print(f"📄 JSON文件: {json_output_path}") print(f"📊 导入了 {len(config_dict)} 个配置参数") return config_dict except Exception as e: print(f"❌ 导入失败: {str(e)}") return None def main(): """主函数""" # 文件路径 excel_file = "BallController标准配置表.xlsx" json_file = "../ballController.json" # 检查Excel文件是否存在 if not os.path.exists(excel_file): print(f"❌ Excel文件不存在: {excel_file}") return # 导入配置 config = import_ball_config_from_excel(excel_file, json_file) if config: print("\n📋 导入的配置参数:") for key, value in config.items(): print(f" {key}: {value}") if __name__ == "__main__": main()