I've built budget.xlsx, but I haven't recalculated it. LibreOffice isn't installed here, so Excel will calculate the formulas when you open the file. The script that generates it is build_xlsx.py, in the same folder.
What's in it
- Inputs sheet: the assumptions are in yellow cells and labelled "Sample". They are monthly income 4,500, rent 1,400, food 600, transport 250, utilities 200 and a savings goal of 15%.
- Budget sheet:
- Categories: rent, food, transport and utilities. Each amount is a formula pointing at the Inputs sheet (for example
=Inputs!B5), and each has a "% of income" column. - Totals: a total spent row (
=SUM(B4:B7)), income, and the remaining amount (income minus total spent). - Savings check: it compares the remaining amount with income × savings goal %. It shows "On track - surplus of …" or "Short by …".
- Categories: rent, food, transport and utilities. Each amount is a formula pointing at the Inputs sheet (for example
- Chart: a bar chart of spending by category, titled as sample, next to the table.
What I checked
- I reopened the saved file and confirmed there are two sheets, one chart, and no hard-coded numbers on the Budget sheet. Every amount and total is a formula.
- I read each formula's cell references by hand and they point at the right inputs.
- Working it out by hand with the sample numbers, total spent is 2,450, remaining is 2,050, and the savings goal is 675. The check should therefore read "On track - surplus of 1,375.00".
Not checked
- No recalculation, so I haven't seen the computed values or confirmed the chart renders correctly.
- If a number looks off when you open it, tell me.
Your call: the savings goal isn't counted as a spending category. It's only compared against what's left over. I can add it as a category if you'd rather see it in the total and the chart.
Test run: a sample household budget built with openpyxl in a temporary folder. The run was allowed to write files and run python3 only. The formulas were inspected by re-opening the file and by hand calculation, but not recalculated, because LibreOffice was not installed. Excel calculates them on opening. All numbers are sample.
它做什么
编写并运行一个 Python 脚本来创建或修改工作簿,让它像真正的电子表格那样工作。计算用公式,引用带标签的输入表或命名区域,而不是把数字埋在公式里或粘贴成数值。它会设置表头格式、数字格式、列宽、冻结窗格、数据验证、条件格式和图表。修改已有文件时,先列出工作表、公式和命名区域,只改你要求的单元格,提醒 openpyxl 保存时可能丢失的内容(数据透视表、切片器、部分格式),并另存为新文件名。它也能审查一张表里的错误值、存成文本的数字、公式里写死的数字,以及没覆盖全部数据的区域。openpyxl 不能重新计算,所以它会说明公式是重新计算过,还是只检查过。
输出
.xlsx 文件、脚本、说明哪些算过哪些没算的核对报告,以及需要你确认的假设。
适合什么场景
根据数据生成或修复预算、跟踪表、报表工作簿,以及快速审查一张出现错误的表格。
中风险:纯指令文件,自身没有脚本,但它会编写并运行 Python,在你的工作目录里创建文件,也可能建议在虚拟环境里安装 openpyxl。它不会覆盖你的原文件,不创建宏,也不添加外部链接。已在临时目录用示例数字试用一次:生成了一个含公式和图表的两表预算;公式经过检查和手算核对,但因为没装 LibreOffice 没有重新计算,Excel 打开时会计算。不要把它用作财务或税务决策的依据。AIBars 原创(MIT)。