excel_processor.py•1.68 kB
```python
import os
import pandas as pd
def main():
# Step 2: Read Excel File
file_path = 'data.xlsx'
sheet_name = 'Sheet1' # Change as needed
column_name = 'TargetColumn' # Change as needed
# Check File Existence
if not os.path.exists(file_path):
raise FileNotFoundError(f"The file {file_path} does not exist.")
# Select the Desired Sheet and Read Data
try:
df = pd.read_excel(file_path, sheet_name=sheet_name)
except Exception as e:
raise Exception(f"Error reading the Excel file: {e}")
# Step 3: Data Processing
# Check for Required Column
if column_name not in df.columns:
raise ValueError(f"Column {column_name} not found in the sheet.")
# Handle Missing Values
df[column_name].fillna(0, inplace=True) # Or use another strategy
# Check Data Types
if not pd.api.types.is_numeric_dtype(df[column_name]):
raise TypeError(f"Column {column_name} must be numeric.")
# Step 4: Calculate Average
try:
average_value = df[column_name].mean()
except Exception as e:
raise Exception(f"Error calculating the mean: {e}")
# Format Result
formatted_result = f"The average value of {column_name} is {average_value:.2f}"
# Step 5: Output Result
try:
# Print the Result
print(formatted_result)
# Option to Save Result
save_result = True # Change as needed
if save_result:
with open('result.txt', 'w') as f:
f.write(formatted_result)
except Exception as e:
raise Exception(f"Error outputting the result: {e}")
if __name__ == "__main__":
main()
```