larray.ExcelReport

class larray.ExcelReport[source]

Automate the generation of multiple graphs in an Excel file.

The ExcelReport instance is initially populated with information (data, title, destination sheet, template, size) required to create the graphs. Once all information has been provided, the to_excel method is called to generate an Excel file with all graphs in one step.

Parameters
template_dirstr or Path, optional

Path to the directory containing the Excel template files (with a ‘.crtx’ extension). Defaults to None.

templatestr or Path, optional

Name of the template to be used as default template. The extension ‘.crtx’ will be added if not given. The full path to the template file must be given if no template directory has been set. Defaults to None.

graphs_per_row: int, optional

Default number of graphs per row. Defaults to 1.

Notes

The data associated with all graphical items is dumped in the same sheet named ‘__data__’.

Examples

>>> demo = load_example_data('demography_eurostat')
>>> report = ExcelReport(EXAMPLE_EXCEL_TEMPLATES_DIR)

Set a new destination sheet

>>> sheet_be = report.new_sheet('Belgium')

Add a new title item

>>> sheet_be.add_title('Population, births and deaths')

Add a new graph item (each new graph is placed right to previous one unless you use newline() or add_title())

>>> # using default 'width' and 'height' values
>>> sheet_be.add_graph(demo.population['Belgium'], 'Population', template='Line')
>>> # specifying the 'width' and 'height' values
>>> sheet_be.add_graph(demo.births['Belgium'], 'Births', template='Line', width=450, height=250)

Override the default ‘width’ and ‘height’ values for graphs

>>> sheet_be.set_item_default_size('graph', width=450, height=250)
>>> # add a new graph with the new default 'width' and 'height' values
>>> sheet_be.add_graph(demo.deaths['Belgium'], 'Deaths')

Set a default template for all next graphs

>>> # if a default template directory has been set, just pass the name
>>> sheet_be.template = 'Line'
>>> # otherwise, give the full path to the template file
>>> sheet_be.template = r'C:\other_template_dir\Line_Marker.crtx' 
>>> # add a new graph with the default template
>>> sheet_be.add_graph(demo.population['Belgium', 'Female'], 'Population - Female')
>>> sheet_be.add_graph(demo.population['Belgium', 'Male'], 'Population - Male')

Specify the number of graphs per row

>>> sheet_countries = report.new_sheet('All countries')
>>> sheet_countries.graphs_per_row = 2
>>> for combined_labels, subset in demo.population.items(('time', 'gender')):
...    title = ' - '.join([str(label) for label in combined_labels])
...    sheet_countries.add_graph(subset, title)

Force a new row of graphs

>>> sheet_countries.newline()

Add multiple graphs at once (add a new graph for each combination of gender and year)

>>> sheet_countries.add_graphs({'Population of {gender} by country in {year}': population},
...                            {'gender': population.gender, 'year': population.time},
...                            template='line', width=450, height=250, graphs_per_row=2)

Generate the report Excel file

>>> report.to_excel('Demography_Report.xlsx')
__init__()[source]

Methods

__init__()

new_sheet(sheet_name)

Add a new empty output sheet.

set_item_default_size(kind[, width, height])

Override the default 'width' and 'height' values for the given kind of item.

sheet_names()

Return the names of the output sheets.

to_excel(filepath[, data_sheet_name, overwrite])

Generate the report Excel file.

Attributes

graphs_per_row

Default number of graphs per row.

template

Set a default Excel template file.

template_dir

Set the path to the directory containing the Excel template files (with '.crtx' extension).