Skip to content

Commit e0b3a97

Browse files
committed
ODSReader: Parse numbers correctly from .ods files
#343
1 parent be412eb commit e0b3a97

File tree

5 files changed

+18
-7
lines changed

5 files changed

+18
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88

99
### Fixed
1010

11-
- Fix parsing number formatting from .ods files https://github.com/OpenDataServices/flatten-tool/pull/373
11+
- Fix parsing date and number formatting from .ods files https://github.com/OpenDataServices/flatten-tool/pull/373
1212

1313
## [0.15.3] - 2020-02-23
1414

flattentool/ODSReader.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import odf.opendocument
2323
from odf.table import Table, TableCell, TableRow
2424

25-
2625
# Backport for datetime.fromisoformat, which is new in Python 3.7
2726
backports.datetime_fromisoformat.MonkeyPatch.patch_fromisoformat()
2827

@@ -79,10 +78,16 @@ def readSheet(self, sheet):
7978
)
8079
)
8180
if value_type == "float":
81+
value = cell.attributes.get(
82+
(
83+
"urn:oasis:names:tc:opendocument:xmlns:office:1.0",
84+
"value",
85+
)
86+
)
8287
if "." in str(cell):
83-
arrCells[count] = float(str(cell))
88+
arrCells[count] = float(value)
8489
else:
85-
arrCells[count] = int(str(cell))
90+
arrCells[count] = int(value)
8691
elif value_type == "date":
8792
date_value = cell.attributes.get(
8893
(
112 Bytes
Binary file not shown.
-1.44 KB
Binary file not shown.

flattentool/tests/test_input_SpreadsheetInput.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,18 @@ def test_xlsx_input_types(self):
193193
assert list(xlsxinput.get_sheet_lines("main")) == [
194194
{
195195
"colInt": 1,
196-
"colFloat": 1.2,
196+
"colFloat": 1000.2,
197+
"colFloatComma": 1000.2,
197198
"colDate": datetime.datetime(2020, 3, 5),
198199
"colDateTime": datetime.datetime(2020, 2, 7, 16, 41, 0, 1),
200+
None: None,
199201
}
200202
]
201203
assert type(list(xlsxinput.get_sheet_lines("main"))[0]["colInt"]) == int
202204
assert type(list(xlsxinput.get_sheet_lines("main"))[0]["colFloat"]) == float
205+
assert (
206+
type(list(xlsxinput.get_sheet_lines("main"))[0]["colFloatComma"]) == float
207+
)
203208
assert xlsxinput.sub_sheet_names == ["main"]
204209

205210
def test_ods_input_types(self):
@@ -210,13 +215,14 @@ def test_ods_input_types(self):
210215
assert list(odsinput.get_sheet_lines("main")) == [
211216
{
212217
"colInt": 1,
213-
"colFloat": 1.2,
218+
"colFloat": 1000.2,
219+
"colFloatComma": 1000.2,
214220
"colDate": datetime.datetime(2020, 3, 5),
215221
"colDateTime": datetime.datetime(2020, 2, 7, 16, 41),
216222
}
217223
]
218224
assert type(list(odsinput.get_sheet_lines("main"))[0]["colInt"]) == int
219-
assert type(list(odsinput.get_sheet_lines("main"))[0]["colFloat"]) == float
225+
assert type(list(odsinput.get_sheet_lines("main"))[0]["colFloatComma"]) == float
220226
assert list(odsinput.sub_sheet_names) == ["main"]
221227

222228
def test_xlsx_input_integer2(self):

0 commit comments

Comments
 (0)