excel
pinky_snowpark.excel
openpyxl helpers for building Excel workbooks from Snowpark DataFrames.
Requires the excel optional dependency::
pip install pinky-snowpark[excel]
All openpyxl imports are lazy — the module is importable without openpyxl installed, and errors surface only when a function is actually called.
CellFormat
dataclass
Formatting descriptor for an Excel column (openpyxl).
All fields are optional — only non-None / non-False attributes are applied, leaving the openpyxl default intact for everything else.
Attributes:
| Name | Type | Description |
|---|---|---|
number_format |
str | None
|
openpyxl number format string (e.g. |
bold |
bool
|
Bold font. |
italic |
bool
|
Italic font. |
font_color |
str | None
|
Font colour as RRGGBB hex without |
font_size |
int | None
|
Font size in points. |
bg_color |
str | None
|
Background fill colour as RRGGBB hex (e.g. |
h_align |
str | None
|
Horizontal alignment — |
v_align |
str | None
|
Vertical alignment — |
wrap |
bool
|
Enable text wrap. |
Source code in src/pinky_snowpark/excel.py
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 | |
ParseResult
Bases: NamedTuple
Return value of :func:parse_worksheet — supports tuple unpacking.
Attributes:
| Name | Type | Description |
|---|---|---|
headers |
list[str]
|
Column names after normalization. |
rows |
list[dict[str, str | None]]
|
Row data — every cell value is cast to |
Source code in src/pinky_snowpark/excel.py
30 31 32 33 34 35 36 37 38 | |
create_workbook_from_df(df, sheet_name='Data', freeze_panes='A2', auto_fit=True, fixed_column_widths=None, wrap_columns=None, col_fmt=None, col_transform=None, wb=None, header_fmt=_DEFAULT_HEADER_FMT)
Create (or extend) an openpyxl Workbook from a Snowpark DataFrame.
Writes column headers from the DataFrame schema, then all data rows. Optionally applies: pane freeze, auto column widths, fixed widths, text wrap, per-column number formats and per-column value transforms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
'snowpark.DataFrame'
|
Source Snowpark DataFrame. |
required |
sheet_name
|
str
|
Sheet tab name (default |
'Data'
|
freeze_panes
|
str
|
Top-left cell of the frozen region (default |
'A2'
|
auto_fit
|
bool
|
Auto-size column widths from content (default |
True
|
fixed_column_widths
|
dict[str, int] | None
|
|
None
|
wrap_columns
|
list[int] | None
|
1-based column indices where text wrap is enabled. |
None
|
col_fmt
|
dict[int | str, str | CellFormat] | None
|
|
None
|
col_transform
|
dict[int | str, Callable[[Any], Any]] | None
|
|
None
|
wb
|
'Workbook | None'
|
Existing Workbook to add a sheet to ( |
None
|
header_fmt
|
CellFormat | None
|
Format applied to header row (default: bold 12pt). |
_DEFAULT_HEADER_FMT
|
Returns:
| Type | Description |
|---|---|
'Workbook'
|
openpyxl Workbook with the populated sheet. |
Source code in src/pinky_snowpark/excel.py
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 | |
parse_worksheet(ws, *, normalize_headers=True, deduplicate=True)
Parse an openpyxl Worksheet into a list of row dicts.
Skips leading empty rows to find the header row, then reads all subsequent non-empty rows as string values.
Typical use: download an XLSX from an API (INSEE Melodi, DGEFP…),
open with openpyxl.load_workbook(io.BytesIO(xlsx_bytes)), then
call this function on each sheet before building a Snowpark DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
'Worksheet'
|
openpyxl Worksheet object (read-only or normal). |
required |
normalize_headers
|
bool
|
Convert headers to UPPER_SNAKE_CASE using
|
True
|
deduplicate
|
bool
|
Append |
True
|
Returns:
| Type | Description |
|---|---|
ParseResult
|
Tuple |
ParseResult
|
column names and |
ParseResult
|
values cast to |
Example::
import io, openpyxl
from pinky_snowpark import parse_worksheet
wb = openpyxl.load_workbook(io.BytesIO(xlsx_bytes), read_only=True, data_only=True)
for sheet_name in wb.sheetnames:
headers, rows = parse_worksheet(wb[sheet_name])
# → build Snowpark DataFrame or stage_write(session, rows, ...)
Source code in src/pinky_snowpark/excel.py
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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |