Creating Excel tables with a neural network: structure, formulas and an XLSX file
The model describes the table as JSON, and an openpyxl script builds a finished XLSX with formulas, formats and totals. The schema and the code.
4 min · · автоматизация · development
The most common way to get a table from a neural network is to ask it to draw a table in a chat and copy it into Excel. That works for five rows; after that formulas get lost, numbers turn into text and column widths drift. It is more reliable to split the work: a neural network for creating Excel tables handles content and structure as JSON, and an ordinary script builds the finished XLSX file. Below are the scheme, working Python code and the cost.
Why not just ask the model for a file
A language model returns text. XLSX is an archive of XML, and you cannot "write" it as plain text. There are two ways to get a file:
- Ask the model to generate code (
openpyxl) and run it. Flexible, but executing generated code without review is unsafe. - Ask the model to return data and a description of the structure in a fixed JSON, and write the building code yourself once. This path is predictable: the model can do nothing except fill the schema, and you validate the JSON before building.
We cover the second option. It suits estimates, plans, price lists, vacation schedules and expense tracking: anywhere columns, formulas and totals matter.
JSON schema
Agree on the structure you request from the model:
{
"sheet_name": "Renovation estimate",
"columns": [
{"title": "Work", "type": "text", "width": 34},
{"title": "Quantity", "type": "int", "width": 14},
{"title": "Price", "type": "money", "width": 14},
{"title": "Amount", "type": "money", "width": 16}
],
"rows": [
["Wall putty", 40, 350, null],
["Ceiling painting", 18, 420, null]
],
"formulas": {"Amount": "=B{r}*C{r}"},
"totals": {"Amount": "SUM"}
}Schema rules: rows holds values in column order, with null for formula columns. In formulas the key is a column title and the value is a template with {r} in place of the row number. totals names the total function for a column.
The request to the model
The most expensive model is usually not needed for this job. The examples use gpt-6.1-sol, but you can take claude-sonnet-5-5 or deepseek-v4.1-flash: test on your own tables which one works for you. The code uses the OpenAI SDK with base URL https://api.zukko.pro/v1:
import json, os, re
from openai import OpenAI
client = OpenAI(base_url="https://api.zukko.pro/v1", api_key=os.environ["ZUKKO_API_KEY"])
FENCE = "`" * 3 # triple backtick the model may wrap the JSON in
SYSTEM = """You build tables for Excel. Reply with ONE JSON object only, no explanations and no markdown.
Schema: {"sheet_name": str, "columns": [{"title": str, "type": "text|int|money|percent|date", "width": int}],
"rows": [[...]], "formulas": {"<column title>": "=B{r}*C{r}"}, "totals": {"<column title>": "SUM|AVERAGE|MIN|MAX|COUNT"}}.
Rules: in rows put null for formula columns; dates as YYYY-MM-DD; in formulas use English function names
and a comma as the argument separator; {r} is replaced by the row number."""
def ask_spec(task: str, model: str = "gpt-6.1-sol") -> dict:
for attempt in range(3):We avoid special JSON modes because their support depends on the model; instead we strip possible wrappers and retry on a parse error. Other ways to get strict JSON: structured JSON response.
Building the XLSX with openpyxl
The openpyxl library installs with pip install openpyxl and can write values, formulas, formats and styles.
from datetime import date
from openpyxl import Workbook
from openpyxl.styles import Alignment, Font, PatternFill
from openpyxl.utils import get_column_letter
FORMATS = {"money": "#,##0.00", "int": "0", "percent": "0.0%", "date": "DD.MM.YYYY"}
ALLOWED_TOTALS = {"SUM", "AVERAGE", "MIN", "MAX", "COUNT"}
def build_xlsx(spec: dict, path: str) -> None:
cols = spec["columns"]
formulas = spec.get("formulas", {})
totals = spec.get("totals", {})
rows = spec["rows"]
What to know about formulas here:
- formulas are written to the file in English with commas between arguments (
=SUM(D2:D9)); Excel with a Russian locale will displayСУММand a semicolon by itself; openpyxldoes not calculate formulas: values appear once the file is opened in Excel or another spreadsheet editor; if you read the file withdata_only=Truebefore the first open, you getNoneinstead of numbers;- references like
B{r}depend on column order, so ask the model in the prompt to use exactly this substitution, and you can check column letters with a script.
If you need only formulas for an existing table rather than the whole table, see how a neural network writes Excel formulas. For analyzing finished data: analysis of CSV and tables.
Checks before opening the file
The model can make mistakes and the script will not notice them. What to check automatically:
- The number of values in each row does not exceed the number of columns.
- Numeric columns hold numbers, not strings such as
"1 200 RUB". - Every name in
formulasandtotalsmatches a column title. - Row sums agree with the total (after opening in Excel or recalculating in LibreOffice).
And do open the result by eye at least the first few times: an error in the logic of the estimate (say, a wrong unit of measure) does not go away because of nice formatting.
What it costs
We do not promise a free neural network for creating Excel tables: the openpyxl library is free, while the model request is paid. But it costs fractions of a cent. Calculated at prices on October 18, 2026: a request of 500 input tokens (schema and task) and 1,500 output tokens (the table JSON).
gpt-6.1-sol(0.27 / 1.32): 500 × 0.27 / 1,000,000 = $0.000135; 1,500 × 1.32 / 1,000,000 = $0.00198; total $0.002115, about $0.0021.deepseek-v4.1-flash(0.013 / 0.038): 500 × 0.013 / 1,000,000 = $0.0000065; 1,500 × 0.038 / 1,000,000 = $0.000057; total about $0.0000635.
Retries on a JSON error multiply this by the number of attempts. Current prices: GET /v1/models. More on SDK use: Python.
FAQ
Can a neural network create an XLSX file by itself?
The model returns text, so a script builds the file. The "model returns JSON, openpyxl builds the file" scheme works best.
Why do cells show only formulas and no values?
openpyxl writes formulas but does not calculate them. Values appear after the file is opened in Excel or LibreOffice.
How do I make a table with several sheets?
Add a sheets array to the schema with the same fields and call the builder for each sheet via wb.create_sheet(...). First check the single-sheet version.
Where do I start?
Create a key in the dashboard, set a small daily limit on it and run the example above on your own task.
zukko
Pay 25×
less
for Claude Code & ChatGPT
One fetch_* key and base URL api.zukko.pro — for Cursor, Claude Code, Codex, and any SDK.
Related articles
- Translating subtitles with a neural network: SRT without broken timecodesThe main risk when translating SRT with a neural network is a broken file structure. A scheme where timecodes never reach the model, and a Python script with validation.
- Editing texts with a neural network: proofreading, style and cuttingDifferent kinds of editing need different prompts. Wording for proofreading, style and cutting, how to get edits with explanations and how not to lose the author's voice.
- A neural network for writing scripts: Bash, PowerShell and PythonA model writes scripts for routine fast, but running them blindly is risky. How to state the task, demand a safe dry-run mode and check the result before launch.
- Dify in Docker: installation, first-run setup and connecting modelsInstall Dify with Docker Compose, create the admin account and connect Claude, GPT and DeepSeek models on one key. Plus what to do when containers will not start.