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