|
|
@@ -0,0 +1,47 @@
|
|
|
+from typing import List, Dict
|
|
|
+import pandas as pd
|
|
|
+from urllib.parse import quote
|
|
|
+from mylib.logging_config import setup_logging
|
|
|
+import logging
|
|
|
+
|
|
|
+# Setup logging
|
|
|
+setup_logging()
|
|
|
+logger = logging.getLogger('excel_tool')
|
|
|
+
|
|
|
+class ExcelProcessor:
|
|
|
+ def __init__(self, file_path: str):
|
|
|
+ """Initialize Excel processor with file path"""
|
|
|
+ self.file_path = file_path
|
|
|
+ self.df = pd.read_excel(file_path)
|
|
|
+ logger.info(f"Loaded Excel file: {file_path}")
|
|
|
+
|
|
|
+ def add_translation_column(self, column_name: str, translations: Dict[str, str]):
|
|
|
+ """Add translated column next to specified column"""
|
|
|
+ try:
|
|
|
+ new_col = f"{column_name}_中文"
|
|
|
+ self.df[new_col] = self.df[column_name].map(translations)
|
|
|
+ logger.info(f"Added translation column for {column_name}")
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"Error adding translation column: {str(e)}")
|
|
|
+ raise
|
|
|
+
|
|
|
+ def add_hyperlink_column(self, column_name: str, base_url: str):
|
|
|
+ """Add hyperlink column for specified column"""
|
|
|
+ try:
|
|
|
+ new_col = f"{column_name}_链接"
|
|
|
+ self.df[new_col] = self.df[column_name].apply(
|
|
|
+ lambda x: f'=HYPERLINK("{base_url}{quote(x)}", "{x}")'
|
|
|
+ )
|
|
|
+ logger.info(f"Added hyperlink column for {column_name}")
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"Error adding hyperlink column: {str(e)}")
|
|
|
+ raise
|
|
|
+
|
|
|
+ def save(self, output_path: str):
|
|
|
+ """Save processed Excel file"""
|
|
|
+ try:
|
|
|
+ self.df.to_excel(output_path, index=False)
|
|
|
+ logger.info(f"Saved processed file to {output_path}")
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"Error saving file: {str(e)}")
|
|
|
+ raise
|