Python + Claude API 实现香港薪俸税自动计算方案(2026/27新税率)
Python+Claude API实现香港2026/27薪俸税自动计算方案。完整代码含免税额配置(基本14.5万/已婚29万/子女14万/新生双倍28万)、MPF上限1.8万、累进税率表,自动取累进/标准税率低者。含Claude API自然语言输入版,支持JSON输出。
·

文章目录
背景
2026/27课税年度香港薪俸税免税额全面上调:基本免税额132,000→145,000、已婚264,000→290,000、子女每名130,000→140,000、新生子女双倍合计28万。本文用 Python + Claude API 写了一段自动计算代码,输入年收入/婚姻/子女信息即可算出应缴税额。
环境要求
- Python 3.8+
- anthropic >= 0.49.0
- Claude API Key
pip install anthropic
2026/27 免税额速查表
| 免税额类别 | 2025/26(旧) | 2026/27(新) | 增加 |
|---|---|---|---|
| 基本免税额 | 132,000 | 145,000 | +13,000 |
| 已婚人士 | 264,000 | 290,000 | +26,000 |
| 子女(每名) | 130,000 | 140,000 | +10,000 |
| 新生子女额外 | 130,000 | 140,000 | +10,000 |
| 供养父母(60岁+) | 50,000 | 55,000 | +5,000 |
新生子女合计:140,000(正常) + 140,000(出生年度额外) = 280,000
完整代码
import json
# 2026/27香港薪俸税税率 & 免税额
TAX_BRACKETS = [
(50000, 0.02), (50000, 0.06),
(50000, 0.10), (50000, 0.14),
(float('inf'), 0.17)
]
STANDARD_RATE = 0.15
MPF_CAP = 18000 # MPF年度供款上限
ALLOWANCES = {
"single": 145000,
"married": 290000,
"child": 140000,
"newborn_extra": 140000,
"parent_60plus": 55000
}
def calculate_tax(income, marital="single",
children=0, newborn=0, parents_60plus=0):
"""计算2026/27课税年度香港薪俸税"""
mpf = min(income * 0.05, MPF_CAP)
allowances = ALLOWANCES[marital]
allowances += children * ALLOWANCES["child"]
allowances += newborn * ALLOWANCES["newborn_extra"]
allowances += parents_60plus * ALLOWANCES["parent_60plus"]
taxable = max(0, income - allowances - mpf)
# 累进税率计算
remaining, progressive = taxable, 0
for bracket, rate in TAX_BRACKETS:
if remaining <= 0:
break
portion = min(remaining, bracket)
progressive += portion * rate
remaining -= portion
# 标准税率(15%)计算
standard = (income - mpf) * 0.15
return {
"income": income,
"mpf": mpf,
"allowances": allowances,
"taxable": taxable,
"progressive": round(progressive, 2),
"standard": round(standard, 2),
"final_tax": round(min(progressive, standard), 2)
}
# 三个典型场景测试
r1 = calculate_tax(500000) # 年薪50万单身
r2 = calculate_tax(800000, "married", children=1) # 年薪80万已婚1孩
r3 = calculate_tax(600000, children=1, newborn=1) # 年薪60万新生1孩
for r in [r1, r2, r3]:
print(json.dumps(r, indent=2, ensure_ascii=False))
print("---")
运行结果:
年薪50万单身 → 应缴 36,980.00
年薪80万已婚1孩 → 应缴 39,680.00
年薪60万新生1孩 → 应缴 13,590.00 ← 新生子女双倍28万免税额显著减税
三个场景的对比结论:
- 单身50万 vs 已婚80万+1孩:后者收入高60%但税款仅高7.3%,已婚+子女免税额效果显著
- 60万+新生子女场景:28万合计免税额让应缴税款降至1.36万,省税超2万
- 累进税率(2%-17%) vs 标准税率(15%):年收入低于约200万时累进税率更优
集成Claude API版
不想手动传参数?让Claude从自然语言描述中提取信息并计算:
import anthropic
client = anthropic.Anthropic(api_key="your-key")
def calc_with_claude(description):
prompt = f"""从描述提取税务信息,计算2026/27香港薪俸税:
{description}
规则:基本免税额145k/已婚290k/子女14万每名/出生额外14万/MPF 5%上限18k
输出JSON。"""
resp = client.messages.create(
model="claude-3-7-sonnet-20250219",
max_tokens=1024,
messages=[{"role": "user", "content": prompt}]
)
return resp.content[0].text
result = calc_with_claude("香港工作年薪60万,去年生了小孩")
print(result)
注意事项
- 仅适用于薪俸税,不涵盖利得税和物业税
- 免税额需在报税表中手动申报才生效,代码中配置的免税额不会自动生效
- 实际税款以税务局评税通知为准,本文代码供参考
- 2026/27新增:新生子女额外免税额申索期延长,去年出生未申报可补
参考
更多推荐


所有评论(0)