Skip to content

Commit 704ee59

Browse files
committed
Fix calling from_xls passing custom options. #355
1 parent daf80dd commit 704ee59

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

benedict/serializers/xls.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,15 @@ def _get_sheet_columns_indexes(self, columns_count):
5252
return list(range(columns_count))
5353

5454
def _decode_legacy(self, s, **kwargs):
55-
filepath = s
55+
options = {}
56+
options["filename"] = s
57+
options["logfile"] = kwargs.pop("logfile", None)
58+
options["verbosity"] = kwargs.pop("verbosity", 0) or 0
59+
options["use_mmap"] = kwargs.pop("use_mmap", False) or False
60+
options["file_contents"] = kwargs.pop("file_contents", None)
5661

5762
# load the worksheet
58-
workbook = open_workbook(filename=filepath)
63+
workbook = open_workbook(**options)
5964

6065
# get sheet by index or by name
6166
sheet_index, sheet_name = self._get_sheet_index_and_name_from_options(**kwargs)
@@ -100,10 +105,15 @@ def _decode_legacy(self, s, **kwargs):
100105
return items
101106

102107
def _decode(self, s, **kwargs):
103-
filepath = s
108+
options = {}
109+
options["filename"] = s
110+
options["read_only"] = True
111+
options["data_only"] = kwargs.pop("data_only", False)
112+
options["keep_links"] = kwargs.pop("keep_links", True)
113+
options["keep_vba"] = kwargs.pop("keep_vba", True)
104114

105115
# load the worksheet
106-
workbook = load_workbook(filename=filepath, read_only=True)
116+
workbook = load_workbook(**options)
107117

108118
# get sheet by index or by name
109119
sheet_index, sheet_name = self._get_sheet_index_and_name_from_options(**kwargs)

tests/github/test_issue_0355.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import pathlib
2+
import unittest
3+
4+
from benedict import benedict
5+
6+
7+
class github_issue_0355_test_case(unittest.TestCase):
8+
"""
9+
This class describes a github issue 0355 test case.
10+
https://github.com/fabiocaccamo/python-benedict/issues/355
11+
12+
To run this specific test:
13+
- Run python -m unittest tests.github.test_issue_0355
14+
"""
15+
16+
def test_from_xls_with_options(self):
17+
# print(pathlib.Path("./test_issue_0144.json"))
18+
filepath = pathlib.Path("tests/github/test_issue_0355.xlsx")
19+
20+
d = benedict.from_xls(filepath)
21+
# print(d.dump())
22+
self.assertEqual(
23+
d,
24+
{
25+
"values": [
26+
{
27+
"formula": '="A2 value is: "&A2',
28+
"integer": 123,
29+
"text": "abc",
30+
}
31+
]
32+
},
33+
)
34+
35+
d = benedict.from_xls(filepath, data_only=True)
36+
# print(d.dump())
37+
self.assertEqual(
38+
d,
39+
{
40+
"values": [
41+
{
42+
"formula": "A2 value is: abc",
43+
"integer": 123,
44+
"text": "abc",
45+
}
46+
]
47+
},
48+
)

tests/github/test_issue_0355.xlsx

8.95 KB
Binary file not shown.

0 commit comments

Comments
 (0)