Skip to content

Excel

ExcelToList

Bases: Node

Source code in dynamiq/nodes/transformers/excel.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class ExcelToList(Node):
    group: Literal[NodeGroup.TRANSFORMERS] = NodeGroup.TRANSFORMERS
    name: str = "ExcelToList"
    description: str = "Node that transforms excel file into a list of dictionaries"

    model_config = ConfigDict(arbitrary_types_allowed=True)

    input_schema: ClassVar[type[ExcelToListTransformerInputSchema]] = ExcelToListTransformerInputSchema

    def execute(
        self, input_data: ExcelToListTransformerInputSchema, config: RunnableConfig = None, **kwargs
    ) -> dict[str, Any]:
        """
        Transform an Excel file into a list of dictionaries.

        Args:
            input_data (ExcelToListTransformerInputSchema): input data for the tool, which includes the data from
                Excel file.
            config (RunnableConfig, optional): Configuration for the runnable, including callbacks.
            **kwargs: Additional arguments passed to the execution context.

        Returns:
            dict[str, Any]: A dictionary containing list of data from Excel file.
        """
        import pandas as pd

        config = ensure_config(config)
        self.run_on_node_execute_run(config.callbacks, **kwargs)

        value = input_data.value
        sheet_name = input_data.sheet_name
        try:
            if sheet_name is None:
                excel_data = pd.read_excel(value, sheet_name=None)
                result = {sheet: df.to_dict(orient="records") for sheet, df in excel_data.items()}
            else:
                df = pd.read_excel(value, sheet_name=sheet_name)
                result = {sheet_name: df.to_dict(orient="records")}

            return {"content": result}
        except Exception as e:
            raise ValueError(f"Encountered an error while performing transformation. \nError details: {e}")

execute(input_data, config=None, **kwargs)

Transform an Excel file into a list of dictionaries.

Parameters:

Name Type Description Default
input_data ExcelToListTransformerInputSchema

input data for the tool, which includes the data from Excel file.

required
config RunnableConfig

Configuration for the runnable, including callbacks.

None
**kwargs

Additional arguments passed to the execution context.

{}

Returns:

Type Description
dict[str, Any]

dict[str, Any]: A dictionary containing list of data from Excel file.

Source code in dynamiq/nodes/transformers/excel.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def execute(
    self, input_data: ExcelToListTransformerInputSchema, config: RunnableConfig = None, **kwargs
) -> dict[str, Any]:
    """
    Transform an Excel file into a list of dictionaries.

    Args:
        input_data (ExcelToListTransformerInputSchema): input data for the tool, which includes the data from
            Excel file.
        config (RunnableConfig, optional): Configuration for the runnable, including callbacks.
        **kwargs: Additional arguments passed to the execution context.

    Returns:
        dict[str, Any]: A dictionary containing list of data from Excel file.
    """
    import pandas as pd

    config = ensure_config(config)
    self.run_on_node_execute_run(config.callbacks, **kwargs)

    value = input_data.value
    sheet_name = input_data.sheet_name
    try:
        if sheet_name is None:
            excel_data = pd.read_excel(value, sheet_name=None)
            result = {sheet: df.to_dict(orient="records") for sheet, df in excel_data.items()}
        else:
            df = pd.read_excel(value, sheet_name=sheet_name)
            result = {sheet_name: df.to_dict(orient="records")}

        return {"content": result}
    except Exception as e:
        raise ValueError(f"Encountered an error while performing transformation. \nError details: {e}")