Skip to content

Commit 07a5735

Browse files
jmcnamarajreback
authored andcommitted
BUG: Fix for extraneous default cell format in xlsxwriter files.
Fix for issue in the xlsxwriter engine where is adds a default 'General' format to cells if no other format is applied. This isn't a bug, per se, but it prevents other row or column formatting. Closes #9167
1 parent 389b022 commit 07a5735

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

doc/source/whatsnew/v0.16.0.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ Bug Fixes
144144

145145
- DataFrame now properly supports simultaneous ``copy`` and ``dtype`` arguments in constructor (:issue:`9099`)
146146
- Bug in read_csv when using skiprows on a file with CR line endings with the c engine. (:issue:`9079`)
147-
- isnull now detects NaT in PeriodIndex (:issue:`9129`)
147+
- isnull now detects ``NaT`` in PeriodIndex (:issue:`9129`)
148148
- Bug in groupby ``.nth()`` with a multiple column groupby (:issue:`8979`)
149+
149150
- Fixed division by zero error for ``Series.kurt()`` when all values are equal (:issue:`9197`)
151+
152+
153+
- 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`)

pandas/io/excel.py

+4
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,10 @@ def _convert_to_style(self, style_dict, num_format_str=None):
12741274
num_format_str: optional number format string
12751275
"""
12761276

1277+
# If there is no formatting we don't create a format object.
1278+
if num_format_str is None and style_dict is None:
1279+
return None
1280+
12771281
# Create a XlsxWriter format object.
12781282
xl_format = self.book.add_format()
12791283

pandas/io/tests/test_excel.py

+41
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,47 @@ class XlsxWriterTests(ExcelWriterBase, tm.TestCase):
13531353
engine_name = 'xlsxwriter'
13541354
check_skip = staticmethod(_skip_if_no_xlsxwriter)
13551355

1356+
def test_column_format(self):
1357+
# Test that column formats are applied to cells. Test for issue #9167.
1358+
# Applicable to xlsxwriter only.
1359+
_skip_if_no_xlsxwriter()
1360+
1361+
import warnings
1362+
with warnings.catch_warnings():
1363+
# Ignore the openpyxl lxml warning.
1364+
warnings.simplefilter("ignore")
1365+
_skip_if_no_openpyxl()
1366+
import openpyxl
1367+
1368+
with ensure_clean(self.ext) as path:
1369+
frame = DataFrame({'A': [123456, 123456],
1370+
'B': [123456, 123456]})
1371+
1372+
writer = ExcelWriter(path)
1373+
frame.to_excel(writer)
1374+
1375+
# Add a number format to col B and ensure it is applied to cells.
1376+
num_format = '#,##0'
1377+
write_workbook = writer.book
1378+
write_worksheet = write_workbook.worksheets()[0]
1379+
col_format = write_workbook.add_format({'num_format': num_format})
1380+
write_worksheet.set_column('B:B', None, col_format)
1381+
writer.save()
1382+
1383+
read_workbook = openpyxl.load_workbook(path)
1384+
read_worksheet = read_workbook.get_sheet_by_name(name='Sheet1')
1385+
1386+
# Get the number format from the cell. This method is backward
1387+
# compatible with older versions of openpyxl.
1388+
cell = read_worksheet.cell('B2')
1389+
1390+
try:
1391+
read_num_format = cell.style.number_format._format_code
1392+
except:
1393+
read_num_format = cell.style.number_format
1394+
1395+
self.assertEqual(read_num_format, num_format)
1396+
13561397

13571398
class OpenpyxlTests_NoMerge(ExcelWriterBase, tm.TestCase):
13581399
ext = '.xlsx'

0 commit comments

Comments
 (0)