linexcel.insights¶
linexcel.insights ¶
Workbook context extraction and optional screenshot rendering.
WorkbookRenderError ¶
Bases: RuntimeError
Renderer failure retaining the failing tool's exit code, if available.
Source code in src/linexcel/insights.py
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
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 | |
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, with 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 by 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
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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
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
prepare_readonly_office_profile ¶
Create an isolated rendering profile with no trusted macro locations.
LibreOffice's very-high macro level permits only trusted locations. This disposable profile trusts none; document macros cannot be used to render or recalculate a workbook. It never changes the user's Office profile.
Source code in src/linexcel/insights.py
render_workbook_screenshots ¶
render_workbook_screenshots(
data: bytes | Path,
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, because an older build ignored the option or a page setup overrode 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
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 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |
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
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 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 | |