Skip to content

Commit a62361f

Browse files
authored
Merge pull request #184 from OpenDataServices/cove-954-update-openpyxl
Update openpyxl
2 parents aba7ef8 + c801c68 commit a62361f

File tree

3 files changed

+43
-34
lines changed

3 files changed

+43
-34
lines changed

flattentool/input.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
import traceback
1616
import datetime
1717
import pytz
18-
from openpyxl.utils import _get_column_letter, column_index_from_string
18+
from openpyxl.utils import column_index_from_string
19+
from openpyxl.utils.cell import _get_column_letter
1920
from flattentool.exceptions import DataErrorWarning
2021
from flattentool.lib import isint, parse_sheet_configuration
2122

@@ -580,18 +581,18 @@ def get_sheet_headings(self, sheet_name):
580581
return []
581582

582583
if self.vertical_orientation:
583-
return [cell.value for cell in worksheet.columns[skip_rows][configuration_line:]]
584+
return [cell.value for cell in worksheet[_get_column_letter(skip_rows + 1)][configuration_line:]]
584585

585586
try:
586-
return [cell.value for cell in worksheet.rows[skip_rows + configuration_line]]
587+
return [cell.value for cell in worksheet[skip_rows + configuration_line + 1]]
587588
except IndexError:
588589
# If the heading line is after data in the spreadsheet. i.e when skipRows
589590
return []
590591

591592
def get_sheet_configuration(self, sheet_name):
592593
worksheet = self.workbook[self.sheet_names_map[sheet_name]]
593-
if worksheet.rows[0][0].value == '#':
594-
return [cell.value for num, cell in enumerate(worksheet.rows[0]) if num != 0 and cell.value]
594+
if worksheet['A1'].value == '#':
595+
return [cell.value for num, cell in enumerate(worksheet[1]) if num != 0 and cell.value]
595596
else:
596597
return []
597598

@@ -609,14 +610,14 @@ def get_sheet_lines(self, sheet_name):
609610

610611
worksheet = self.workbook[self.sheet_names_map[sheet_name]]
611612
if self.vertical_orientation:
612-
header_row = worksheet.columns[skip_rows]
613-
remaining_rows = worksheet.columns[skip_rows + header_rows:]
613+
header_row = worksheet[_get_column_letter(skip_rows + 1)]
614+
remaining_rows = worksheet.iter_cols(min_col=skip_rows + header_rows + 1)
614615
if configuration_line:
615616
header_row = header_row[1:]
616-
remaining_rows = [row[1:] for row in remaining_rows]
617+
remaining_rows = worksheet.iter_cols(min_col=skip_rows + header_rows + 1, min_row=2)
617618
else:
618-
header_row = worksheet.rows[skip_rows + configuration_line]
619-
remaining_rows = worksheet.rows[skip_rows + configuration_line + header_rows:]
619+
header_row = worksheet[skip_rows + configuration_line + 1]
620+
remaining_rows = worksheet.iter_rows(min_row=skip_rows + configuration_line + header_rows + 1)
620621

621622
coli_to_header = {}
622623
for i, header in enumerate(header_row):

flattentool/tests/test_output.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ def test_blank_sheets(tmpdir):
3333
# Check XLSX is empty
3434
wb = openpyxl.load_workbook(tmpdir.join('release.xlsx').strpath)
3535
assert wb.get_sheet_names() == ['release']
36-
assert len(wb['release'].rows) == 1
37-
assert len(wb['release'].rows[0]) == 1
38-
assert wb['release'].rows[0][0].value == None
36+
rows = list(wb['release'].rows)
37+
assert len(rows) == 1
38+
assert len(rows[0]) == 1
39+
assert rows[0][0].value == None
3940

4041
# Check CSV is Empty
4142
assert tmpdir.join('release').listdir() == [ tmpdir.join('release').join('release.csv') ]
@@ -55,10 +56,12 @@ def test_populated_header(tmpdir):
5556
# Check XLSX
5657
wb = openpyxl.load_workbook(tmpdir.join('release.xlsx').strpath)
5758
assert wb.get_sheet_names() == ['release', 'b']
58-
assert len(wb['release'].rows) == 1
59-
assert [ x.value for x in wb['release'].rows[0] ] == [ 'a', 'd' ]
60-
assert len(wb['b'].rows) == 1
61-
assert [ x.value for x in wb['b'].rows[0] ] == [ 'ocid', 'c' ]
59+
rows = list(wb['release'].rows)
60+
assert len(rows) == 1
61+
assert [ x.value for x in rows[0] ] == [ 'a', 'd' ]
62+
b_rows = list(wb['b'].rows)
63+
assert len(b_rows) == 1
64+
assert [ x.value for x in b_rows[0] ] == [ 'ocid', 'c' ]
6265

6366
# Check CSV
6467
assert set(tmpdir.join('release').listdir()) == set([
@@ -84,10 +87,12 @@ def test_empty_lines(tmpdir):
8487
# Check XLSX
8588
wb = openpyxl.load_workbook(tmpdir.join('release.xlsx').strpath)
8689
assert wb.get_sheet_names() == ['release', 'b']
87-
assert len(wb['release'].rows) == 1
88-
assert [ x.value for x in wb['release'].rows[0] ] == [ 'a', 'd' ]
89-
assert len(wb['b'].rows) == 1
90-
assert [ x.value for x in wb['b'].rows[0] ] == [ 'ocid', 'c' ]
90+
rows = list(wb['release'].rows)
91+
assert len(rows) == 1
92+
assert [ x.value for x in rows[0] ] == [ 'a', 'd' ]
93+
b_rows = list(wb['b'].rows)
94+
assert len(b_rows) == 1
95+
assert [ x.value for x in b_rows[0] ] == [ 'ocid', 'c' ]
9196

9297
# Check CSV
9398
assert set(tmpdir.join('release').listdir()) == set([
@@ -115,14 +120,16 @@ def test_populated_lines(tmpdir):
115120
# Check XLSX
116121
wb = openpyxl.load_workbook(tmpdir.join('release.xlsx').strpath)
117122
assert wb.get_sheet_names() == ['release', 'b']
118-
assert len(wb['release'].rows) == 3
119-
assert [ x.value for x in wb['release'].rows[0] ] == [ 'a' ]
120-
assert [ x.value for x in wb['release'].rows[1] ] == [ 'cell1' ]
121-
assert [ x.value for x in wb['release'].rows[2] ] == [ 'cell2' ]
122-
assert len(wb['b'].rows) == 3
123-
assert [ x.value for x in wb['b'].rows[0] ] == [ 'ocid', 'c' ]
124-
assert [ x.value for x in wb['b'].rows[1] ] == [ None, 'cell3' ]
125-
assert [ x.value for x in wb['b'].rows[2] ] == [ None, 'cell4' ]
123+
rows = list(wb['release'].rows)
124+
assert len(rows) == 3
125+
assert [ x.value for x in rows[0] ] == [ 'a' ]
126+
assert [ x.value for x in rows[1] ] == [ 'cell1' ]
127+
assert [ x.value for x in rows[2] ] == [ 'cell2' ]
128+
b_rows = list(wb['b'].rows)
129+
assert len(b_rows) == 3
130+
assert [ x.value for x in b_rows[0] ] == [ 'ocid', 'c' ]
131+
assert [ x.value for x in b_rows[1] ] == [ None, 'cell3' ]
132+
assert [ x.value for x in b_rows[2] ] == [ None, 'cell4' ]
126133

127134
# Check CSV
128135
assert set(tmpdir.join('release').listdir()) == set([
@@ -146,10 +153,11 @@ def test_utf8(tmpdir):
146153
# Check XLSX
147154
wb = openpyxl.load_workbook(tmpdir.join('release.xlsx').strpath)
148155
assert wb.get_sheet_names() == ['release']
149-
assert len(wb['release'].rows) == 3
150-
assert [ x.value for x in wb['release'].rows[0] ] == [ 'é' ]
151-
assert [ x.value for x in wb['release'].rows[1] ] == [ 'éαГ😼𝒞人' ]
152-
assert [ x.value for x in wb['release'].rows[2] ] == [ 'cell2' ]
156+
rows = list(wb['release'].rows)
157+
assert len(rows) == 3
158+
assert [ x.value for x in rows[0] ] == [ 'é' ]
159+
assert [ x.value for x in rows[1] ] == [ 'éαГ😼𝒞人' ]
160+
assert [ x.value for x in rows[2] ] == [ 'cell2' ]
153161

154162
# Check CSV
155163
assert set(tmpdir.join('release').listdir()) == set([

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup
22
import sys
33

4-
install_requires = ['jsonref', 'schema', 'openpyxl>=2,<2.4', 'six', 'pytz', 'xmltodict']
4+
install_requires = ['jsonref', 'schema', 'openpyxl>=2.5', 'six', 'pytz', 'xmltodict']
55

66
if sys.version < '3':
77
install_requires.append('unicodecsv')

0 commit comments

Comments
 (0)