{"record":{"id":"9c1968aad0f3859b","repo":"pola-rs/polars","slug":"empty-excel-sheet-if-you-want-to-read-this-as-an-empty","errorCode":null,"errorMessage":"empty Excel sheet\n\nIf you want to read this as an empty DataFrame, set `raise_if_empty=False`.","messagePattern":"empty Excel sheet\n\nIf you want to read this as an empty DataFrame, set `raise_if_empty=False`\\.","errorType":"exception","errorClass":"NoDataError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/io/spreadsheet/functions.py","lineNumber":1013,"sourceCode":"                ):\n                    null_cols.append(col_name)\n        if null_cols:\n            df = df.drop(*null_cols)\n\n    if df.height == df.width == 0:\n        return _empty_frame(raise_if_empty)\n    if drop_empty_rows:\n        return df.filter(~F.all_horizontal(F.all().is_null()))\n    return df\n\n\ndef _empty_frame(raise_if_empty: bool) -> pl.DataFrame:  # noqa: FBT001\n    if raise_if_empty:\n        msg = (\n            \"empty Excel sheet\"\n            \"\\n\\nIf you want to read this as an empty DataFrame, set `raise_if_empty=False`.\"\n        )\n        raise NoDataError(msg)\n    return pl.DataFrame()\n\n\ndef _reorder_columns(\n    df: pl.DataFrame, columns: Sequence[int] | Sequence[str] | None\n) -> pl.DataFrame:\n    if columns:\n        from polars.selectors import by_index, by_name\n\n        cols = (\n            by_index(*columns)\n            if is_non_empty_sequence_of(columns, int)\n            else by_name(*columns)\n        )\n        df = df.select(cols)\n    return df\n\n","sourceCodeStart":995,"sourceCodeEnd":1031,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/io/spreadsheet/functions.py#L995-L1031","documentation":"The target Excel/ODS sheet contains no data rows, and polars refuses to silently return an empty DataFrame unless the caller opted out with `raise_if_empty=False`. `_empty_frame` is invoked by the openpyxl reader, null-dropping cleanup, and the CSV-buffer path when a sheet turns out to be blank. This makes truly-empty input an explicit, catchable NoDataError.","triggerScenarios":"`pl.read_excel('empty.xlsx')` on a sheet with no used range; a sheet containing only formatting; reading a sheet whose rows are all null (dropped by _drop_null_data); passing sheet_name pointing at a blank tab with the default raise_if_empty=True.","commonSituations":"Templates with header-only or blank sheets; generated workbooks where a step wrote no data; iterating over many sheets where some are empty.","solutions":["Pass `raise_if_empty=False` to read_excel to get an empty DataFrame instead of an error.","Catch `polars.exceptions.NoDataError` and skip/handle empty sheets.","Before reading, check the sheet has data (e.g. via openpyxl `ws.max_row`) and skip blank tabs."],"exampleFix":"// before\npl.read_excel('report.xlsx', sheet_name='summary')  # NoDataError if blank\n// after\npl.read_excel('report.xlsx', sheet_name='summary', raise_if_empty=False)\n// or: try: ... except pl.exceptions.NoDataError: df = pl.DataFrame()","handlingStrategy":"try-catch","validationCode":"import openpyxl\nws = openpyxl.load_workbook(path, read_only=True)[sheet_name]\nif ws.max_row is None or ws.max_row < 1:\n    df = pl.DataFrame()  # skip read entirely","typeGuard":null,"tryCatchPattern":"try:\n    df = pl.read_excel(path, sheet_name=sheet_name)\nexcept pl.exceptions.NoDataError:\n    df = pl.DataFrame()  # or: pass raise_if_empty=False upfront","preventionTips":["Pass raise_if_empty=False when iterating over many sheets","Skip known-blank template sheets before reading","Check ws.max_row / ws.max_column with openpyxl before reading"],"tags":["polars","excel","empty-data"],"backgroundTag":"empty-required-field","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}