Skip to content

Commit e82bfa4

Browse files
committed
ENH get_effective_cell for getting the contents of Excel cell when the cell is merged (GH4672)
1 parent 8224ed7 commit e82bfa4

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pandas 0.13
4444
- Text parser now treats anything that reads like inf ("inf", "Inf", "-Inf",
4545
"iNf", etc.) to infinity. (:issue:`4220`, :issue:`4219`), affecting
4646
``read_table``, ``read_csv``, etc.
47+
- Created get_effective_cell for getting the contents of Excel cell
48+
when the cell is merged (:issue:`4672`)
4749

4850
**API Changes**
4951

pandas/io/excel.py

+14
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,17 @@ def _writecells_xls(self, cells, sheet_name, startrow, startcol):
461461
wks.write(startrow + cell.row,
462462
startcol + cell.col,
463463
val, style)
464+
465+
def get_effective_cell(sheet, rowx, colx):
466+
import xlrd # throw an ImportError if we need to
467+
468+
cell_at_loc = sheet.cell(rowx, colx)
469+
if len(sheet.merged_cells) == 0 or cell_at_loc.ctype != xlrd.XL_CELL_BLANK:
470+
return sheet.cell(rowx, colx)
471+
472+
for merged_cell in sheet.merged_cells:
473+
rlo, rhi, clo, chi = merged_cell
474+
if rowx >= rlo and rowx < rhi and colx >= clo and colx < chi:
475+
return sheet.cell(rlo, clo)
476+
477+
return cell_at_loc
17 KB
Binary file not shown.

pandas/io/tests/test_excel.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import pandas.io.parsers as parsers
1919
from pandas.io.parsers import (read_csv, read_table, read_fwf,
2020
TextParser, TextFileReader)
21-
from pandas.io.excel import ExcelFile, ExcelWriter, read_excel
21+
from pandas.io.excel import ExcelFile, ExcelWriter, read_excel, get_effective_cell
2222
from pandas.util.testing import (assert_almost_equal,
2323
assert_series_equal,
2424
network,
@@ -259,6 +259,35 @@ def test_excel_table(self):
259259
skip_footer=1)
260260
tm.assert_frame_equal(df4, df.ix[:-1])
261261
tm.assert_frame_equal(df4, df5)
262+
263+
def test_read_effective_cells(self):
264+
_skip_if_no_xlrd()
265+
import xlrd
266+
267+
pth = os.path.join(self.dirpath, 'merged_effective.xls')
268+
xls = ExcelFile(pth, formatting_info=True)
269+
book = xls.book
270+
sheet = book.sheet_by_index(0)
271+
self.assertEqual(get_effective_cell(sheet, 0, 0).value, 1)
272+
self.assertEqual(get_effective_cell(sheet, 0, 4).value, 5)
273+
274+
self.assertEqual(get_effective_cell(sheet, 1, 0).value, "a")
275+
self.assertEqual(get_effective_cell(sheet, 1, 1).value, "b") #Top left of merged
276+
self.assertEqual(get_effective_cell(sheet, 1, 2).value, "b") #merged
277+
self.assertEqual(get_effective_cell(sheet, 1, 3).value, "c")
278+
self.assertEqual(get_effective_cell(sheet, 1, 4).value, "d")
279+
280+
self.assert_(get_effective_cell(sheet, 2, 0).ctype in(xlrd.XL_CELL_BLANK, xlrd.XL_CELL_EMPTY))
281+
self.assertEqual(get_effective_cell(sheet, 2, 1).value, "b") #merged
282+
self.assertEqual(get_effective_cell(sheet, 2, 2).value, "b") #merged
283+
self.assert_(get_effective_cell(sheet, 2, 3).ctype in(xlrd.XL_CELL_BLANK, xlrd.XL_CELL_EMPTY))
284+
self.assert_(get_effective_cell(sheet, 2, 4).ctype in(xlrd.XL_CELL_BLANK, xlrd.XL_CELL_EMPTY))
285+
286+
self.assertEqual(get_effective_cell(sheet, 3, 0).value, 1)
287+
self.assert_(get_effective_cell(sheet, 3, 1).ctype in(xlrd.XL_CELL_BLANK, xlrd.XL_CELL_EMPTY))
288+
self.assert_(get_effective_cell(sheet, 3, 2).ctype in(xlrd.XL_CELL_BLANK, xlrd.XL_CELL_EMPTY))
289+
self.assertEqual(get_effective_cell(sheet, 3, 3).value, 4)
290+
self.assertEqual(get_effective_cell(sheet, 3, 4).value, 5)
262291

263292
def test_excel_read_buffer(self):
264293
_skip_if_no_xlrd()

0 commit comments

Comments
 (0)