ExcelManager

class exceltablekit.excel.manager.ExcelManager(filepath, create_sheet=False, sheetname=None)[source]

Manages an Excel .xlsx file: opens or creates it, resolves the target worksheet, owns the Table instance, and persists changes to disk after every style operation.

Parameters:
  • filepath (str) – Path to the .xlsx file. Created on disk if it does not exist.

  • create_sheet (bool) – When True and sheetname is provided, a new worksheet is inserted into the workbook.

  • sheetname (str | None) – Name of the worksheet to use. None selects the active sheet.

Raises:

ExcelSheetMissingError – If the named sheet does not exist and create_sheet is False.

Table setup

set_table(start_cell, end_cell)[source]

Define the rectangular table range.

Parameters:
  • start_cell (str) – Top-left cell address (e.g. "A1").

  • end_cell (str) – Bottom-right cell address (e.g. "D10").

Raises:

Example:

excel = ExcelManager("output.xlsx")
excel.set_table("A1", "D10")
define_header(rows)[source]

Designate the first rows rows of the table as header rows.

Must be called after set_table().

Parameters:

rows (int) – Number of header rows.

Example:

excel.set_table("A1", "F20")
excel.define_header(rows=2)   # rows 1-2 are headers

Style application

apply_style_body(property, value)[source]

Apply an openpyxl style object to the property attribute of every body cell, then save the file.

Parameters:
  • property (str) – Cell attribute name (e.g. "fill", "font", "alignment", "border").

  • value – openpyxl style object to assign.

Raises:

TableNotDefinedError – If set_table() has not been called.

Example:

from openpyxl.styles import PatternFill

fill = PatternFill(fill_type="solid", fgColor="E8F4FD")
excel.apply_style_body("fill", fill)
apply_style_header(property, value)[source]

Apply an openpyxl style object to every header cell, then save.

Parameters:
  • property (str) – Cell attribute name.

  • value – openpyxl style object.

Raises:
apply_style_table(property, value)[source]

Apply a style to all cells (header + body), then save.

Parameters:
  • property (str) – Cell attribute name.

  • value – openpyxl style object.

Raises:

TableNotDefinedError – If set_table() has not been called.

Example:

from openpyxl.styles import Alignment

alignment = Alignment(horizontal="center", vertical="center")
excel.apply_style_table("alignment", alignment)

Utilities

freeze_header()[source]

Freeze the worksheet pane immediately below the last header row.

The freeze cell is {first_column}{first_row + total_header_rows}.

Raises:

Example:

excel.set_table("A1", "D20")
excel.define_header(rows=1)
excel.freeze_header()
# Freeze pane → "A2"
auto_fit_columns()[source]

Set each column’s width to max(len(cell value)) + 2 across all cells in the table (header + body), then save.

Raises: