Skip to content

raplan.excel

RaPlan I/O with Excel. For this functionality to work, the 'excel' extra should be installed.

from_excel

from_excel(path: str | Path) -> list[Project]

Convert an Excel file to a RaPlan project.

Parameters:

Name Type Description Default
path str | Path

Path to an Excel workbook containing a RaPlan project.

required
Source code in src/raplan/excel.py
def from_excel(path: str | Path) -> list[Project]:
    """Convert an Excel file to a RaPlan project.

    Arguments:
        path: Path to an Excel workbook containing a RaPlan project.
    """
    wb = openpyxl.load_workbook(path)
    objects: dict[uuid.UUID, Any] = dict()

    # First set horizon
    horizon = None

    for sheetname in [
        "Horizon",
        "Tasks",
        "Maintenance",
        "Components",
        "Systems",
    ]:
        ws = wb[sheetname]
        for i in range(2, ws.max_row + 1):
            _from_row(ws[i], sheetname, wb, objects, horizon)

        if sheetname == "Horizon":
            horizon = [item for item in objects.values()][0]
            horizon.start = int(horizon.start)
            horizon.end = int(horizon.end)

    ws = wb["Project"]
    project = _from_row(ws[2], "Project", wb, objects)
    return project

to_excel

to_excel(
    project: Project,
    path: str | Path | None = None,
    lock: bool = False,
) -> Workbook

Convert a RaPlan project to an Excel workbook.

Parameters:

Name Type Description Default
project Project

RaPlan project to export.

required
path str | Path | None

Optional filename to save the workbook to.

None

Returns:

Type Description
Workbook

An openpyxl Workbook.

Source code in src/raplan/excel.py
def to_excel(
    project: Project, path: str | Path | None = None, lock: bool = False
) -> openpyxl.Workbook:
    """Convert a RaPlan project to an Excel workbook.

    Arguments:
        project: RaPlan project to export.
        path: Optional filename to save the workbook to.

    Returns:
        An openpyxl Workbook.
    """
    wb = Workbook()

    # Let schedule be the first sheet.
    schedule = wb.active
    schedule.title = "Schedule"
    schedule.append(
        [
            "project",
            "system",
            "component",
            "maintenance",
            "task",
            "maintenance_time",
            "task_rejuvenation",
            "task_duration",
            "task_cost",
        ]
    )
    if lock:
        _lock_worksheet(schedule)

    # Create sheets for instance storage.
    sheets: dict[str, Worksheet] = dict()
    indices: dict[str, dict[uuid.UUID, int]] = dict()
    objects: dict[uuid.UUID, Any] = dict()

    for cls, title in _TYPES_TO_SHEETS.items():  # Setup sheets, fields and headers.
        sheets[title] = wb.create_sheet(title)
        sheets[title].append(_FIELDS[title])
        indices[title] = dict()
        if lock:
            _lock_worksheet(sheets[title])

    # Add data.
    horizon = project.horizon
    _add_obj_row(project, sheets, indices, objects, horizon)
    _add_obj_row(project.horizon, sheets, indices, objects, horizon)
    for system in project.systems:
        _add_obj_row(system, sheets, indices, objects, horizon)
        for component in system.components:
            _add_obj_row(component, sheets, indices, objects, horizon)
            for maintenance in component.maintenance:
                _add_obj_row(maintenance, sheets, indices, objects, horizon)
                _add_obj_row(maintenance.task, sheets, indices, objects, horizon)
                _add_schedule_row(
                    schedule,
                    project,
                    system,
                    component,
                    maintenance,
                    wb,
                    indices,
                )

    _set_defined_names(wb)  # Add local defined names for UUIDs.

    for ws in wb.worksheets:  # Set style and lock all worksheets.
        _set_table_style(ws)
        _set_auto_width(ws)

    if path:
        wb.save(str(Path(path)))

    return wb