Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.27.32-dev3

### Enhancements

- Expose `open_excel_workbook()` for integrations that read one worksheet at a time while
retaining the worksheet cell limit and optional empty-row compaction.

## 0.27.32-dev2

### Enhancements
Expand Down
62 changes: 62 additions & 0 deletions test_unstructured/partition/test_xlsx_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import io

import openpyxl
import pytest

from test_unstructured.unit_utils import example_doc_path
from unstructured.errors import UnprocessableEntityError
from unstructured.partition.xlsx import open_excel_workbook


def workbook_bytes():
book = openpyxl.Workbook()
book.active["A1"] = "first"
book.active["Z1000000"] = "far"
book.create_sheet("second")["A1"] = "second"
stream = io.BytesIO()
book.save(stream)
return stream.getvalue()


def test_compaction_and_sheet_order(monkeypatch):
monkeypatch.setenv("XLSX_MAX_CELLS", "1000")
with open_excel_workbook(workbook_bytes(), compact_empty_rows=True) as reader:
assert reader.sheet_names == ["Sheet", "second"]
first = reader.parse("Sheet")
assert first.shape == (3, 26)
assert first.iloc[-1, -1] == "far"
assert reader.parse("second").iloc[0, 0] == "second"


def test_guard_before_dense_read(monkeypatch):
monkeypatch.setenv("XLSX_MAX_CELLS", "1000")
with pytest.raises(UnprocessableEntityError, match="XLSX_MAX_CELLS"):
with open_excel_workbook(workbook_bytes()):
pytest.fail("oversized workbook was opened")


def test_reader_closed_after_consumer_failure(monkeypatch):
from unstructured.partition import xlsx

monkeypatch.setenv("XLSX_MAX_CELLS", "1000")

closed = []
original = xlsx._RowCompactingOpenpyxlReader.close

def close(reader):
closed.append(True)
original(reader)

monkeypatch.setattr(xlsx._RowCompactingOpenpyxlReader, "close", close)
with pytest.raises(RuntimeError, match="consumer"):
with open_excel_workbook(workbook_bytes(), compact_empty_rows=True):
raise RuntimeError("consumer")
assert closed


def test_password_protected_workbook_is_rejected_before_reading():
with open(example_doc_path("password_protected.xlsx"), "rb") as f:
file_bytes = f.read()
with pytest.raises(UnprocessableEntityError, match="password protected"):
with open_excel_workbook(file_bytes):
pytest.fail("encrypted workbook was opened")
2 changes: 1 addition & 1 deletion unstructured/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.27.32-dev2" # pragma: no cover
__version__ = "0.27.32-dev3" # pragma: no cover
113 changes: 72 additions & 41 deletions unstructured/partition/xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import io
import struct
from contextlib import contextmanager
from functools import cached_property
from typing import IO, Any, Iterator, Optional

