{"record":{"id":"5cb91ea5e618a242","repo":"pola-rs/polars","slug":"the-given-workbook-object-wb-filename-r-is-not-t","errorCode":null,"errorMessage":"the given workbook object {wb.filename!r} is not the parent of worksheet {ws.name!r}","messagePattern":"the given workbook object (.+?) is not the parent of worksheet (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/io/spreadsheet/_write_utils.py","lineNumber":582,"sourceCode":"\n\n@overload\ndef _xl_worksheet_in_workbook(\n    wb: Workbook, ws: Worksheet, *, return_worksheet: Literal[False] = ...\n) -> bool: ...\n@overload\ndef _xl_worksheet_in_workbook(\n    wb: Workbook, ws: Worksheet, *, return_worksheet: Literal[True]\n) -> Worksheet: ...\n\n\ndef _xl_worksheet_in_workbook(\n    wb: Workbook, ws: Worksheet, *, return_worksheet: bool = False\n) -> bool | Worksheet:\n    if any(ws is sheet for sheet in wb.worksheets()):\n        return ws if return_worksheet else True\n    msg = f\"the given workbook object {wb.filename!r} is not the parent of worksheet {ws.name!r}\"\n    raise ValueError(msg)\n\n\ndef _xl_setup_workbook(\n    workbook: Workbook | IO[bytes] | Path | str | None,\n    worksheet: str | Worksheet | None = None,\n    *,\n    use_zip64: bool = False,\n) -> tuple[Workbook, Worksheet, bool]:\n    \"\"\"Establish the target Excel workbook and worksheet.\"\"\"\n    from xlsxwriter import Workbook\n    from xlsxwriter.worksheet import Worksheet\n\n    if isinstance(workbook, Workbook):\n        wb, can_close = workbook, False\n        ws = (\n            worksheet\n            if (\n                isinstance(worksheet, Worksheet)","sourceCodeStart":564,"sourceCodeEnd":600,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/io/spreadsheet/_write_utils.py#L564-L600","documentation":"ValueError raised by _xl_worksheet_in_workbook during write_excel when the worksheet passed via the worksheet parameter does not belong to the workbook passed via the workbook parameter. The helper walks wb.worksheets() looking for the exact worksheet object; polars only writes into sheets it can verify are owned by the target workbook, to avoid corrupting another open workbook's state.","triggerScenarios":"Creating wb1 = Workbook('a.xlsx') and wb2 = Workbook('b.xlsx'), then pl.write_excel(df, workbook=wb1, worksheet=wb2.get_worksheet_by_name('Sheet1')).","commonSituations":"Multi-workbook report generation where worksheet handles are stored in a dict and paired with the wrong workbook; refactoring code that creates one workbook per sheet and mixing up variables; appending polars output to a template workbook while passing a worksheet handle from a different template instance.","solutions":["Pass the worksheet's actual parent: pl.write_excel(df, workbook=wb2, worksheet=ws_from_wb2).","Or reference the sheet by name within the correct workbook: worksheet='Sheet1' together with workbook=wb.","When iterating sheets across workbooks, keep (workbook, worksheet) pairs together instead of separate lookups."],"exampleFix":"# before\nws = wb_b.add_worksheet('data')\npl.write_excel(df, workbook=wb_a, worksheet=ws)  # ws belongs to wb_b\n\n# after\npl.write_excel(df, workbook=wb_b, worksheet=ws)","handlingStrategy":"validation","validationCode":"if worksheet is not None and workbook is not None:\n    import xlsxwriter\n    assert isinstance(workbook, xlsxwriter.Workbook)\n    assert any(worksheet is ws for ws in workbook.worksheets()), 'worksheet does not belong to workbook'\npl.write_excel(df, workbook=workbook, worksheet=worksheet)","typeGuard":null,"tryCatchPattern":"try:\n    pl.write_excel(df, workbook=wb, worksheet=ws)\nexcept ValueError as e:\n    if 'is not the parent of worksheet' in str(e):\n        raise ValueError(f'wrong workbook for {ws.name!r}; pass its actual parent Workbook') from e\n    raise","preventionTips":["Always pair a worksheet object with the workbook that produced it.","Reference sheets by name (worksheet='Sheet1') when the pairing could get mixed up.","In loops over multiple workbooks, carry (workbook, worksheet) tuples, never bare handles."],"tags":["excel","spreadsheet","write-excel","workbook","worksheet"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}