ExcelManager
- class exceltablekit.excel.manager.ExcelManager(filepath, create_sheet=False, sheetname=None)[source]
Manages an Excel
.xlsxfile: opens or creates it, resolves the target worksheet, owns theTableinstance, and persists changes to disk after every style operation.- Parameters:
- Raises:
ExcelSheetMissingError – If the named sheet does not exist and
create_sheetisFalse.
Table setup
- set_table(start_cell, end_cell)[source]
Define the rectangular table range.
- Parameters:
- Raises:
InvalidCellError – If either address is malformed.
InvalidColumnValueError – If a column letter exceeds
XFD.InvalidRowValueError – If a row number exceeds
1048576.
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:
TableNotDefinedError – If
set_table()has not been called.HeaderTableNotDefinedError – If
define_header()has not been called.
- 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:
TableNotDefinedError – If
set_table()has not been called.HeaderTableNotDefinedError – If
define_header()has not been called.HeaderTableFreezeError – If openpyxl raises an exception while setting the freeze pane.
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)) + 2across all cells in the table (header + body), then save.- Raises:
TableNotDefinedError – If
set_table()has not been called.TableColumnWidthError – If openpyxl raises an exception.