pola-rs/polars · error · TypeError
worksheet object requires the parent workbook object; found…
Error message
worksheet object requires the parent workbook object; found workbook={workbook!r} What it means
write_excel allows passing an existing Worksheet object to write into, but only together with its parent Workbook object. This TypeError fires when a Worksheet is supplied but the workbook parameter is None or not a Workbook instance.
Solutions
- Pass the parent workbook alongside the worksheet: workbook=wb, worksheet=ws.
- If you only have a file path, omit the worksheet object and use the worksheet name string instead.
- Keep the Workbook returned/captured from the earlier write and reuse that object, not a new one.
Example fix
// before
df2.write_excel('out.xlsx', worksheet=ws)
// after
df2.write_excel('out.xlsx', workbook=wb, worksheet=ws) Defensive patterns
Strategy: validation
Validate before calling
if isinstance(worksheet, Worksheet):
assert isinstance(workbook, Workbook), 'worksheet requires its parent workbook object' Type guard
def can_pass_worksheet(workbook, worksheet) -> bool:
from xlsxwriter.workbook import Workbook
from xlsxwriter.worksheet import Worksheet
return not isinstance(worksheet, Worksheet) or isinstance(workbook, Workbook) Try / catch
try:
df.write_excel('out.xlsx', worksheet=ws)
except TypeError as e:
if 'parent workbook' in str(e):
df.write_excel('out.xlsx', workbook=ws.workbook, worksheet=ws) Prevention
- Keep the Workbook object from the original write call alongside the Worksheet
- Use worksheet name strings when you do not have the workbook handle
When it happens
Trigger: df.write_excel(worksheet=some_worksheet) without workbook=..., or workbook= given as a path/str while worksheet is a Worksheet object.
Common situations: Users capture a worksheet from a previous write_excel call to append more data but forget to pass the workbook object that owns it.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- the given workbook object
- cannot create a second
- cannot specify both `columns` and `read_options["columns"]`
- cannot specify both `columns` and…
- cannot specify both `infer_schema_length` and…
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/6907b124b2c7d7c3.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/io/spreadsheet/_write_utils.py:611
from xlsxwriter.worksheet import Worksheet
if isinstance(workbook, Workbook):
wb, can_close = workbook, False
ws = (
worksheet
if (
isinstance(worksheet, Worksheet)
and _xl_worksheet_in_workbook(wb, worksheet)
)
# Argument `Worksheet | str | None` is not assignable to parameter `name`
# with type `str`.
else wb.get_worksheet_by_name(
name=worksheet # pyrefly: ignore[bad-argument-type]
)
)
elif isinstance(worksheet, Worksheet):
msg = f"worksheet object requires the parent workbook object; found workbook={workbook!r}"
raise TypeError(msg)
else:
workbook_options = {
"use_zip64": use_zip64,
"nan_inf_to_errors": True,
"strings_to_formulas": False,
"default_date_format": _XL_DEFAULT_DTYPE_FORMATS_[Date],
}
if isinstance(workbook, BytesIO):
wb, ws, can_close = Workbook(workbook, workbook_options), None, True
else:
file: Path | IO[bytes]
if workbook is None:
file = Path("dataframe.xlsx")
elif isinstance(workbook, str):
file = Path(workbook)
else:
file = workbook
View on GitHub (pinned to fe841f959e)