piper.xl.WorkBook.get_default_format¶
-
static
WorkBook.get_default_format(df)[source]¶ For given dataframe, calculate default format.
Returns dictionary list of columns with calculated width info. It provides an iteratable to apply column formatting attributes across an excel sheet for given dataframe (such as centreing columns).
- Parameters
df – a pandas dataframe
- Returns
A dictionary (list) of each field, excel column reference and currently calculated column width.
- Return type
column format dictionary
Examples
df = get_sample_data() formats = get_default_format(df) >> [{'column_name': 'dates', 'column': 'A', 'width': 11}, >> {'column_name': 'order_dates', 'column': 'B', 'width': 12}, >> {'column_name': 'countries', 'column': 'C', 'width': 12}, >> {'column_name': 'regions', 'column': 'D', 'width': 8}, >> {'column_name': 'ids', 'column': 'E', 'width': 4}, >> {'column_name': 'values_1', 'column': 'F', 'width': 9}, >> {'column_name': 'values_2', 'column': 'G', 'width': 9}]
wb = WorkBook('outputs/testing output format') ws = wb.add_sheet(df, sheet_name='testing') [x.update({'format': 'center_wrap'}) for x in formats] wb.add_format(ws, column_attr=formats) wb.close()