#!/usr/bin/env python3 """ LLM Notes Excel Converter - Convert JSON notes to Excel format """ import sys import os import json import logging from pathlib import Path # Add parent directory to path for imports sys.path.insert(0, str(Path(__file__).parent.parent)) # Insert at beginning to prioritize main utils from utils.utils_normalize import normalize_llm_notes_json from notes.json_to_excel import json_to_xlsx # Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def main(): """Main function to convert LLM-generated notes JSON to Excel""" if len(sys.argv) != 3: logger.error("Usage: python llm_notes_excel_converter.py ") sys.exit(1) input_json = sys.argv[1] output_excel = sys.argv[2] if not os.path.exists(input_json): logger.error(f"Input JSON file not found: {input_json}") sys.exit(1) try: logger.info(f"Converting notes from {input_json} to {output_excel}") # Load the generated notes with open(input_json, "r", encoding="utf-8") as f: notes_data = json.load(f) # Normalize the JSON structure wrapped_json_path = "data/generated_notes/notes_wrapped.json" os.makedirs("data/generated_notes", exist_ok=True) if isinstance(notes_data, dict) and "notes" in notes_data: # Already in correct format normalized_data = notes_data else: # Wrap in notes structure normalized_data = {"notes": notes_data} if isinstance(notes_data, list) else notes_data # Normalize using the utility function wrapped = normalize_llm_notes_json(normalized_data) # Save wrapped JSON with open(wrapped_json_path, "w", encoding="utf-8") as f: json.dump(wrapped, f, ensure_ascii=False, indent=2) # Convert to Excel os.makedirs(os.path.dirname(output_excel), exist_ok=True) json_to_xlsx(wrapped_json_path, output_excel) logger.info(f"Excel conversion completed. Output saved to: {output_excel}") return 0 except Exception as e: logger.error(f"Error converting to Excel: {e}") return 1 if __name__ == "__main__": sys.exit(main())