main.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from typing import List
  2. from excel_processor import ExcelProcessor
  3. from translation_service import TranslationService
  4. from mylib.logging_config import setup_logging
  5. import logging
  6. # Setup logging
  7. setup_logging()
  8. logger = logging.getLogger('main')
  9. def process_excel_file(input_path: str, output_path: str, columns_to_translate: List[str]):
  10. """Main function to process Excel file"""
  11. try:
  12. logger.info("Starting Excel processing")
  13. # Initialize services
  14. excel_processor = ExcelProcessor(input_path)
  15. translation_service = TranslationService()
  16. # Process each column
  17. for column in columns_to_translate:
  18. # Get unique values for translation
  19. unique_values = excel_processor.df[column].unique().tolist()
  20. # Translate values
  21. translations = translation_service.translate_batch(unique_values)
  22. # Add translated column
  23. excel_processor.add_translation_column(column, translations)
  24. # Add hyperlink column
  25. if column == "搜索词":
  26. excel_processor.add_hyperlink_column(column, "https://www.amazon.co.jp/s?k=")
  27. elif column == "ASIN":
  28. excel_processor.add_hyperlink_column(column, "https://www.amazon.co.jp/dp/")
  29. # Save processed file
  30. excel_processor.save(output_path)
  31. logger.info("Excel processing completed successfully")
  32. except Exception as e:
  33. logger.error(f"Error processing Excel file: {str(e)}")
  34. raise
  35. if __name__ == "__main__":
  36. # Example usage
  37. process_excel_file(
  38. input_path="input.xlsx",
  39. output_path="output.xlsx",
  40. columns_to_translate=["搜索词", "品牌", "点击量最高的类别"]
  41. )