Prerequisites

  • Python 3.10 or higher
  • PraisonAI Agents package installed
  • openpyxl package installed
  • Basic understanding of Excel files

Excel Tools

Use Excel Tools to process and manipulate Excel files with AI agents.

1

Install Dependencies

First, install the required packages:

pip install praisonaiagents openpyxl
2

Import Components

Import the necessary components:

from praisonaiagents import Agent, Task, PraisonAIAgents
from praisonaiagents.tools import read_excel, write_excel, merge_excel
3

Create Agent

Create an Excel processing agent:

excel_agent = Agent(
    name="ExcelProcessor",
    role="Excel Processing Specialist",
    goal="Process Excel files efficiently and accurately.",
    backstory="Expert in Excel file manipulation and analysis.",
    tools=[read_excel, write_excel, merge_excel],
    self_reflect=False
)
4

Define Task

Define the Excel processing task:

excel_task = Task(
    description="Process and analyze Excel spreadsheets.",
    expected_output="Processed Excel data with analysis.",
    agent=excel_agent,
    name="excel_processing"
)
5

Run Agent

Initialize and run the agent:

agents = PraisonAIAgents(
    agents=[excel_agent],
    tasks=[excel_task],
    process="sequential"
)
agents.start()

Understanding Excel Tools

What are Excel Tools?

Excel Tools provide Excel processing capabilities for AI agents:

  • File reading and writing
  • Data extraction
  • Sheet manipulation
  • Formula handling
  • Data analysis

Key Components

Excel Agent

Create specialized Excel agents:

Agent(tools=[read_excel, write_excel, merge_excel])

Excel Task

Define Excel tasks:

Task(description="excel_operation")

Process Types

Sequential or parallel processing:

process="sequential"

Excel Options

Customize Excel parameters:

sheet_name="Sheet1", header=0

Available Functions

from praisonaiagents.tools import read_excel
from praisonaiagents.tools import write_excel
from praisonaiagents.tools import merge_excel

Function Details

read_excel(filepath: str, sheet_name: Optional[Union[str, int, List[Union[str, int]]]] = 0, header: Optional[int] = 0, usecols: Optional[List[str]] = None, skiprows: Optional[Union[int, List[int]]] = None, na_values: Optional[List[str]] = None, dtype: Optional[Dict[str, str]] = None)

Reads Excel files with advanced options:

  • Support for multiple sheets
  • Flexible header handling
  • Column selection
  • Row skipping
  • Custom NA values
  • Data type specification
# Basic usage - read first sheet
data = read_excel("data.xlsx")

# Read specific sheets
data = read_excel(
    "data.xlsx",
    sheet_name=['Sheet1', 'Sheet2'],
    header=0,
    usecols=['A', 'B', 'C'],
    skiprows=[0, 1],
    na_values=['NA', 'missing'],
    dtype={'age': 'int32', 'salary': 'float64'}
)
# Returns: Dict[str, List[Dict[str, Any]]] for multiple sheets
# Example: {
#   'Sheet1': [{"name": "Alice", "age": 25}, ...],
#   'Sheet2': [{"dept": "Engineering", "count": 10}, ...]
# }

write_excel(filepath: str, data: Union[Dict[str, List[Dict[str, Any]]], List[Dict[str, Any]]], sheet_name: Optional[str] = None, index: bool = False, header: bool = True, mode: str = ‘w’)

Writes data to Excel files with formatting:

  • Multiple sheet support
  • Append mode for existing files
  • Optional row indices
  • Optional headers
  • Flexible data structure handling
# Write single sheet
data = [
    {"name": "Alice", "age": 25, "city": "New York"},
    {"name": "Bob", "age": 30, "city": "San Francisco"}
]
success = write_excel("output.xlsx", data, sheet_name="Employees")

# Write multiple sheets
data = {
    "Employees": [
        {"name": "Alice", "age": 25},
        {"name": "Bob", "age": 30}
    ],
    "Departments": [
        {"name": "Engineering", "count": 50},
        {"name": "Sales", "count": 30}
    ]
}
success = write_excel(
    "company.xlsx",
    data,
    index=False,
    header=True,
    mode='w'
)
# Returns: bool (True if successful)

merge_excel(files: List[str], output_file: str, how: str = ‘inner’, on: Optional[Union[str, List[str]]] = None, suffixes: Optional[Tuple[str, str]] = None)

Merges multiple Excel files:

  • Flexible join operations
  • Multiple key columns support
  • Custom column suffix handling
  • Preserves data types
# Merge two files on a common column
success = merge_excel(
    files=["employees.xlsx", "salaries.xlsx"],
    output_file="merged.xlsx",
    how='inner',
    on='employee_id'
)

