-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Monkey patched openpyxl for ranges #33
- Loading branch information
Showing
5 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains 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,43 @@ | ||
import pytest | ||
from openpyxl.utils import range_boundaries | ||
from xlsx2html import xlsx2html | ||
from xlsx2html.parser.parser import XLSXParser | ||
|
||
|
||
def test_monkey_patched(): | ||
assert range_boundaries._monkey | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"range,expect_result", | ||
[ | ||
["A:A", (1, 1, 1, 1048576)], | ||
["2:2", (1, 2, 18278, 2)], | ||
["B2:B", (2, 2, 2, 1048576)], | ||
["D2:2", (4, 2, 18278, 2)], | ||
], | ||
) | ||
def test_range_boundaries(range, expect_result): | ||
result = range_boundaries(range) | ||
assert result == expect_result | ||
|
||
|
||
@pytest.mark.skip("TODO optimize large merges") | ||
def test_parse_merge_cells(fixture_file): | ||
p = XLSXParser(filepath=fixture_file("merge_cells.xlsx")) | ||
result = p.get_sheet() | ||
assert result | ||
assert len(result.cols) == 4 | ||
assert len(result.rows) == 11 | ||
|
||
|
||
@pytest.mark.skip("TODO optimize large merges") | ||
@pytest.mark.webtest() | ||
def test_merge_cells(temp_file, browser, screenshot_regression, fixture_file): | ||
browser.driver.set_window_size(1280, 1024) | ||
out_file = temp_file() | ||
|
||
xlsx2html(fixture_file("merge_cells.xlsx"), out_file, locale="en") | ||
|
||
browser.visit("file://" + out_file) | ||
screenshot_regression() |
This file contains 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 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,74 @@ | ||
# type: ignore | ||
import functools | ||
|
||
import openpyxl | ||
from openpyxl import utils | ||
from openpyxl.utils import cell | ||
from openpyxl.worksheet.cell_range import CellRange | ||
|
||
old_range_boundaries = cell.range_boundaries | ||
|
||
MAX_COL_INDEX = 18278 | ||
MAX_ROW_INDEX = 1048576 | ||
|
||
|
||
@functools.wraps(old_range_boundaries) | ||
def new_range_boundaries(range_string): | ||
""" | ||
patch for handle string like | ||
A:A - all column | ||
2:2 - all row | ||
A2:A - from row 2 to end rows | ||
B3:3 - from column B to end columns | ||
""" | ||
from openpyxl.utils.cell import ABSOLUTE_RE | ||
|
||
m = ABSOLUTE_RE.match(range_string) | ||
if m: | ||
min_col, min_row, sep, max_col, max_row = m.groups() | ||
if sep: | ||
if not max_col: | ||
max_col = utils.get_column_letter(MAX_COL_INDEX) | ||
if not max_row: | ||
max_row = str(MAX_ROW_INDEX) | ||
range_string = "".join( | ||
[min_col or utils.get_column_letter(1), min_row or "1", ":", max_col, max_row] | ||
) | ||
|
||
min_col, min_row, max_col, max_row = old_range_boundaries(range_string) | ||
return min_col or 1, min_row or 1, max_col or MAX_COL_INDEX, max_row or MAX_ROW_INDEX | ||
|
||
|
||
new_range_boundaries._monkey = True | ||
cell.range_boundaries = new_range_boundaries | ||
utils.range_boundaries = new_range_boundaries | ||
openpyxl.utils = utils | ||
|
||
|
||
def CellRange__init__( | ||
self, range_string=None, min_col=None, min_row=None, max_col=None, max_row=None, title=None | ||
): | ||
if range_string is not None: | ||
if "!" in range_string: | ||
from openpyxl.utils import range_to_tuple | ||
|
||
title, (min_col, min_row, max_col, max_row) = range_to_tuple(range_string) | ||
else: | ||
# FIX | ||
min_col, min_row, max_col, max_row = new_range_boundaries(range_string) | ||
|
||
self.min_col = min_col | ||
self.min_row = min_row | ||
self.max_col = max_col | ||
self.max_row = max_row | ||
self.title = title | ||
|
||
if min_col > max_col: | ||
fmt = "{max_col} must be greater than {min_col}" | ||
raise ValueError(fmt.format(min_col=min_col, max_col=max_col)) | ||
if min_row > max_row: | ||
fmt = "{max_row} must be greater than {min_row}" | ||
raise ValueError(fmt.format(min_row=min_row, max_row=max_row)) | ||
|
||
|
||
CellRange.__init__ = CellRange__init__ |
This file contains 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