@@ -804,6 +804,10 @@ class ExcelWriter(object):
804
804
datetime_format : string, default None
805
805
Format string for datetime objects written into Excel files
806
806
(e.g. 'YYYY-MM-DD HH:MM:SS')
807
+ mode : {'w' or 'a'}, default 'w'
808
+ File mode to use (write or append).
809
+
810
+ .. versionadded:: 0.24.0
807
811
808
812
Notes
809
813
-----
@@ -897,7 +901,8 @@ def save(self):
897
901
pass
898
902
899
903
def __init__ (self , path , engine = None ,
900
- date_format = None , datetime_format = None , ** engine_kwargs ):
904
+ date_format = None , datetime_format = None , mode = 'w' ,
905
+ ** engine_kwargs ):
901
906
# validate that this engine can handle the extension
902
907
if isinstance (path , string_types ):
903
908
ext = os .path .splitext (path )[- 1 ]
@@ -919,6 +924,8 @@ def __init__(self, path, engine=None,
919
924
else :
920
925
self .datetime_format = datetime_format
921
926
927
+ self .mode = mode
928
+
922
929
def __fspath__ (self ):
923
930
return _stringify_path (self .path )
924
931
@@ -993,23 +1000,27 @@ class _OpenpyxlWriter(ExcelWriter):
993
1000
engine = 'openpyxl'
994
1001
supported_extensions = ('.xlsx' , '.xlsm' )
995
1002
996
- def __init__ (self , path , engine = None , ** engine_kwargs ):
1003
+ def __init__ (self , path , engine = None , mode = 'w' , ** engine_kwargs ):
997
1004
# Use the openpyxl module as the Excel writer.
998
1005
from openpyxl .workbook import Workbook
999
1006
1000
- super (_OpenpyxlWriter , self ).__init__ (path , ** engine_kwargs )
1007
+ super (_OpenpyxlWriter , self ).__init__ (path , mode = mode , ** engine_kwargs )
1001
1008
1002
- # Create workbook object with default optimized_write=True.
1003
- self .book = Workbook ()
1009
+ if self .mode == 'a' : # Load from existing workbook
1010
+ from openpyxl import load_workbook
1011
+ book = load_workbook (self .path )
1012
+ self .book = book
1013
+ else :
1014
+ # Create workbook object with default optimized_write=True.
1015
+ self .book = Workbook ()
1004
1016
1005
- # Openpyxl 1.6.1 adds a dummy sheet. We remove it.
1006
- if self .book .worksheets :
1007
- try :
1008
- self .book .remove (self .book .worksheets [0 ])
1009
- except AttributeError :
1017
+ if self .book .worksheets :
1018
+ try :
1019
+ self .book .remove (self .book .worksheets [0 ])
1020
+ except AttributeError :
1010
1021
1011
- # compat
1012
- self .book .remove_sheet (self .book .worksheets [0 ])
1022
+ # compat - for openpyxl <= 2.4
1023
+ self .book .remove_sheet (self .book .worksheets [0 ])
1013
1024
1014
1025
def save (self ):
1015
1026
"""
@@ -1443,11 +1454,16 @@ class _XlwtWriter(ExcelWriter):
1443
1454
engine = 'xlwt'
1444
1455
supported_extensions = ('.xls' ,)
1445
1456
1446
- def __init__ (self , path , engine = None , encoding = None , ** engine_kwargs ):
1457
+ def __init__ (self , path , engine = None , encoding = None , mode = 'w' ,
1458
+ ** engine_kwargs ):
1447
1459
# Use the xlwt module as the Excel writer.
1448
1460
import xlwt
1449
1461
engine_kwargs ['engine' ] = engine
1450
- super (_XlwtWriter , self ).__init__ (path , ** engine_kwargs )
1462
+
1463
+ if mode == 'a' :
1464
+ raise ValueError ('Append mode is not supported with xlwt!' )
1465
+
1466
+ super (_XlwtWriter , self ).__init__ (path , mode = mode , ** engine_kwargs )
1451
1467
1452
1468
if encoding is None :
1453
1469
encoding = 'ascii'
@@ -1713,13 +1729,18 @@ class _XlsxWriter(ExcelWriter):
1713
1729
supported_extensions = ('.xlsx' ,)
1714
1730
1715
1731
def __init__ (self , path , engine = None ,
1716
- date_format = None , datetime_format = None , ** engine_kwargs ):
1732
+ date_format = None , datetime_format = None , mode = 'w' ,
1733
+ ** engine_kwargs ):
1717
1734
# Use the xlsxwriter module as the Excel writer.
1718
1735
import xlsxwriter
1719
1736
1737
+ if mode == 'a' :
1738
+ raise ValueError ('Append mode is not supported with xlsxwriter!' )
1739
+
1720
1740
super (_XlsxWriter , self ).__init__ (path , engine = engine ,
1721
1741
date_format = date_format ,
1722
1742
datetime_format = datetime_format ,
1743
+ mode = mode ,
1723
1744
** engine_kwargs )
1724
1745
1725
1746
self .book = xlsxwriter .Workbook (path , ** engine_kwargs )
0 commit comments