mpl_bsic.df_to_excel#
- mpl_bsic.df_to_excel(df, path_to_excel, title=None, offset=(1, 1))[source]#
Export a Pandas DataFrame as a formatted Excel table.
Saves the content of the DataFrame to Excel and formats the table according to the BSIC Styling Standards.
The function will also add a title (optional) at the very top of the table and a row containing “Source: BSIC” at the very bottom, leaving a blank row (with reduced height) in between for better aesthetics.
- Parameters:
- df
Union
[pandas.DataFrame
,list
[pandas.DataFrame
]] Either a single DataFrame or a list of dataframes to be included in the final file.
- path_to_excel
str
The path for the final excel file, e.g.
output/fmt_data.xlsx
.- title
Optional
[Union
[str
,list
[str
]]], optional Titles to be included above the formatted table, by default
None
.- offset
tuple
[int
,int
], optional The offset to use in the formatted output, by default
(1, 1)
. For example, if offset is set to(1,2)
, the formatter will leave 2 empty rows and 1 empty column for better visualization.
- df
- Raises:
Exception
If you provide a list of DataFrames and you want to set titles, you must provide a title for each DataFrame.
Exception
The length of the DataFrame list/array must match the length of the titles list/array.
Exception
If you only provide a DataFrame (not a list), the title must also be a str, not a list.
Warning
The source Excel file must only contain the headings, the index and the data, with no blank columns or rows in between them. The table has to start at row 0, col 0.
See also
mpl_bsic.style_excel_file
Styles an Excel file. Use if you want to format data from a spreadsheet rather than a pandas DataFrame.
Examples
import pandas as pd from mpl_bsic import df_to_excel df = pd.read_excel("your_excel_file.xlsx") df_to_excel( df, "output_filename.xlsx", "Title of the Table", (2,2) # if you want the table to have an offset )