-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Append Mode for ExcelWriter with openpyxl #21251
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
Changes from 4 commits
a22c9fe
bfe9155
b516f89
09e5b45
8acadb3
64b0a63
e6242e6
e0c5b69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -804,6 +804,8 @@ class ExcelWriter(object): | |
datetime_format : string, default None | ||
Format string for datetime objects written into Excel files | ||
(e.g. 'YYYY-MM-DD HH:MM:SS') | ||
mode : {'w' or 'a'}, default 'w' | ||
File mode to use (write or append). | ||
|
||
Notes | ||
----- | ||
|
@@ -897,7 +899,8 @@ def save(self): | |
pass | ||
|
||
def __init__(self, path, engine=None, | ||
date_format=None, datetime_format=None, **engine_kwargs): | ||
date_format=None, datetime_format=None, mode='w', | ||
**engine_kwargs): | ||
# validate that this engine can handle the extension | ||
if isinstance(path, string_types): | ||
ext = os.path.splitext(path)[-1] | ||
|
@@ -919,6 +922,8 @@ def __init__(self, path, engine=None, | |
else: | ||
self.datetime_format = datetime_format | ||
|
||
self.mode = mode | ||
|
||
def __fspath__(self): | ||
return _stringify_path(self.path) | ||
|
||
|
@@ -993,23 +998,28 @@ class _OpenpyxlWriter(ExcelWriter): | |
engine = 'openpyxl' | ||
supported_extensions = ('.xlsx', '.xlsm') | ||
|
||
def __init__(self, path, engine=None, **engine_kwargs): | ||
def __init__(self, path, engine=None, mode='w', **engine_kwargs): | ||
# Use the openpyxl module as the Excel writer. | ||
from openpyxl.workbook import Workbook | ||
|
||
super(_OpenpyxlWriter, self).__init__(path, **engine_kwargs) | ||
super(_OpenpyxlWriter, self).__init__(path, mode=mode, **engine_kwargs) | ||
|
||
# Create workbook object with default optimized_write=True. | ||
self.book = Workbook() | ||
if self.mode == 'a': # Load from existing workbook | ||
from openpyxl import load_workbook | ||
book = load_workbook(self.path) | ||
self.book = book | ||
else: | ||
# Create workbook object with default optimized_write=True. | ||
self.book = Workbook() | ||
|
||
# Openpyxl 1.6.1 adds a dummy sheet. We remove it. | ||
if self.book.worksheets: | ||
try: | ||
self.book.remove(self.book.worksheets[0]) | ||
except AttributeError: | ||
# Openpyxl 1.6.1 adds a dummy sheet. We remove it. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can remove this comment (and maybe don't need this anymore), 1.6.1 is lower than supported version There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless I'm overlooking it I don't see an explicit min version. Happy to add one - what would that be? |
||
if self.book.worksheets: | ||
try: | ||
self.book.remove(self.book.worksheets[0]) | ||
except AttributeError: | ||
|
||
# compat | ||
self.book.remove_sheet(self.book.worksheets[0]) | ||
# compat | ||
self.book.remove_sheet(self.book.worksheets[0]) | ||
|
||
def save(self): | ||
""" | ||
|
@@ -1443,11 +1453,16 @@ class _XlwtWriter(ExcelWriter): | |
engine = 'xlwt' | ||
supported_extensions = ('.xls',) | ||
|
||
def __init__(self, path, engine=None, encoding=None, **engine_kwargs): | ||
def __init__(self, path, engine=None, encoding=None, mode='w', | ||
**engine_kwargs): | ||
# Use the xlwt module as the Excel writer. | ||
import xlwt | ||
engine_kwargs['engine'] = engine | ||
super(_XlwtWriter, self).__init__(path, **engine_kwargs) | ||
|
||
if mode == 'a': | ||
raise ValueError('Append mode is not supported with xlwt!') | ||
|
||
super(_XlwtWriter, self).__init__(path, mode=mode, **engine_kwargs) | ||
|
||
if encoding is None: | ||
encoding = 'ascii' | ||
|
@@ -1713,13 +1728,18 @@ class _XlsxWriter(ExcelWriter): | |
supported_extensions = ('.xlsx',) | ||
|
||
def __init__(self, path, engine=None, | ||
date_format=None, datetime_format=None, **engine_kwargs): | ||
date_format=None, datetime_format=None, mode='w', | ||
**engine_kwargs): | ||
# Use the xlsxwriter module as the Excel writer. | ||
import xlsxwriter | ||
|
||
if mode == 'a': | ||
raise ValueError('Append mode is not supported with xlsxwriter!') | ||
|
||
super(_XlsxWriter, self).__init__(path, engine=engine, | ||
date_format=date_format, | ||
datetime_format=datetime_format, | ||
mode=mode, | ||
**engine_kwargs) | ||
|
||
self.book = xlsxwriter.Workbook(path, **engine_kwargs) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a versionadded
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it's worth adding this class to the API? Would be a little strange as its an ABCMeta but on the flip side may still be beneficial for those looking further at it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't you just do
from pandas.io.excel import ExcelWriter
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not the import mechanism I was referring to as much as the documentation - this class is not part of the doc build
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@WillAyd : Ah, thanks for clarifying. I don't see why not then, but let's see what @jreback has to say about that.