Index
Build Excel formulas with Python operators.
Cell references are objects, so Python's own operators assemble the formula and a precedence-aware renderer parenthesises it correctly::
from pycountant.excel import cell, rng, Ref, F
rev, cost = cell("B3"), cell("B4")
ws["B5"] = rev - cost # =B3-B4
ws["B6"] = (rev - cost) / rev # =(B3-B4)/B3
ws["B7"] = rng("B3:B10").sum() * 1.2 # =SUM(B3:B10)*1.2
Or let Sheet hand you the reference as you write, so no coordinate is typed
twice::
s = Sheet(ws)
rev, cost = s.put("B2", 1000), s.put("B3", 600)
s.put("B4", (rev - cost) / rev) # =(B2-B3)/B2
Expressions are str subclasses whose value is the finished formula, so they can
be assigned straight to an openpyxl cell. Nothing here imports openpyxl.
BinOp
Bases: Expr
A binary operation between two expressions.
Source code in src/pycountant/excel/expr.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
CellRef
Bases: Expr
A single cell, e.g. B3, $B$3 or 'Source Data'!B3.
Source code in src/pycountant/excel/refs.py
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 | |
coordinate
property
The bare B3 coordinate, without sheet or $ markers.
abs()
Lock both row and column: $B$3.
Source code in src/pycountant/excel/refs.py
152 153 154 | |
abs_c()
Lock the column only: $B3.
Source code in src/pycountant/excel/refs.py
160 161 162 | |
abs_r()
Lock the row only: B$3.
Source code in src/pycountant/excel/refs.py
156 157 158 | |
bare()
Drop the sheet prefix, keeping the home sheet for later qualification.
Source code in src/pycountant/excel/refs.py
180 181 182 | |
offset(rows=0, cols=0)
A new reference moved by rows down and cols right.
Source code in src/pycountant/excel/refs.py
142 143 144 | |
on(sheet)
The same cell, qualified with another sheet.
Source code in src/pycountant/excel/refs.py
168 169 170 | |
qualified()
Qualify with the sheet this ref came from, for use in another sheet.
Source code in src/pycountant/excel/refs.py
172 173 174 175 176 177 178 | |
rel()
Drop all $ locking.
Source code in src/pycountant/excel/refs.py
164 165 166 | |
rel_text()
This cell's text without the sheet prefix, keeping any $ markers.
Source code in src/pycountant/excel/refs.py
118 119 120 | |
to(other)
The rectangular range spanning this cell and other.
Source code in src/pycountant/excel/refs.py
184 185 186 | |
Expr
Bases: str
Base class for every formula fragment.
The str value is the complete formula ("=B3-B4"); :attr:text is the
body ("B3-B4"). Composition always reads :attr:text.
Source code in src/pycountant/excel/expr.py
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 | |
formula
property
The complete formula as a plain str, including the leading =.
text
property
The formula body, without the leading =.
pct()
Postfix percent: x.pct() renders x%.
Source code in src/pycountant/excel/expr.py
158 159 160 | |
shift(rows=0, cols=0)
Return a copy with every relative reference moved by rows/cols.
Absolute ($-locked) components are left alone. Nodes without references
return themselves.
Source code in src/pycountant/excel/expr.py
98 99 100 101 102 103 104 | |
Func
Bases: Expr
A worksheet function call.
None renders as an omitted argument, so Func("VLOOKUP", a, b, c, None)
gives VLOOKUP(a,b,c,).
Source code in src/pycountant/excel/functions.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
Lit
Bases: Expr
A Python value rendered as an Excel literal.
Source code in src/pycountant/excel/expr.py
189 190 191 192 193 194 195 196 197 198 | |
RangeRef
Bases: Expr
A range: B3:B10, a whole column B:B, or a whole row 3:3.
Source code in src/pycountant/excel/refs.py
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 | |
coordinate
property
The bare B3:B10, without the sheet prefix.
bare()
Drop the sheet prefix, keeping the home sheet.
Source code in src/pycountant/excel/refs.py
230 231 232 | |
qualified()
Qualify with the sheet this range came from.
Source code in src/pycountant/excel/refs.py
222 223 224 225 226 227 228 | |
Raw
Bases: Expr
Verbatim formula text.
Parenthesised whenever it is nested, because its internal structure is unknown
and Raw("A1+B1") * 2 must not silently become A1+B1*2. Pass
atomic=True when the text is a single self-contained term.
Source code in src/pycountant/excel/expr.py
230 231 232 233 234 235 236 237 238 239 | |
Ref
A sheet-bound factory for references.
Takes an openpyxl worksheet (duck-typed on .title, so openpyxl is never
imported) or a plain sheet name::
data = Ref(ws)
data["B3"] # 'Data'!B3
data["A:A"] # 'Data'!A:A
data.cell(3, 2) # 'Data'!B3
Source code in src/pycountant/excel/refs.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 | |
Sheet
A write-and-remember wrapper around an openpyxl worksheet.
openpyxl is never imported: the worksheet is duck-typed on .title and
.cell(row=, column=, value=).
Source code in src/pycountant/excel/sheet.py
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 | |
cell(row, col, **kw)
A reference by position, without writing anything.
Source code in src/pycountant/excel/sheet.py
72 73 74 | |
column(ref_or_letter)
The whole column a reference sits in: s.column(rev) -> B:B.
Source code in src/pycountant/excel/sheet.py
80 81 82 83 | |
put(*args, number_format=None, **style)
Write a value and return a reference to the cell it landed in.
Called either as put("B2", value) or as put(row, col, value).
value may be an expression, in which case the formula is written.
number_format and any further keywords are applied to the openpyxl
cell as attributes, so font=, fill=, alignment= and border=
all pass straight through.
Source code in src/pycountant/excel/sheet.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
ref(key)
A reference into this sheet without writing anything.
Source code in src/pycountant/excel/sheet.py
66 67 68 | |
span(start, end)
The range between two references written on this sheet.
Source code in src/pycountant/excel/sheet.py
76 77 78 | |
UnaryOp
Bases: Expr
A prefix (-) or postfix (%) operation.
Source code in src/pycountant/excel/expr.py
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
cell(*args, sheet=None, home=None, abs_row=None, abs_col=None)
Build a :class:CellRef from "B3" or from (row, col).
cell("$B$3") picks up the $ markers; the abs_row/abs_col
keywords override whatever the string said.
Source code in src/pycountant/excel/refs.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | |
col_index(letters)
Column letters to a 1-based index: A -> 1, AA -> 27.
Source code in src/pycountant/excel/refs.py
52 53 54 55 56 57 58 59 60 61 | |
col_letter(index)
1-based column index to letters: 1 -> A, 27 -> AA.
Source code in src/pycountant/excel/refs.py
41 42 43 44 45 46 47 48 49 | |
lit(value)
Coerce a Python value into an :class:Expr, passing existing ones through.
Source code in src/pycountant/excel/expr.py
184 185 186 | |
rng(*args, sheet=None, home=None)
Build a :class:RangeRef from "B3:B10" or from two cell references.
Source code in src/pycountant/excel/refs.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |