Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions pandas/io/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0):

for cell in cells:
colletter = get_column_letter(startcol + cell.col + 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of these changes need to be back-compat. so need to switch code based on the version.

xcell = wks.cell("%s%s" % (colletter, startrow + cell.row + 1))
xcell = wks.cell(row=startrow + cell.row + 1, column=colletter)
xcell.value = _conv_value(cell.val)
style_kwargs = {}

Expand Down Expand Up @@ -943,7 +943,7 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0):
# Ignore first cell. It is already handled.
continue
colletter = get_column_letter(col)
xcell = wks.cell("%s%s" % (colletter, row))
xcell = wks.cell(row=row, column=colletter)
xcell.style = xcell.style.copy(**style_kwargs)

@classmethod
Expand Down Expand Up @@ -1308,10 +1308,7 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0):
self.sheets[sheet_name] = wks

for cell in cells:
xcell = wks.cell(
row=startrow + cell.row + 1,
column=startcol + cell.col + 1
)
xcell = wks.cell(row=startrow + cell.row + 1, column=startcol + cell.col + 1)
xcell.value = _conv_value(cell.val)

style_kwargs = {}
Expand Down Expand Up @@ -1349,7 +1346,7 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0):
if row == first_row and col == first_col:
# Ignore first cell. It is already handled.
continue
xcell = wks.cell(column=col, row=row)
xcell = wks.cell(row=row, column=col)
for k, v in style_kwargs.items():
setattr(xcell, k, v)

Expand Down
20 changes: 12 additions & 8 deletions pandas/io/tests/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1940,6 +1940,7 @@ def test_to_excel_styleconverter(self):
self.assertEqual(kw['protection'], protection)

def test_write_cells_merge_styled(self):
import warnings
from pandas.formats.format import ExcelCell
from openpyxl import styles

Expand All @@ -1962,13 +1963,14 @@ def test_write_cells_merge_styled(self):
]

with ensure_clean('.xlsx') as path:
warnings.simplefilter('error', UserWarning)
writer = _Openpyxl20Writer(path)
writer.write_cells(initial_cells, sheet_name=sheet_name)
writer.write_cells(merge_cells, sheet_name=sheet_name)

wks = writer.sheets[sheet_name]
xcell_b1 = wks.cell('B1')
xcell_a2 = wks.cell('A2')
xcell_b1 = wks['B1']
xcell_a2 = wks['A2']
self.assertEqual(xcell_b1.style, openpyxl_sty_merged)
self.assertEqual(xcell_a2.style, openpyxl_sty_merged)

Expand Down Expand Up @@ -2052,7 +2054,8 @@ def test_to_excel_styleconverter(self):
def test_write_cells_merge_styled(self):
if not openpyxl_compat.is_compat(major_ver=2):
raise nose.SkipTest('incompatiable openpyxl version')


import warnings
from pandas.formats.format import ExcelCell

sheet_name = 'merge_styled'
Expand All @@ -2074,13 +2077,14 @@ def test_write_cells_merge_styled(self):
]

with ensure_clean('.xlsx') as path:
warnings.simplefilter('error', UserWarning)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use assert_produces_warning

writer = _Openpyxl22Writer(path)
writer.write_cells(initial_cells, sheet_name=sheet_name)
writer.write_cells(merge_cells, sheet_name=sheet_name)

wks = writer.sheets[sheet_name]
xcell_b1 = wks.cell('B1')
xcell_a2 = wks.cell('A2')
xcell_b1 = wks['B1']
xcell_a2 = wks['A2']
self.assertEqual(xcell_b1.font, openpyxl_sty_merged)
self.assertEqual(xcell_a2.font, openpyxl_sty_merged)

Expand Down Expand Up @@ -2176,9 +2180,9 @@ def test_column_format(self):
read_workbook = openpyxl.load_workbook(path)
read_worksheet = read_workbook.get_sheet_by_name(name='Sheet1')

# Get the number format from the cell. This method is backward
# compatible with older versions of openpyxl.
cell = read_worksheet.cell('B2')
# Get the number format from the cell. This method is current
# with openpyxl latest release.
cell = read_worksheet['B2']

try:
read_num_format = cell.number_format
Expand Down