|
|
@@ -8,7 +8,7 @@ setup_logging()
|
|
|
logger = logging.getLogger('excel_tool')
|
|
|
|
|
|
class ExcelProcessor:
|
|
|
- def __init__(self, file_path, header_row=0):
|
|
|
+ def __init__(self, file_path, header_row=1):
|
|
|
"""Initialize Excel processor"""
|
|
|
self.file_path = file_path
|
|
|
self.header_row = header_row
|
|
|
@@ -154,11 +154,12 @@ class ExcelProcessor:
|
|
|
logger.error(f"读取文件失败: {file_path}, 错误: {str(e)}")
|
|
|
raise
|
|
|
|
|
|
- def save_file(self, df: pd.DataFrame, output_path: str) -> None:
|
|
|
+ def save_file(self, output_path: str) -> None:
|
|
|
"""
|
|
|
将DataFrame保存为文件
|
|
|
支持Excel和CSV格式
|
|
|
"""
|
|
|
+ df = self.df
|
|
|
try:
|
|
|
if output_path.endswith('.csv'):
|
|
|
df.to_csv(output_path, index=False)
|
|
|
@@ -214,7 +215,7 @@ class ExcelProcessor:
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
# 测试代码
|
|
|
- processor = ExcelProcessor('/home/mrh/code/excel_tool/temp/测试_process.csv')
|
|
|
+ processor = ExcelProcessor('/home/mrh/code/excel_tool/temp/测试.csv.utf8.csv')
|
|
|
# 测试CSV文件
|
|
|
processor.insert_column('B', '翻译结果')
|
|
|
|
|
|
@@ -230,6 +231,6 @@ if __name__ == '__main__':
|
|
|
|
|
|
# 测试写入列数据
|
|
|
new_data = ['新数据1', '新数据2', '新数据3']
|
|
|
- processor.write_column_data('B', new_data)
|
|
|
+ processor.write_column_data('C', new_data)
|
|
|
|
|
|
- # processor.save_file(df, '/home/mrh/code/excel_tool/temp/测试_process.csv')
|
|
|
+ processor.save_file('/home/mrh/code/excel_tool/temp/测试_process.csv')
|