API Reference
Complete documentation for xltemplate classes and methods.
Workbook
xltemplate.Workbook
A wrapper around an openpyxl Workbook for template population.
Provides a clean, stateful interface for loading Excel templates, writing DataFrames to sheets, and saving the result.
Example
wb = Workbook("template.xlsx") wb.sheet("Data").write_df(df, row=5, col=2) wb.save("output.xlsx")
Source code in xltemplate/workbook.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
sheet_names
property
List of all sheet names in the workbook.
__init__(path)
Load an existing Excel workbook from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Path to the .xlsx file to load |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist |
InvalidFileException
|
If not a valid xlsx |
Source code in xltemplate/workbook.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
sheet(name)
Get a Sheet object for the named worksheet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the worksheet |
required |
Returns:
| Type | Description |
|---|---|
Sheet
|
A Sheet object for the worksheet |
Raises:
| Type | Description |
|---|---|
KeyError
|
If no sheet with that name exists |
Source code in xltemplate/workbook.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
save(path)
Save the workbook to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Destination path for the saved workbook |
required |
Source code in xltemplate/workbook.py
75 76 77 78 79 80 81 82 | |
close()
Close the workbook and release resources.
Source code in xltemplate/workbook.py
84 85 86 | |
__enter__()
Context manager entry.
Source code in xltemplate/workbook.py
88 89 90 | |
__exit__(exc_type, exc_val, exc_tb)
Context manager exit - closes the workbook.
Source code in xltemplate/workbook.py
92 93 94 | |
Sheet
xltemplate.Sheet
Represents a worksheet within a Workbook.
Provides methods for writing DataFrames and values while preserving existing formatting and formulas.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The name of the worksheet |
Source code in xltemplate/sheet.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
name
property
The name of this worksheet.
write_df(df, row, col, *, headers=True, preserve_format=True, preserve_formulas=True)
Write a DataFrame to the worksheet starting at the specified position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
Any
|
A Pandas or Polars DataFrame |
required |
row
|
int
|
Starting row (1-indexed) |
required |
col
|
int
|
Starting column (1-indexed) |
required |
headers
|
bool
|
Include column headers as the first row (default: True) |
True
|
preserve_format
|
bool
|
Keep existing cell formatting (default: True) |
True
|
preserve_formulas
|
bool
|
Skip cells containing formulas (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
Sheet
|
Self for method chaining |
Raises:
| Type | Description |
|---|---|
TypeError
|
If df is not a supported DataFrame type |
ValueError
|
If row or col is less than 1 |
Source code in xltemplate/sheet.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
write_value(value, row, col, *, preserve_format=True)
Write a single value to a cell.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to write |
required |
row
|
int
|
Row number (1-indexed) |
required |
col
|
int
|
Column number (1-indexed) |
required |
preserve_format
|
bool
|
Keep existing cell formatting (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
Sheet
|
Self for method chaining |
Raises:
| Type | Description |
|---|---|
ValueError
|
If row or col is less than 1 |
Source code in xltemplate/sheet.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
TableSchema
xltemplate.TableSchema
dataclass
Represents the column structure extracted from a template header.
Use this to create DataFrames that match a template's expected structure, or to validate that existing DataFrames conform to the template.
The schema captures hierarchical column headers (e.g., grouped columns) and creates DataFrames with pandas MultiIndex columns so that each cell can be addressed by all levels of the hierarchy.
Attributes:
| Name | Type | Description |
|---|---|---|
column_names |
list[str]
|
List of column names in order (leaf-level for multi-row headers) |
header_rows |
list[list[tuple[str, int]]]
|
List of header rows above the leaf row, from top to bottom. Each row is a list of (label, span) tuples. |
Example
schema = sheet.extract_header_schema(row=6, col=2, n_cols=16, n_header_rows=3) df = schema.empty_df() df[("Prevalence by Domain", "Domain: XXX", "N")] # Access by hierarchy
Source code in xltemplate/schema.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
column_names
instance-attribute
header_rows = field(default_factory=list)
class-attribute
instance-attribute
groups
property
Backward-compatible alias for the first header row.
n_levels
property
Number of header levels (header_rows + leaf column_names).
to_multiindex()
Build a pandas MultiIndex from the header structure.
Each level of the MultiIndex corresponds to a header row, with the leaf column_names as the final level.
Returns:
| Type | Description |
|---|---|
Any
|
pandas.MultiIndex with one level per header row + leaf columns |
Raises:
| Type | Description |
|---|---|
ImportError
|
If pandas is not installed |
Source code in xltemplate/schema.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
empty_df(n_rows=0)
Create an empty DataFrame with MultiIndex columns matching this schema.
For multi-row headers, the DataFrame will have hierarchical columns that can be accessed by the full path, e.g.: df[("Prevalence by Domain", "Domain: XXX", "N")]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_rows
|
int
|
Number of rows to pre-allocate (default: 0) |
0
|
Returns:
| Type | Description |
|---|---|
Any
|
A pandas DataFrame with MultiIndex columns matching the schema. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If pandas is not installed |
Source code in xltemplate/schema.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
validate_df(df)
Check if a DataFrame's columns match this schema's MultiIndex structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
Any
|
A pandas DataFrame to validate |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if columns match exactly (all levels, names, and order) |
Source code in xltemplate/schema.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
Preserving Formulas
By default, write_df() will skip cells containing formulas. This prevents accidental overwriting of calculated fields.
# Formula at C5 will NOT be overwritten
wb.sheet("Data").write_df(df, row=1, col=1, preserve_formulas=True)
# Force overwrite formulas
wb.sheet("Data").write_df(df, row=1, col=1, preserve_formulas=False)
Preserving Formatting
Cell formatting (fonts, colors, borders, fills) is preserved by default when writing values:
# Formatting preserved (default)
wb.sheet("Data").write_value("New Text", row=1, col=1, preserve_format=True)
# Clear formatting
wb.sheet("Data").write_value("New Text", row=1, col=1, preserve_format=False)