langchain-cellprose 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,74 @@
1
+ """LangChain loader: Excel workbooks as Cellprose documents.
2
+
3
+ from langchain_cellprose import CellproseLoader
4
+ docs = CellproseLoader("book.xlsx").load() # one Document for the workbook
5
+ docs = CellproseLoader("book.xlsx", mode="sheet").load() # one per sheet, for splitting and retrieval
6
+ """
7
+ from __future__ import annotations
8
+
9
+ from pathlib import Path
10
+ from typing import Any, Iterator, Literal
11
+
12
+ from langchain_core.document_loaders import BaseLoader
13
+ from langchain_core.documents import Document
14
+
15
+ __all__ = ["CellproseLoader", "split_sheets"]
16
+
17
+
18
+ class CellproseLoader(BaseLoader):
19
+ """Load a .xlsx/.xlsm (or an existing .sheet.md) as Cellprose text.
20
+
21
+ mode: "workbook" gives one Document; "sheet" gives one per sheet (plus one for defined names, if any), each
22
+ with the front matter so it reads alone. Extra keyword arguments go to `cellprose.dumps` (sheets, cell_range,
23
+ max_rows, recalc, legend). recalc defaults to "never": formulas without cached values print as `?`.
24
+ """
25
+
26
+ def __init__(self, file_path: str | Path, mode: Literal["workbook", "sheet"] = "workbook", **options: Any):
27
+ if mode not in ("workbook", "sheet"):
28
+ raise ValueError("mode must be 'workbook' or 'sheet'")
29
+ self.file_path = Path(file_path)
30
+ self.mode = mode
31
+ self.options = {"recalc": "never", **options}
32
+
33
+ def _text(self) -> str:
34
+ if self.file_path.name.endswith((".sheet.md", ".cellprose.md")):
35
+ return self.file_path.read_text(encoding="utf-8")
36
+ import cellprose
37
+ return cellprose.dumps(self.file_path, **self.options)
38
+
39
+ def lazy_load(self) -> Iterator[Document]:
40
+ text = self._text()
41
+ meta = {"source": str(self.file_path), "format": "cellprose"}
42
+ if self.mode == "workbook":
43
+ yield Document(page_content=text, metadata=meta)
44
+ return
45
+ for sheet, part in split_sheets(text):
46
+ yield Document(page_content=part, metadata={**meta, "sheet": sheet} if sheet is not None
47
+ else {**meta, "section": "names"})
48
+
49
+
50
+ def split_sheets(text: str) -> list[tuple[str | None, str]]:
51
+ """Split Cellprose text into (sheet name, text) parts, one per `# Sheet:` section. Each part keeps the front
52
+ matter so it reads on its own; the `# Names` section, when present, is the last part, with sheet name None."""
53
+ lines = text.split("\n")
54
+ start = 0
55
+ if lines and lines[0] == "---" and "---" in lines[1:]:
56
+ start = lines.index("---", 1) + 1
57
+ head = "\n".join(lines[:start]).strip("\n")
58
+ parts: list[tuple[str | None, list[str]]] = []
59
+ fenced = False
60
+ for line in lines[start:]:
61
+ if line.startswith("```"):
62
+ fenced = not fenced
63
+ if not fenced and line.startswith("# Sheet: "):
64
+ name = line[len("# Sheet: "):].strip()
65
+ if len(name) >= 2 and name[0] == name[-1] == '"':
66
+ name = name[1:-1]
67
+ parts.append((name, [line]))
68
+ elif not fenced and line == "# Names":
69
+ parts.append((None, [line]))
70
+ elif parts:
71
+ parts[-1][1].append(line)
72
+ if not parts:
73
+ return [(None, text)]
74
+ return [(n, (head + "\n\n" if head else "") + "\n".join(b).strip("\n") + "\n") for n, b in parts]
@@ -0,0 +1,35 @@
1
+ Metadata-Version: 2.5
2
+ Name: langchain-cellprose
3
+ Version: 0.1.0
4
+ Summary: LangChain document loader for Excel workbooks as Cellprose text (formulas, every sheet, cell addresses)
5
+ Project-URL: Homepage, https://cellprose.com
6
+ Project-URL: Documentation, https://cellprose.com/guide/
7
+ Project-URL: Repository, https://github.com/cellprose/cellprose
8
+ Project-URL: Issues, https://github.com/cellprose/cellprose/issues
9
+ Author: Utkarsh Gaikwad
10
+ License-Expression: Apache-2.0
11
+ Requires-Python: >=3.10
12
+ Requires-Dist: cellprose>=0.3
13
+ Requires-Dist: langchain-core>=0.3
14
+ Description-Content-Type: text/markdown
15
+
16
+ # langchain-cellprose
17
+
18
+ A LangChain document loader that reads `.xlsx` and `.xlsm` workbooks as [Cellprose](https://cellprose.com) text,
19
+ keeping formulas, cell addresses and every sheet in valid Markdown.
20
+
21
+ ```bash
22
+ pip install langchain-cellprose # not yet on PyPI: pip install -e integrations/langchain-cellprose from a clone
23
+ ```
24
+
25
+ ```python
26
+ from langchain_cellprose import CellproseLoader
27
+
28
+ docs = CellproseLoader("book.xlsx").load() # one Document for the whole workbook
29
+ docs = CellproseLoader("book.xlsx", mode="sheet").load() # one per sheet, metadata["sheet"] set
30
+ docs = CellproseLoader("book.xlsx", max_rows=200).load() # options pass to cellprose.dumps
31
+ ```
32
+
33
+ For question answering over a workbook, passing the whole workbook in one Document usually beats chunking: the
34
+ benchmark answers came from reading every sheet at once. Use `mode="sheet"` when the workbook is too large for
35
+ the context window.
@@ -0,0 +1,4 @@
1
+ langchain_cellprose/__init__.py,sha256=kfk-5K6GUW_G4ZnK0vKJXmSeM3nTmjIfR2d2ZiDF1s8,3282
2
+ langchain_cellprose-0.1.0.dist-info/METADATA,sha256=9vUhzUcIksdKH9i_9njX3KaPBRyxdbiAaPHd3BNHsl8,1520
3
+ langchain_cellprose-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
4
+ langchain_cellprose-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any