@@ -2313,149 +2313,6 @@ def to_excel(
23132313 if not isinstance (excel_writer , ExcelWriter ):
23142314 # we need to close the writer if we created it
23152315 excel_writer .close ()
2316- ) -> None :
2317- """
2318- Write {klass} to an Excel sheet.
2319-
2320- To write a single {klass} to an Excel .xlsx file it is only necessary to
2321- specify a target file name. To write to multiple sheets it is necessary to
2322- create an `ExcelWriter` object with a target file name, and specify a sheet
2323- in the file to write to.
2324-
2325- Multiple sheets may be written to by specifying unique `sheet_name`.
2326- With all data written to the file it is necessary to save the changes.
2327- Note that creating an `ExcelWriter` object with a file name that already
2328- exists will result in the contents of the existing file being erased.
2329-
2330- Parameters
2331- ----------
2332- excel_writer : path-like, file-like, or ExcelWriter object
2333- File path or existing ExcelWriter.
2334- sheet_name : str, default 'Sheet1'
2335- Name of sheet which will contain DataFrame.
2336- na_rep : str, default ''
2337- Missing data representation.
2338- float_format : str, optional
2339- Format string for floating point numbers. For example
2340- ``float_format="%.2f"`` will format 0.1234 to 0.12.
2341- columns : sequence or list of str, optional
2342- Columns to write.
2343- header : bool or list of str, default True
2344- Write out the column names. If a list of string is given it is
2345- assumed to be aliases for the column names.
2346- index : bool, default True
2347- Write row names (index).
2348- index_label : str or sequence, optional
2349- Column label for index column(s) if desired. If not specified, and
2350- `header` and `index` are True, then the index names are used. A
2351- sequence should be given if the DataFrame uses MultiIndex.
2352- startrow : int, default 0
2353- Upper left cell row to dump data frame.
2354- startcol : int, default 0
2355- Upper left cell column to dump data frame.
2356- engine : str, optional
2357- Write engine to use, 'openpyxl' or 'xlsxwriter'. You can also set this
2358- via the options ``io.excel.xlsx.writer`` or
2359- ``io.excel.xlsm.writer``.
2360-
2361- merge_cells : bool or 'columns', default False
2362- If True, write MultiIndex index and columns as merged cells.
2363- If 'columns', merge MultiIndex column cells only.
2364- {encoding_parameter}
2365- inf_rep : str, default 'inf'
2366- Representation for infinity (there is no native representation for
2367- infinity in Excel).
2368- {verbose_parameter}
2369- freeze_panes : tuple of int (length 2), optional
2370- Specifies the one-based bottommost row and rightmost column that
2371- is to be frozen.
2372- {storage_options}
2373-
2374- .. versionadded:: {storage_options_versionadded}
2375- {extra_parameters}
2376- See Also
2377- --------
2378- to_csv : Write DataFrame to a comma-separated values (csv) file.
2379- ExcelWriter : Class for writing DataFrame objects into excel sheets.
2380- read_excel : Read an Excel file into a pandas DataFrame.
2381- read_csv : Read a comma-separated values (csv) file into DataFrame.
2382- io.formats.style.Styler.to_excel : Add styles to Excel sheet.
2383-
2384- Notes
2385- -----
2386- For compatibility with :meth:`~DataFrame.to_csv`,
2387- to_excel serializes lists and dicts to strings before writing.
2388-
2389- Once a workbook has been saved it is not possible to write further
2390- data without rewriting the whole workbook.
2391-
2392- pandas will check the number of rows, columns,
2393- and cell character count does not exceed Excel's limitations.
2394- All other limitations must be checked by the user.
2395-
2396- Examples
2397- --------
2398-
2399- Create, write to and save a workbook:
2400-
2401- >>> df1 = pd.DataFrame(
2402- ... [["a", "b"], ["c", "d"]],
2403- ... index=["row 1", "row 2"],
2404- ... columns=["col 1", "col 2"],
2405- ... )
2406- >>> df1.to_excel("output.xlsx") # doctest: +SKIP
2407-
2408- To specify the sheet name:
2409-
2410- >>> df1.to_excel("output.xlsx", sheet_name="Sheet_name_1") # doctest: +SKIP
2411-
2412- If you wish to write to more than one sheet in the workbook, it is
2413- necessary to specify an ExcelWriter object:
2414-
2415- >>> df2 = df1.copy()
2416- >>> with pd.ExcelWriter("output.xlsx") as writer: # doctest: +SKIP
2417- ... df1.to_excel(writer, sheet_name="Sheet_name_1")
2418- ... df2.to_excel(writer, sheet_name="Sheet_name_2")
2419-
2420- ExcelWriter can also be used to append to an existing Excel file:
2421-
2422- >>> with pd.ExcelWriter("output.xlsx", mode="a") as writer: # doctest: +SKIP
2423- ... df1.to_excel(writer, sheet_name="Sheet_name_3")
2424-
2425- To set the library that is used to write the Excel file,
2426- you can pass the `engine` keyword (the default engine is
2427- automatically chosen depending on the file extension):
2428-
2429- >>> df1.to_excel("output1.xlsx", engine="xlsxwriter") # doctest: +SKIP
2430- """
2431- if engine_kwargs is None :
2432- engine_kwargs = {}
2433-
2434- df = self if isinstance (self , ABCDataFrame ) else self .to_frame ()
2435-
2436- from pandas .io .formats .excel import ExcelFormatter
2437-
2438- formatter = ExcelFormatter (
2439- df ,
2440- na_rep = na_rep ,
2441- cols = columns ,
2442- header = header ,
2443- float_format = float_format ,
2444- index = index ,
2445- index_label = index_label ,
2446- merge_cells = merge_cells ,
2447- inf_rep = inf_rep ,
2448- )
2449- formatter .write (
2450- excel_writer ,
2451- sheet_name = sheet_name ,
2452- startrow = startrow ,
2453- startcol = startcol ,
2454- freeze_panes = freeze_panes ,
2455- engine = engine ,
2456- storage_options = storage_options ,
2457- engine_kwargs = engine_kwargs ,
2458- )
24592316
24602317 @final
24612318 @doc (
0 commit comments