import os from typing import Optional import pandas as pd class ExcelFile: def __init__(self, file_path): # 基于引用 dict_from_json 函数的文件的绝对路径 self._file_path = file_path self.file = self.read_excel(file_path) def new_column(self, column_name): try: column_index = self.file.columns.get_loc(column_name) if column_index is None: self.file[column_name] = "" self.file.to_excel(self._file_path, index=False) except Exception as e: print(f"Unexpected error creating new column: {column_name}") def new_value(self, row_index, column_name, value): try: self.file.at[row_index, column_name] = value self.file.to_excel(self._file_path, index=False) except Exception as e: print(f"Unexpected error creating new column: {column_name}") @staticmethod def read_excel(file_path): try: df = pd.read_excel(file_path) except FileNotFoundError: print(f"File not found: {file_path}") return None except Exception as e: print(f"Unexpected error reading file: {file_path}") print(str(e)) return None try: return df except Exception as e: print(f"Unexpected error converting DataFrame to string") print(str(e)) return None