AGAZO_Final_Assignment / excel_parser_tool.py
Alexandre Gazola
roda so chess e excel
aa1410a
from utils import clean_text
from langchain_core.tools import tool
import io
import pandas as pd
import base64
from utils import get_base64, get_text_file_contents
@tool
def parse_excel(base64_filepath: str) -> str:
"""Parses a base64-encoded Excel (.xlsx) file and returns a markdown summary."""
try:
base64_str = get_text_file_contents(base64_filepath)
file_bytes = base64.b64decode(base64_str)
excel_file = pd.ExcelFile(io.BytesIO(file_bytes))
output = []
for sheet in excel_file.sheet_names:
df = excel_file.parse(sheet)
output.append(f"### Sheet: {sheet}\n")
output.append(df.head(20).to_markdown(index=False))
output.append("\n")
return "\n".join(output)
except Exception as e:
return f"Error parsing Excel file: {e}"
if __name__ == "__main__":
import sys
filepath = 'C:\\Users\\agazo\\Downloads\\7bd855d8-463d-4ed5-93ca-5fe35145f733_file.xlsx'
try:
result = parse_excel(get_base64(filepath))
print(result)
except FileNotFoundError:
print(f"File not found: {filepath}")
except Exception as e:
print(f"Error: {e}")