linexcel.insights¶
linexcel.insights ¶
Workbook context extraction and optional screenshot rendering.
WorkbookRenderError ¶
extract_workbook_context ¶
extract_workbook_context(
data: bytes,
filename: str = "workbook.xlsx",
*,
preview_rows: int = PREVIEW_ROWS,
preview_columns: int = PREVIEW_COLUMNS,
) -> dict[str, Any]
Extract bounded presentation context without launching Excel.
The preview preserves the first cells as they are, without guessing which row is a header. Comments and sheet layout markers complement formula lineage with the cues users usually see when opening a workbook.
Source code in src/linexcel/insights.py
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 | |
detect_tables ¶
Detect Excel tables (TableObjects) and static tables on a worksheet.
Returns one dict per table with name, kind ("dynamic" or
"static"), ref, bounds (header_row, first_row, last_row,
first_col, last_col), headers and data_rows.
Dynamic tables come from ws.tables (what Excel calls a Table / List
Object). Static tables are ranges that look like a table — a header row
of text above contiguous data — detected heuristically so a workbook with
no formal tables still benefits from header/index enrichment.
Source code in src/linexcel/insights.py
static_tables_from_rows ¶
static_tables_from_rows(
rows: list[list[Any]],
covered: list[tuple[int, int, int, int]],
) -> list[dict[str, Any]]
The heuristic itself, over a top-left window of cell values.
Split out of :func:_detect_static_tables so the analyzer can run it on a
window read from the engine: openpyxl only reaches these values by parsing
the entire workbook, which costs seconds per million cells for a window of
at most STATIC_TABLE_SCAN_ROWS × STATIC_TABLE_SCAN_COLS.
Values are expected as openpyxl's data_only=False hands them over: a
formula cell as its =… text, a blank cell as None.
Source code in src/linexcel/insights.py
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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
find_libreoffice ¶
Locate the LibreOffice launcher, on PATH or in a standard install.
Windows and macOS installers do not put LibreOffice on PATH, so a
PATH-only lookup reports the renderer as missing on machines where it is
installed. Well-known install directories are therefore searched as well.
Source code in src/linexcel/insights.py
find_pdftoppm ¶
Locate Poppler's pdftoppm, on PATH or in a standard install.
Source code in src/linexcel/insights.py
render_workbook_screenshots ¶
render_workbook_screenshots(
data: bytes,
filename: str,
output_dir: str | Path,
*,
dpi: int = 144,
timeout: int = 180,
per_sheet: bool = True,
) -> dict[str, list[Path]] | list[Path]
Render workbook sheets to PNG with LibreOffice and Poppler.
Works on Linux, macOS and Windows. LibreOffice runs headlessly; no desktop
Excel process is needed. It exports the workbook to PDF, then pdftoppm
creates one PNG per rendered page.
With per_sheet — the default — LibreOffice is asked to put each sheet on
a single page, so the result is a {sheet name: [png]} mapping keyed by
the workbook's own sheet names, and the report shows each image under the
sheet it belongs to. Sheets are not split across pages, so a long one comes
out as one tall image rather than as print pages nobody can map back.
Setting per_sheet=False returns the flat list[Path] of print pages
instead, as the page setup of the workbook lays them out.
The mapping is only returned when LibreOffice produced exactly one page per sheet. When it did not — an older build ignoring the option, a page setup that overrides it — the flat page list is returned rather than a guessed mapping, because a screenshot filed under the wrong sheet is worse than one filed under none.
Source code in src/linexcel/insights.py
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | |
empty_sheet_render_exemptions ¶
Name only sheets proven empty enough to omit a rendered screenshot.
This is a conservative validation aid, not an estimate from dimensions or cached values. Chartsheets, styled cells, comments, drawings, relationships, unknown XML and oversized parts remain expected. Unsupported or malformed packages return no exemptions. No workbook cells are materialized.
Source code in src/linexcel/insights.py
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 | |