From 704ee5936e6f31b27237f1e7c19d913fde0c9717 Mon Sep 17 00:00:00 2001 From: Fabio Caccamo Date: Wed, 27 Dec 2023 11:06:42 +0100 Subject: [PATCH] Fix calling `from_xls` passing custom options. #355 --- benedict/serializers/xls.py | 18 ++++++++--- tests/github/test_issue_0355.py | 48 ++++++++++++++++++++++++++++++ tests/github/test_issue_0355.xlsx | Bin 0 -> 9165 bytes 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 tests/github/test_issue_0355.py create mode 100644 tests/github/test_issue_0355.xlsx diff --git a/benedict/serializers/xls.py b/benedict/serializers/xls.py index 64ce785..bb52999 100644 --- a/benedict/serializers/xls.py +++ b/benedict/serializers/xls.py @@ -52,10 +52,15 @@ class XLSSerializer(AbstractSerializer): return list(range(columns_count)) def _decode_legacy(self, s, **kwargs): - filepath = s + options = {} + options["filename"] = s + options["logfile"] = kwargs.pop("logfile", None) + options["verbosity"] = kwargs.pop("verbosity", 0) or 0 + options["use_mmap"] = kwargs.pop("use_mmap", False) or False + options["file_contents"] = kwargs.pop("file_contents", None) # load the worksheet - workbook = open_workbook(filename=filepath) + workbook = open_workbook(**options) # get sheet by index or by name sheet_index, sheet_name = self._get_sheet_index_and_name_from_options(**kwargs) @@ -100,10 +105,15 @@ class XLSSerializer(AbstractSerializer): return items def _decode(self, s, **kwargs): - filepath = s + options = {} + options["filename"] = s + options["read_only"] = True + options["data_only"] = kwargs.pop("data_only", False) + options["keep_links"] = kwargs.pop("keep_links", True) + options["keep_vba"] = kwargs.pop("keep_vba", True) # load the worksheet - workbook = load_workbook(filename=filepath, read_only=True) + workbook = load_workbook(**options) # get sheet by index or by name sheet_index, sheet_name = self._get_sheet_index_and_name_from_options(**kwargs) diff --git a/tests/github/test_issue_0355.py b/tests/github/test_issue_0355.py new file mode 100644 index 0000000..dcc6536 --- /dev/null +++ b/tests/github/test_issue_0355.py @@ -0,0 +1,48 @@ +import pathlib +import unittest + +from benedict import benedict + + +class github_issue_0355_test_case(unittest.TestCase): + """ + This class describes a github issue 0355 test case. + https://github.com/fabiocaccamo/python-benedict/issues/355 + + To run this specific test: + - Run python -m unittest tests.github.test_issue_0355 + """ + + def test_from_xls_with_options(self): + # print(pathlib.Path("./test_issue_0144.json")) + filepath = pathlib.Path("tests/github/test_issue_0355.xlsx") + + d = benedict.from_xls(filepath) + # print(d.dump()) + self.assertEqual( + d, + { + "values": [ + { + "formula": '="A2 value is: "&A2', + "integer": 123, + "text": "abc", + } + ] + }, + ) + + d = benedict.from_xls(filepath, data_only=True) + # print(d.dump()) + self.assertEqual( + d, + { + "values": [ + { + "formula": "A2 value is: abc", + "integer": 123, + "text": "abc", + } + ] + }, + ) diff --git a/tests/github/test_issue_0355.xlsx b/tests/github/test_issue_0355.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..2efd9d97d9f92370deb589c14703a39d3a89d563 GIT binary patch literal 9165 zcmeHN^;cX;w{6^Ag9Hd3+=4@JXdFUt8h3XKngETvLm;?YAP}Unpur(H2^QRf`|D)p zy~!~1{(|qTweIa7s?J$e)q9^hwa-h?UA)C$>q^wqK?tEA<~A+LRxbrdX|#; z)72{mef3%}5b~(~M%QOSQC_w@Gp;|7lV(y=h4(g(18D-pl83MYD>VCGpG(W*fD;`m zuGZrz$WaFrIJ9hHcwcvT7D+T8f9T*iNCM)$53;2a_r<|5I?P&r%UUj)sY|pWCBUGs zVTCf#uP8ylFp7kYY7;P%`lLdN`u**9k=3_BaYbfA?6N*>Gz(ogTmEp$BZD~c3NchY ztuSOCcjexXG!eo~>Y{T$3WmFx++U9E?DJA&;dI1N`nYjZ;y~2lm5+jX=pS%^V^`;* zEROvyWFpp`=ej?x@O}kP!Uw{jLcS!lXfeSCh1dN(96;r79Ie-2r#^x9C_pKQ2IVNo z+0533jrHgIzuf!}Cgg8RFOOGL{)3x0p?&95i?P_^@*ZNc%~UV^0^}C3>Z9^#NEadW zWY{lAg5hNRTm5bZ<`;#cw)?2gR=CR}aPbAH8$2pPQm&lbke<>xCrdk(FL&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