@@ -707,39 +707,55 @@ class ExcelWriter(metaclass=abc.ABCMeta):
707
707
--------
708
708
Default usage:
709
709
710
- >>> with ExcelWriter('path_to_file.xlsx') as writer:
710
+ >>> df = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"])
711
+ >>> with ExcelWriter("path_to_file.xlsx") as writer:
711
712
... df.to_excel(writer)
712
713
713
714
To write to separate sheets in a single file:
714
715
715
- >>> with ExcelWriter('path_to_file.xlsx') as writer:
716
- ... df1.to_excel(writer, sheet_name='Sheet1')
717
- ... df2.to_excel(writer, sheet_name='Sheet2')
716
+ >>> df1 = pd.DataFrame([["AAA", "BBB"]], columns=["Spam", "Egg"])
717
+ >>> df2 = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"])
718
+ >>> with ExcelWriter("path_to_file.xlsx") as writer:
719
+ ... df1.to_excel(writer, sheet_name="Sheet1")
720
+ ... df2.to_excel(writer, sheet_name="Sheet2")
718
721
719
722
You can set the date format or datetime format:
720
723
721
- >>> with ExcelWriter('path_to_file.xlsx',
722
- ... date_format='YYYY-MM-DD',
723
- ... datetime_format='YYYY-MM-DD HH:MM:SS') as writer:
724
+ >>> from datetime import date, datetime
725
+ >>> df = pd.DataFrame(
726
+ ... [
727
+ ... [date(2014, 1, 31), date(1999, 9, 24)],
728
+ ... [datetime(1998, 5, 26, 23, 33, 4), datetime(2014, 2, 28, 13, 5, 13)],
729
+ ... ],
730
+ ... index=["Date", "Datetime"],
731
+ ... columns=["X", "Y"],
732
+ ... )
733
+ >>> with ExcelWriter(
734
+ ... "path_to_file.xlsx",
735
+ ... date_format="YYYY-MM-DD",
736
+ ... datetime_format="YYYY-MM-DD HH:MM:SS"
737
+ ... ) as writer:
724
738
... df.to_excel(writer)
725
739
726
740
You can also append to an existing Excel file:
727
741
728
- >>> with ExcelWriter(' path_to_file.xlsx' , mode='a' ) as writer:
729
- ... df.to_excel(writer, sheet_name=' Sheet3' )
742
+ >>> with ExcelWriter(" path_to_file.xlsx" , mode="a", engine="openpyxl" ) as writer:
743
+ ... df.to_excel(writer, sheet_name=" Sheet3" )
730
744
731
745
You can store Excel file in RAM:
732
746
733
747
>>> import io
748
+ >>> df = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"])
734
749
>>> buffer = io.BytesIO()
735
750
>>> with pd.ExcelWriter(buffer) as writer:
736
751
... df.to_excel(writer)
737
752
738
753
You can pack Excel file into zip archive:
739
754
740
755
>>> import zipfile
741
- >>> with zipfile.ZipFile('path_to_file.zip', 'w') as zf:
742
- ... with zf.open('filename.xlsx', 'w') as buffer:
756
+ >>> df = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"])
757
+ >>> with zipfile.ZipFile("path_to_file.zip", "w") as zf:
758
+ ... with zf.open("filename.xlsx", "w") as buffer:
743
759
... with pd.ExcelWriter(buffer) as writer:
744
760
... df.to_excel(writer)
745
761
"""
0 commit comments