Skip to content

Commit 4541c37

Browse files
committed
Fix for openpyxl < 2, and for issue pandas-dev#11408
If using openpyxl < 2, and value is a string that could be a number, force a string to be written out. If using openpyxl >= 2.2, then fix issue pandas-dev#11408 to do with merging cells
1 parent cb1a9a8 commit 4541c37

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

pandas/io/excel.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,12 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0):
708708
for cell in cells:
709709
colletter = get_column_letter(startcol + cell.col + 1)
710710
xcell = wks.cell("%s%s" % (colletter, startrow + cell.row + 1))
711-
xcell.value = _conv_value(cell.val)
711+
new_data_type = xcell.data_type_for_value(cell.val)
712+
if (isinstance(cell.val, compat.string_types)
713+
and new_data_type != xcell.TYPE_STRING):
714+
xcell.set_explicit_value(cell.val)
715+
else:
716+
xcell.value = _conv_value(cell.val)
712717
style = None
713718
if cell.style:
714719
style = self._convert_to_style(cell.style)
@@ -1240,7 +1245,7 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0):
12401245
start_row=startrow + cell.row + 1,
12411246
start_column=startcol + cell.col + 1,
12421247
end_column=startcol + cell.mergeend + 1,
1243-
end_row=startrow + cell.mergeend + 1
1248+
end_row=startrow + cell.mergestart + 1
12441249
)
12451250

12461251
# When cells are merged only the top-left cell is preserved

pandas/io/tests/test_excel.py

-2
Original file line numberDiff line numberDiff line change
@@ -1116,8 +1116,6 @@ def test_to_excel_multiindex_cols(self):
11161116
fm = frame.columns.format(sparsify=False,
11171117
adjoin=False, names=False)
11181118
frame.columns = [ ".".join(map(str, q)) for q in zip(*fm) ]
1119-
# Above not working on 2.7 and 3.5, so fake the columns
1120-
frame.columns = df.columns.copy()
11211119
tm.assert_frame_equal(frame, df)
11221120

11231121
def test_to_excel_multiindex_dates(self):

0 commit comments

Comments
 (0)