File size: 982 Bytes
a9f884c 5836295 a9f884c 5836295 a9f884c 5836295 a9f884c 5836295 a9f884c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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")) |