Skip to content

Commit c44581f

Browse files
newinhjreback
authored andcommitted
DOC: Expose ExcelWriter as part of the Generated API (#22359)
1 parent d39249a commit c44581f

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

doc/source/api.rst

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ Excel
6161
read_excel
6262
ExcelFile.parse
6363

64+
.. autosummary::
65+
:toctree: generated/
66+
:template: autosummary/class_without_autosummary.rst
67+
68+
ExcelWriter
69+
6470
JSON
6571
~~~~
6672

pandas/core/generic.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -1979,11 +1979,17 @@ def _repr_latex_(self):
19791979
If you wish to write to more than one sheet in the workbook, it is
19801980
necessary to specify an ExcelWriter object:
19811981
1982-
>>> writer = pd.ExcelWriter('output2.xlsx', engine='xlsxwriter')
1983-
>>> df1.to_excel(writer, sheet_name='Sheet1')
19841982
>>> df2 = df1.copy()
1985-
>>> df2.to_excel(writer, sheet_name='Sheet2')
1986-
>>> writer.save()
1983+
>>> with pd.ExcelWriter('output.xlsx') as writer:
1984+
... df1.to_excel(writer, sheet_name='Sheet_name_1')
1985+
... df2.to_excel(writer, sheet_name='Sheet_name_2')
1986+
1987+
To set the library that is used to write the Excel file,
1988+
you can pass the `engine` keyword (the default engine is
1989+
automatically chosen depending on the file extension):
1990+
1991+
>>> df1.to_excel('output1.xlsx', engine='xlsxwriter')
1992+
19871993
"""
19881994

19891995
def to_json(self, path_or_buf=None, orient=None, date_format=None,

pandas/io/excel.py

+35
Original file line numberDiff line numberDiff line change
@@ -824,8 +824,43 @@ class ExcelWriter(object):
824824
825825
Notes
826826
-----
827+
None of the methods and properties are considered public.
828+
827829
For compatibility with CSV writers, ExcelWriter serializes lists
828830
and dicts to strings before writing.
831+
832+
Examples
833+
--------
834+
Default usage:
835+
836+
>>> with ExcelWriter('path_to_file.xlsx') as writer:
837+
... df.to_excel(writer)
838+
839+
To write to separate sheets in a single file:
840+
841+
>>> with ExcelWriter('path_to_file.xlsx') as writer:
842+
... df1.to_excel(writer, sheet_name='Sheet1')
843+
... df2.to_excel(writer, sheet_name='Sheet2')
844+
845+
You can set the date format or datetime format:
846+
847+
>>> with ExcelWriter('path_to_file.xlsx',
848+
date_format='YYYY-MM-DD',
849+
datetime_format='YYYY-MM-DD HH:MM:SS') as writer:
850+
... df.to_excel(writer)
851+
852+
You can also append to an existing Excel file:
853+
854+
>>> with ExcelWriter('path_to_file.xlsx', mode='a') as writer:
855+
... df.to_excel(writer, sheet_name='Sheet3')
856+
857+
Attributes
858+
----------
859+
None
860+
861+
Methods
862+
-------
863+
None
829864
"""
830865
# Defining an ExcelWriter implementation (see abstract methods for more...)
831866

0 commit comments

Comments
 (0)