Skip to content

BUG: Fix for extraneous default cell format in xlsxwriter files. #9171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v0.16.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,7 @@ Bug Fixes
- Bug in read_csv when using skiprows on a file with CR line endings with the c engine. (:issue:`9079`)
- isnull now detects NaT in PeriodIndex (:issue:`9129`)
- Bug in groupby ``.nth()`` with a multiple column groupby (:issue:`8979`)

- Fixed issue in the ``xlsxwriter`` engine where it added a default 'General'
format to cells if no other format wass applied. This prevented other row or
column formatting being applied. (:issue:`9167`)
4 changes: 4 additions & 0 deletions pandas/io/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,10 @@ def _convert_to_style(self, style_dict, num_format_str=None):
num_format_str: optional number format string
"""

# If there is no formatting we don't create a format object.
if num_format_str is None and style_dict is None:
return None

# Create a XlsxWriter format object.
xl_format = self.book.add_format()

Expand Down
41 changes: 41 additions & 0 deletions pandas/io/tests/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,47 @@ class XlsxWriterTests(ExcelWriterBase, tm.TestCase):
engine_name = 'xlsxwriter'
check_skip = staticmethod(_skip_if_no_xlsxwriter)

def test_column_format(self):
# Test that column formats are applied to cells. Test for issue #9167.
# Applicable to xlsxwriter only.
_skip_if_no_xlsxwriter()

import warnings
with warnings.catch_warnings():
Copy link
Contributor

Choose a reason for hiding this comment

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

_skip_if_no_openpyxl() doesn't suffice here?

# Ignore the openpyxl lxml warning.
warnings.simplefilter("ignore")
_skip_if_no_openpyxl()
import openpyxl

with ensure_clean(self.ext) as path:
frame = DataFrame({'A': [123456, 123456],
'B': [123456, 123456]})

writer = ExcelWriter(path)
frame.to_excel(writer)

# Add a number format to col B and ensure it is applied to cells.
num_format = '#,##0'
write_workbook = writer.book
write_worksheet = write_workbook.worksheets()[0]
col_format = write_workbook.add_format({'num_format': num_format})
write_worksheet.set_column('B:B', None, col_format)
writer.save()

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')

try:
read_num_format = cell.style.number_format._format_code
except:
read_num_format = cell.style.number_format

self.assertEqual(read_num_format, num_format)


class OpenpyxlTests_NoMerge(ExcelWriterBase, tm.TestCase):
ext = '.xlsx'
Expand Down