DF_Final_Assignment_Template / tools /excel_to_text.py
dmfelder's picture
Update tools/excel_to_text.py
5836295 verified
Raw History Blame Contribute Delete
982 Bytes
import pandas as pd
from langchain_core.tools import tool
from loguru import logger
from tools.load_file import load_file
@tool("excel_to_text_tool", parse_docstring=True)
def excel_to_text(path_or_url: str) -> str:
"""
Reads an Excel file and returns a text representation of the table data.
Args:
path_or_url (str): A local file path or a URL to an Excel file.
Returns:
str: The table contents formatted as plain text.
"""
logger.info(f"use excel_to_text with param: {path_or_url}")
try:
excel = load_file(path_or_url)
df = pd.read_excel(excel)
return df.to_markdown(index=False)
except Exception as e:
return f"Error reading Excel file: {str(e)}"
if __name__ == "__main__":
print(excel_to_text.invoke("../data/7bd855d8-463d-4ed5-93ca-5fe35145f733.xlsx"))
print(
excel_to_text.invoke("https://agents-course-unit4-scoring.hf.space/files/7bd855d8-463d-4ed5-93ca-5fe35145f733"))