# Advanced merge with multiple keys and custom suffixes
success = merge_excel(
    files=["data2022.xlsx", "data2023.xlsx"],
    output_file="combined.xlsx",
    how='outer',
    on=['id', 'department'],
    suffixes=('_2022', '_2023')
)
# Returns: bool (True if successful)

Examples

Basic Excel Processing Agent

from praisonaiagents import Agent, Task, PraisonAIAgents
from praisonaiagents.tools import read_excel, write_excel, merge_excel

# Create Excel agent
excel_agent = Agent(
    name="ExcelExpert",
    role="Excel Processing Specialist",
    goal="Process Excel files efficiently and accurately.",
    backstory="Expert in spreadsheet manipulation and analysis.",
    tools=[read_excel, write_excel, merge_excel],
    self_reflect=False
)

# Define Excel task
excel_task = Task(
    description="Process and analyze sales data.",
    expected_output="Sales analysis report.",
    agent=excel_agent,
    name="sales_analysis"
)

# Run agent
agents = PraisonAIAgents(
    agents=[excel_agent],
    tasks=[excel_task],
    process="sequential"
)
agents.start()

Advanced Excel Processing with Multiple Agents

# Create data processing agent
processor_agent = Agent(
    name="Processor",
    role="Data Processor",
    goal="Process Excel data systematically.",
    tools=[read_excel, write_excel, merge_excel],
    self_reflect=False
)

# Create analysis agent
analysis_agent = Agent(
    name="Analyzer",
    role="Data Analyst",
    goal="Analyze processed Excel data.",
    backstory="Expert in data analysis and reporting.",
    self_reflect=False
)

# Define tasks
processing_task = Task(
    description="Process sales spreadsheets.",
    agent=processor_agent,
    name="data_processing"
)

analysis_task = Task(
    description="Analyze sales trends and patterns.",
    agent=analysis_agent,
    name="data_analysis"
)

# Run agents
agents = PraisonAIAgents(
    agents=[processor_agent, analysis_agent],
    tasks=[processing_task, analysis_task],
    process="sequential"
)
agents.start()

Best Practices

Common Patterns

Excel Processing Pipeline

# Processing agent
processor = Agent(
    name="Processor",
    role="Excel Processor",
    tools=[read_excel, write_excel, merge_excel]
)

# Analysis agent
analyzer = Agent(
    name="Analyzer",
    role="Data Analyst"
)

# Define tasks
process_task = Task(
    description="Process Excel files",
    agent=processor
)

analyze_task = Task(
    description="Analyze processed data",
    agent=analyzer
)

# Run workflow
agents = PraisonAIAgents(
    agents=[processor, analyzer],
    tasks=[process_task, analyze_task]
)

Dependencies

The Excel tools require the following Python packages:

  • pandas: For data manipulation
  • openpyxl: For Excel file operations

These will be automatically installed when needed.

Example Agent Configuration

from praisonaiagents import Agent
from praisonaiagents.tools import read_excel, write_excel, merge_excel

agent = Agent(
    name="ExcelProcessor",
    description="An agent that processes Excel files",
    tools=[read_excel, write_excel, merge_excel]
)

Error Handling

All functions include comprehensive error handling:

  • File not found errors
  • Permission errors
  • Invalid sheet names
  • Data format errors
  • Missing dependency errors

Errors are logged and returned in a consistent format:

  • Success cases return the expected data type
  • Error cases return a dict with an “error” key containing the error message

Common Use Cases

  1. Data Analysis:
# Read and analyze employee data
data = read_excel(
    "employees.xlsx",
    sheet_name="Salaries",
    usecols=["department", "salary"]
)

# Write summary to new sheet
summary = [
    {"department": dept, "avg_salary": sum(d["salary"] for d in dept_data) / len(dept_data)}
    for dept, dept_data in groupby(data, key=lambda x: x["department"])
]
write_excel("summary.xlsx", summary, sheet_name="Salary Summary")
  1. Data Consolidation:
# Merge monthly reports
monthly_files = ["jan.xlsx", "feb.xlsx", "mar.xlsx"]
merge_excel(
    files=monthly_files,
    output_file="q1_report.xlsx",
    how='outer',
    on='transaction_id'
)
  1. Multi-sheet Processing:
# Read multiple sheets
data = read_excel(
    "company_data.xlsx",
    sheet_name=None  # Read all sheets
)

# Process each sheet
processed = {
    sheet: [process_record(record) for record in records]
    for sheet, records in data.items()
}

# Write back to new file
write_excel("processed_data.xlsx", processed)

Was this page helpful?