@@ -681,13 +681,14 @@ class ExcelWriter(metaclass=abc.ABCMeta):
681
681
be parsed by ``fsspec``, e.g., starting "s3://", "gcs://".
682
682
683
683
.. versionadded:: 1.2.0
684
- if_sheet_exists : {'error', 'new', 'replace'}, default 'error'
684
+ : {'error', 'new', 'replace', 'write_to '}, default 'error'
685
685
How to behave when trying to write to a sheet that already
686
686
exists (append mode only).
687
687
688
688
* error: raise a ValueError.
689
689
* new: Create a new sheet, with a name determined by the engine.
690
690
* replace: Delete the contents of the sheet before writing to it.
691
+ * write_to: Write contents to the existing sheet.
691
692
692
693
.. versionadded:: 1.3.0
693
694
engine_kwargs : dict, optional
@@ -755,6 +756,27 @@ class ExcelWriter(metaclass=abc.ABCMeta):
755
756
>>> with ExcelWriter("path_to_file.xlsx", mode="a", engine="openpyxl") as writer:
756
757
... df.to_excel(writer, sheet_name="Sheet3")
757
758
759
+ Here, the `if_sheet_exists` parameter can be set to replace a sheet if it
760
+ already exists:
761
+
762
+ >>> with ExcelWriter(
763
+ ... "path_to_file.xlsx",
764
+ ... mode="a",
765
+ ... engine="openpyxl",
766
+ ... if_sheet_exists="replace"
767
+ ... ) as writer:
768
+ ... df.to_excel(writer, sheet_name="Sheet1")
769
+
770
+ You can specify arguments to the underlying engine. For example to not
771
+ calculate the result of a formula:
772
+
773
+ >>> df = pd.DataFrame(["=1+1"])
774
+ ... with ExcelWriter(
775
+ ... "path_to_file.xlsx",
776
+ ... engine_kwargs={"strings_to_formulas":False}
777
+ ... ) as writer:
778
+ ... df.to_excel(writer)
779
+
758
780
You can store Excel file in RAM:
759
781
760
782
>>> import io
@@ -940,10 +962,10 @@ def __init__(
940
962
941
963
self .mode = mode
942
964
943
- if if_sheet_exists not in [None , "error" , "new" , "replace" ]:
965
+ if if_sheet_exists not in [None , "error" , "new" , "replace" , "write_to" ]:
944
966
raise ValueError (
945
967
f"'{ if_sheet_exists } ' is not valid for if_sheet_exists. "
946
- "Valid options are 'error', 'new' and 'replace '."
968
+ "Valid options are 'error', 'new', 'replace' and 'write_to '."
947
969
)
948
970
if if_sheet_exists and "r+" not in mode :
949
971
raise ValueError ("if_sheet_exists is only valid in append mode (mode='a')" )
0 commit comments