File size: 1,007 Bytes
f3b4b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64a08a8
f3b4b95
 
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:
    """
    Read an Excel file and return a text representation of the table data.

    Args:
        path_or_url (str): Either a local file path to an Excel file or a URL pointing to an Excel file.

    Returns:
        str: Text representation of the table data
    """
    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"))