Expand Down Expand Up @@ -145,6 +146,73 @@ def partition_xlsx(
return elements


class ExcelWorkbookReader:
"""A validated workbook supporting worksheet-at-a-time reads."""

def __init__(self, reader: Any):
self._reader = reader

@property
def sheet_names(self) -> list[str]:
"""Worksheet titles in workbook order."""
return self._reader.sheet_names

def parse(self, sheet_name: str, *, header: int | None = None) -> pd.DataFrame:
"""Read one worksheet with the validated engine and row-compaction policy."""
return self._reader.parse(sheet_name=sheet_name, header=header)


@contextmanager
def open_excel_workbook(
file_bytes: bytes, *, compact_empty_rows: bool = False
) -> Iterator[ExcelWorkbookReader]:
"""Open a workbook after rejecting password-protected files and validating its cell count.

The returned Pandas reader supports reading individual sheets in workbook order. Callers
should release each data-frame before parsing the next sheet. Empty-row compaction is safe
for subtable detection, but must be disabled when sheet row coordinates are significant.
The context closes the reader even if parsing or processing a worksheet fails.
"""
_reject_encrypted_workbook(file_bytes)
if file_bytes.startswith(_ZIP_SIGNATURE):
engine = "openpyxl"
elif file_bytes.startswith(_OLE_SIGNATURE):
engine = "xlrd"
else:
raise UnprocessableEntityError("Not a valid XLSX or XLS file.")
compact_empty_rows = compact_empty_rows and engine == "openpyxl"
_check_worksheet_cell_count(file_bytes, engine, env_config.XLSX_MAX_CELLS, compact_empty_rows)
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
reader = (
_RowCompactingOpenpyxlReader(io.BytesIO(file_bytes))
if compact_empty_rows
else pd.ExcelFile(io.BytesIO(file_bytes), engine=engine)
)
try:
yield ExcelWorkbookReader(reader)
finally:
reader.close()


def _reject_encrypted_workbook(file_bytes: bytes) -> None:
"""Raise `UnprocessableEntityError` for a file that is not an Office file or is encrypted."""
try:
office_file = OfficeFile(io.BytesIO(file_bytes))
except FileFormatError as e:
raise UnprocessableEntityError("Not a valid XLSX file.") from e

try:
is_encrypted = office_file.is_encrypted()
except struct.error:
# -- msoffcrypto reads an XLS "Workbook" stream as 4-byte record headers and raises when
# -- the stream ends in fewer than 4 bytes of padding, as in many files written by
# -- `xlwt` (nolze/msoffcrypto-tool#83). The FilePass record of an encrypted XLS
# -- directly follows its BOF record, so the check finds it before any padding.
is_encrypted = False

if is_encrypted:
raise UnprocessableEntityError("XLSX file is password protected.")


class _XlsxPartitionerOptions:
"""Encapsulates partitioning option validation, computation, and application of defaults."""

Expand Down Expand Up @@ -199,47 +267,10 @@ def metadata_file_path(self) -> str | None:
@cached_property
def sheets(self) -> dict[str, pd.DataFrame]:
"""The spreadsheet worksheets, each as a data-frame mapped by sheet-name."""
try:
office_file = OfficeFile(io.BytesIO(self._file_bytes))
except FileFormatError as e:
raise UnprocessableEntityError("Not a valid XLSX file.") from e

try:
is_encrypted = office_file.is_encrypted()
except struct.error:
# -- msoffcrypto reads an XLS "Workbook" stream as 4-byte record headers and raises when
# -- the stream ends in fewer than 4 bytes of padding, as in many files written by
# -- `xlwt` (nolze/msoffcrypto-tool#83). The FilePass record of an encrypted XLS
# -- directly follows its BOF record, so the check finds it before any padding.
is_encrypted = False

if is_encrypted:
raise UnprocessableEntityError("XLSX file is password protected.")

# -- Pandas allocates a dense data-frame spanning every worksheet out to its farthest
# -- populated cell, so a tiny file with one far-away cell can exhaust memory. Measure
# -- that span cheaply before reading, and pin the engine to the one that was measured.
# -- A whole worksheet is rendered as-is, empty rows and all, so only subtable detection
# -- reads it with runs of empty rows compacted.
engine = self._excel_engine
compact_empty_rows = engine == "openpyxl" and self.find_subtable
_check_worksheet_cell_count(
self._file_bytes, engine, env_config.XLSX_MAX_CELLS, compact_empty_rows
)

if compact_empty_rows:
reader = _RowCompactingOpenpyxlReader(io.BytesIO(self._file_bytes))
try:
return reader.parse(sheet_name=None, header=self.header_row_idx)
finally:
reader.close()

return pd.read_excel(
io.BytesIO(self._file_bytes),
sheet_name=None,
header=self.header_row_idx,
engine=engine,
)
with open_excel_workbook(self._file_bytes, compact_empty_rows=self.find_subtable) as reader:
return {
name: reader.parse(name, header=self.header_row_idx) for name in reader.sheet_names
}

@cached_property
def _excel_engine(self) -> str:
Expand Down
Loading