Skip to content

Commit ab5af49

Browse files
committed
Added additional validation, whats new entry, documentation entry
1 parent bb18566 commit ab5af49

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

doc/source/io.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,6 +2777,7 @@ Added support for Openpyxl >= 2.2
27772777
``'xlsxwriter'`` will produce an Excel 2007-format workbook (xlsx). If
27782778
omitted, an Excel 2007-formatted workbook is produced.
27792779

2780+
27802781
.. _io.excel.writers:
27812782

27822783
Excel writer engines
@@ -2823,6 +2824,16 @@ argument to ``to_excel`` and to ``ExcelWriter``. The built-in engines are:
28232824
28242825
df.to_excel('path_to_file.xlsx', sheet_name='Sheet1')
28252826
2827+
Style and Formatting
2828+
''''''''''''''''''''
2829+
2830+
The look and feel of Excel worksheets created from pandas can be modified using the following parameters on the ``DataFrame``'s ``to_excel`` method.
2831+
2832+
- ``float_format`` : Format string for floating point numbers (default None)
2833+
- ``freeze_panes`` : A tuple of two integers representing the bottommost row and rightmost column to freeze. Each of these parameters is one-based, so (1, 1) will
2834+
freeze the first row and first column (default None)
2835+
2836+
28262837
.. _io.clipboard:
28272838

28282839
Clipboard

doc/source/whatsnew/v0.20.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ Other enhancements
146146
- ``Series/DataFrame.asfreq()`` have gained a ``fill_value`` parameter, to fill missing values (:issue:`3715`).
147147
- ``Series/DataFrame.resample.asfreq`` have gained a ``fill_value`` parameter, to fill missing values during resampling (:issue:`3715`).
148148
- ``pandas.tools.hashing`` has gained a ``hash_tuples`` routine, and ``hash_pandas_object`` has gained the ability to hash a ``MultiIndex`` (:issue:`15224`)
149+
- ``DataFrame.to_excel()`` has a new ``freeze_panes`` parameter to turn on Freeze Panes when exporting to Excel (:issue:`15160`)
149150

150151
.. _ISO 8601 duration: https://en.wikipedia.org/wiki/ISO_8601#Durations
151152

pandas/core/generic.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ def __setstate__(self, state):
10191019
# I/O Methods
10201020

10211021
_shared_docs['to_excel'] = """
1022-
Write %(klass)s to a excel sheet
1022+
Write %(klass)s to an excel sheet
10231023
%(versionadded_to_excel)s
10241024
Parameters
10251025
----------
@@ -1058,6 +1058,9 @@ def __setstate__(self, state):
10581058
inf_rep : string, default 'inf'
10591059
Representation for infinity (there is no native representation for
10601060
infinity in Excel)
1061+
freeze_panes : tuple of integer (length 2), default None
1062+
Specifies the bottommost row and rightmost column that
1063+
is to be frozen
10611064
10621065
Notes
10631066
-----

pandas/io/excel.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
811811
freeze_panes=None):
812812
# Write the frame cells using openpyxl.
813813
from openpyxl.cell import get_column_letter
814+
814815
sheet_name = self._get_sheet_name(sheet_name)
815816

816817
if sheet_name in self.sheets:
@@ -1327,7 +1328,11 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
13271328
wks.title = sheet_name
13281329
self.sheets[sheet_name] = wks
13291330

1330-
if freeze_panes is not None:
1331+
if (
1332+
freeze_panes is not None and
1333+
len(freeze_panes) == 2 and
1334+
all(isinstance(item, int) for item in freeze_panes)
1335+
):
13311336
wks.freeze_panes = wks.cell(row=freeze_panes[0] + 1,
13321337
column=freeze_panes[1] + 1)
13331338

@@ -1414,7 +1419,11 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
14141419
wks = self.book.add_sheet(sheet_name)
14151420
self.sheets[sheet_name] = wks
14161421

1417-
if freeze_panes is not None:
1422+
if (
1423+
freeze_panes is not None and
1424+
len(freeze_panes) == 2 and
1425+
all(isinstance(item, int) for item in freeze_panes)
1426+
):
14181427
wks.set_panes_frozen(True)
14191428
wks.set_horz_split_pos(freeze_panes[0])
14201429
wks.set_vert_split_pos(freeze_panes[1])
@@ -1545,7 +1554,11 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
15451554

15461555
style_dict = {}
15471556

1548-
if freeze_panes is not None:
1557+
if (
1558+
freeze_panes is not None and
1559+
len(freeze_panes) == 2 and
1560+
all(isinstance(item, int) for item in freeze_panes)
1561+
):
15491562
wks.freeze_panes(*(freeze_panes))
15501563

15511564
for cell in cells:

0 commit comments

Comments
 (0)