pola-rs/polars · error · ValueError

the given workbook object

Error message

the given workbook object {wb.filename!r} is not the parent of worksheet {ws.name!r}

What it means

This internal helper verifies that a Worksheet object actually belongs to the supplied Workbook before write_excel reuses them. It raises ValueError when the worksheet's identity is not found among the workbook's sheets, i.e. the two objects come from different workbooks.

Solutions

  1. Pass the worksheet and the workbook it actually belongs to (get the sheet via ws's own parent workbook).
  2. Look up the worksheet from the target workbook: worksheet = wb.get_worksheet_by_name('Sheet1') and pass that.
  3. If reusing a live workbook/worksheet pair, make sure both come from the same write_excel invocation earlier in the session.

Example fix

// before
ws = other_wb.get_worksheet_by_name('Sheet1')
df.write_excel('out.xlsx', workbook=wb, worksheet=ws)
// after
ws = wb.get_worksheet_by_name('Sheet1')  # sheet from the same workbook
df.write_excel('out.xlsx', workbook=wb, worksheet=ws)
Defensive patterns

Strategy: validation

Validate before calling

assert any(ws is s for s in wb.worksheets()), 'worksheet does not belong to the given workbook'

Type guard

def sheet_in_workbook(ws, wb) -> bool:
    return any(ws is s for s in wb.worksheets())

Try / catch

try:
    df.write_excel('out.xlsx', workbook=wb, worksheet=ws)
except ValueError as e:
    if 'not the parent' in str(e):
        ws = wb.get_worksheet_by_name(ws.name)

Prevention

When it happens

Trigger: Passing workbook=wb and worksheet=ws where ws was created by (or taken from) a different Workbook instance, e.g. ws = other_wb.get_worksheet_by_name('Sheet1').

Common situations: Mixing worksheets across two xlsxwriter Workbook objects loaded in a session; grabbing a sheet from a previously created workbook then trying to write into a new one.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/5cb91ea5e618a242. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/io/spreadsheet/_write_utils.py:582


@overload
def _xl_worksheet_in_workbook(
    wb: Workbook, ws: Worksheet, *, return_worksheet: Literal[False] = ...
) -> bool: ...
@overload
def _xl_worksheet_in_workbook(
    wb: Workbook, ws: Worksheet, *, return_worksheet: Literal[True]
) -> Worksheet: ...


def _xl_worksheet_in_workbook(
    wb: Workbook, ws: Worksheet, *, return_worksheet: bool = False
) -> bool | Worksheet:
    if any(ws is sheet for sheet in wb.worksheets()):
        return ws if return_worksheet else True
    msg = f"the given workbook object {wb.filename!r} is not the parent of worksheet {ws.name!r}"
    raise ValueError(msg)


def _xl_setup_workbook(
    workbook: Workbook | IO[bytes] | Path | str | None,
    worksheet: str | Worksheet | None = None,
    *,
    use_zip64: bool = False,
) -> tuple[Workbook, Worksheet, bool]:
    """Establish the target Excel workbook and worksheet."""
    from xlsxwriter import Workbook
    from xlsxwriter.worksheet import Worksheet

    if isinstance(workbook, Workbook):
        wb, can_close = workbook, False
        ws = (
            worksheet
            if (
                isinstance(worksheet, Worksheet)

View on GitHub (pinned to fe841f959e)