Repository navigation
feat(xlsx): expose a guarded worksheet-at-a-time workbook reader #4562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7b23593
feat(xlsx): Expose a protected workbook reader
CyMule 7fc2d6a
fix(xlsx): Reject password-protected workbooks in the workbook reader
CyMule ca90089
chore(release): Target 0.27.32-dev5
CyMule e39051a
chore: Merge main and preserve development release notes
CyMule ecb6531
chore: Merge main and preserve development release notes
CyMule fe0a71d
Merge remote-tracking branch 'origin/main' into upd-4562
CyMule File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.