name: 統計局_企業財務狀況表 description: | 根據資料登記表或月報檔案資料,生成統計局企業財務狀況表(F203表)。 推薦使用資料登記表方式(僅需2個檔案),也支援原始4檔案方式。 觸發條件:使用者提供資料登記表+模版,或月報xls檔案+模版時,按此流程生成財務狀況模板結果檔案。 agent_created: true
當金魚說"生成財務狀況表"、"財務狀況模板"、"填財務狀況",或者提供資料登記表/月報檔案要求生成財務狀況結果時,使用此技能。
使用者提供以下2個檔案: 1. 財務狀況資料登記表.xlsx(統一填寫所有原始資料,一家或多家公司) 2. 財務狀況模版.xlsx(模板)
使用者提供以下4個檔案:
1. 月報.xls(如 2026年5期月報_深圳卓正瑞祥管理諮詢有限公司_人民幣(元)_月度財務報表_2026061010160054.xls)
2. 財務狀況模版.xlsx(模板)
3. 多賬簿科目餘額表資料引出.xlsx(取應付職工薪酬和應交增值稅,需分別引出2211和2221.01科目)
4. 發薪人數累計平均數v2.xlsx(取平均用工人數)
資料登記表包含3個數據sheet + 1個說明sheet,使用者一次性填寫所有資料。
| 列 | 列名 | 說明 |
|---|---|---|
| A | 公司名稱 | 必須與其它sheet一致 |
| B | 固定資產原價 | 資產負債表-對外填報 期末數 |
| C | 應收賬款 | 資產負債表-對外填報 期末數 |
| D | 資產總計 | 資產負債表-對外填報 期末數 |
| E | 負債合計 | 資產負債表-對外填報 期末數 |
| F | 所有者權益合計 | 資產負債表-對外填報 期末數(股東權益合計) |
| 列 | 列名 | 說明 |
|---|---|---|
| A | 公司名稱 | 必須與其它sheet一致 |
| B | 主營業務收入 | 利潤表 本年累計金額 |
| C | 其他業務收入 | 利潤表 本年累計金額 |
| D | 主營業務成本 | 利潤表 本年累計金額 |
| E | 其他業務成本 | 利潤表 本年累計金額 |
| F | 營業稅金及附加 | 利潤表 本年累計金額 |
| G | 銷售費用 | 利潤表 本年累計金額 |
| H | 管理費用 | 利潤表 本年累計金額 |
| I | 研發費用 | 利潤表 本年累計金額 |
| J | 財務費用 | 利潤表 本年累計金額 |
| K | 資產減值損失 | 填原值,程式自動取反 |
| L | 信用減值損失 | 填原值,程式自動取反 |
| M | 公允價值變動收益 | 利潤表 本年累計金額 |
| N | 資產處置收益 | 利潤表 本年累計金額 |
| O | 投資收益 | 利潤表 本年累計金額 |
| P | 其他收益 | 利潤表 本年累計金額 |
| Q | 營業外收入 | 利潤表 本年累計金額 |
| R | 營業外支出 | 利潤表 本年累計金額 |
| S | 所得稅費用 | 利潤表 本年累計金額 |
| 列 | 列名 | 說明 |
|---|---|---|
| A | 公司名稱 | 必須與其它sheet一致 |
| B | 應付職工薪酬(本年累計貸方/元) | 科目餘額表2211科目 |
| C | 應交增值稅(本年累計貸方/元) | 科目餘額表2221.01科目(注意:是2221.01應交增值稅子科目,不是2221整個應交稅費) |
| D | 平均用工人數(人) | 用工人數統計表 |
xlrd 讀取訪問小蔥技能站7w4.net,解鎖更多實用的AI技能外掛。
load_fixed() 修復後讀取data_only=True 讀取| 模板欄位 | 資料登記表列 | 原始來源(關鍵詞匹配) |
|---|---|---|
| 固定資產原價 | 資產負債表 B列 | 資產負債表-對外填報 "固定資產原值/固定資產原價"行 期末數 |
| 應收賬款 | 資產負債表 C列 | 資產負債表-對外填報 "應收賬款"行 期末數 |
| 資產總計 | 資產負債表 D列 | 資產負債表-對外填報 "資產總計"行 期末數 |
| 負債合計 | 資產負債表 E列 | 資產負債表-對外填報 "負債合計"行 期末數 |
| 所有者權益合計 | 資產負債表 F列 | 資產負債表-對外填報 "股東權益合計/所有者權益合計"行 期末數 |
| 模板欄位 | 資料登記表列 | 計算說明 |
|---|---|---|
| 營業收入 | 利潤表 B+C | 主營+其他業務收入合計 |
| 淨服務收入 | = 營業收入 | 等於營業收入 |
| 營業成本 | 利潤表 D+E | 主營+其他業務成本合計 |
| 稅金及附加 | 利潤表 F列 | 直接取 |
| 銷售費用 | 利潤表 G列 | 直接取 |
| 管理費用 | 利潤表 H列 | 直接取 |
| 研發費用 | 利潤表 I列 | 直接取 |
| 財務費用 | 利潤表 J列 | 直接取 |
| 資產減值損失 | 利潤表 K列 | 取相反數 |
| 信用減值損失 | 利潤表 L列 | 取相反數 |
| 公允價值變動收益 | 利潤表 M列 | 直接取 |
| 資產處置收益 | 利潤表 N列 | 直接取 |
| 投資收益 | 利潤表 O列 | 直接取 |
| 淨敞口套期收益 | - | 填0 |
| 其他收益 | - | ⚠️ 固定填0,不取數 |
| 營業利潤 | 按公式計算 | 見下方公式(不含其他收益) |
| 營業外收入 | 利潤表 Q+P | 利潤表營業外收入 + 其他收益總額 |
| 營業外支出 | 利潤表 R列 | 直接取 |
| 利潤總額 | 按公式計算 | 見下方公式 |
| 所得稅費用 | 利潤表 S列 | 直接取 |
| 模板欄位 | 資料登記表列 | 說明 |
|---|---|---|
| 應付職工薪酬 | 科目餘額及用工 B列 | 2211科目本年累計貸方,元→千元 |
| 應交增值稅 | 科目餘額及用工 C列 | 2221.01應交增值稅子科目本年累計貸方,元→千元(⚠️ 不是2221整個應交稅費) |
| 模板欄位 | 資料登記表列 | 說明 |
|---|---|---|
| 平均用工人數 | 科目餘額及用工 D列 | 單位為人 |
營業利潤 = 營業收入 - 營業成本 - 稅金及附加 - 銷售費用 - 管理費用 - 研發費用 - 財務費用 + 資產減值損失 + 信用減值損失 + 公允價值變動收益 + 資產處置收益 + 投資收益 + 淨敞口套期收益
利潤總額 = 營業利潤 + 營業外收入 - 營業外支出
注意: - 以上公式中的各項均為千元值(轉換後),在千元級別做加減運算 - 其他收益(330)固定填0,不參與營業利潤計算 - 營業外收入 = 利潤表營業外收入 + 其他收益(其他收益的金額併入營業外收入)
round(val / 1000),四捨五入取整| 行 | 指標名稱 | 程式碼 |
|---|---|---|
| 11 | 一、期末資產負債 | - |
| 12 | 固定資產原價 | 209 |
| 13 | 應收賬款 | 202 |
| 14 | 資產總計 | 213 |
| 15 | 負債合計 | 217 |
| 16 | 所有者權益合計 | 218 |
| 17 | 二、損益 | - |
| 18 | 營業收入 | 301 |
| 19 | 其中:淨服務收入 | 340 |
| 20 | 營業成本 | 307 |
| 21 | 稅金及附加 | 309 |
| 22 | 銷售費用 | 312 |
| 23 | 管理費用 | 313 |
| 24 | 研發費用 | 331 |
| 25 | 財務費用 | 317 |
| 26 | 資產減值損失(損失以"-"號記) | 320 |
| 27 | 信用減值損失(損失以"-"號記) | 333 |
| 28 | 公允價值變動收益(損失以"-"號記) | 321 |
| 29 | 資產處置收益(損失以"-"號記) | 335 |
| 30 | 投資收益(損失以"-"號記) | 322 |
| 31 | 淨敞口套期收益(損失以"-"號記) | 334 |
| 32 | 其他收益 | 330 |
| 33 | 營業利潤 | 323 |
| 34 | 營業外收入 | 325 |
| 35 | 營業外支出 | 326 |
| 36 | 利潤總額 | 327 |
| 37 | 所得稅費用 | 328 |
| 38 | 三、成本費用及增值稅 | - |
| 39 | 應付職工薪酬(本年貸方累計發生額) | 401 |
| 40 | 應交增值稅(本年累計發生額) | 402 |
| 41 | 四、平均用工人數 | - |
| 41 | 平均用工人數 | 606 |
| 位置 | 公式 | 含義 | 標紅規則 |
|---|---|---|---|
| P16 | =H14-H15-H16 |
資產總計 - 負債合計 - 所有者權益合計 | 不為0時標紅 |
| P33 | =H18-H20-H21-H22-H23-H24-H25+H26+H27+H28+H29+H30+H31+H32-H33 |
營業利潤勾稽驗證 | 不為0時標紅 |
標紅樣式:紅色字型 + 粉色底色(#FFC7CE),使用 openpyxl 條件格式 CellIsRule。
檢查使用者提供的檔案: - 如果有「財務狀況資料登記表.xlsx」→ 使用方式一 - 如果有月報.xls → 使用方式二 - 必須有「財務狀況模版.xlsx」
import openpyxl
import shutil
import os
from openpyxl.styles import Font, PatternFill
from openpyxl.formatting.rule import CellIsRule
data_path = '財務狀況資料登記表.xlsx' # 使用者提供
template_path = '財務狀況模版.xlsx' # 使用者提供
wb_data = openpyxl.load_workbook(data_path, data_only=True)
ws_balance = wb_data['資產負債表']
ws_income = wb_data['利潤表']
ws_other = wb_data['科目餘額及用工']
# 收集所有公司名(以資產負債表sheet為準)
companies = []
for row in range(5, ws_balance.max_row + 1):
name = ws_balance.cell(row, 1).value
if name and str(name).strip():
companies.append((row, str(name).strip()))
def to_thousands(val):
"""元→千元,四捨五入取整"""
if val is None or val == 0:
return 0
return round(float(val) / 1000)
def safe_val(ws, row, col):
"""安全取值,空值返回0"""
v = ws.cell(row, col).value
return float(v) if v is not None else 0
# 利潤表和科目餘額sheet的公司名→行號對映
def build_row_map(ws, data_start_row):
"""建立公司名→行號的對映"""
m = {}
for r in range(data_start_row, ws.max_row + 1):
name = ws.cell(r, 1).value
if name and str(name).strip():
m[str(name).strip()] = r
return m
income_row_map = build_row_map(ws_income, 6) # 利潤表資料從第6行開始
other_row_map = build_row_map(ws_other, 6) # 科目餘額資料從第6行開始
# 校驗公式樣式
red_font = Font(color='FF0000', bold=True)
red_fill = PatternFill(start_color='FFC7CE', end_color='FFC7CE', fill_type='solid')
for bal_row, company_name in companies:
# --- 資產負債表 (資料在第5行起,A=名稱, B-F=數值) ---
fixed_assets = to_thousands(safe_val(ws_balance, bal_row, 2)) # B列
accounts_recv = to_thousands(safe_val(ws_balance, bal_row, 3)) # C列
total_assets = to_thousands(safe_val(ws_balance, bal_row, 4)) # D列
total_liabilities = to_thousands(safe_val(ws_balance, bal_row, 5)) # E列
equity = to_thousands(safe_val(ws_balance, bal_row, 6)) # F列
# --- 利潤表 (資料在第6行起,A=名稱, B-S=數值) ---
inc_row = income_row_map.get(company_name)
if not inc_row:
print(f"警告:公司 {company_name} 在利潤表sheet中未找到,跳過")
continue
main_revenue = safe_val(ws_income, inc_row, 2) # B列
other_revenue = safe_val(ws_income, inc_row, 3) # C列
main_cost = safe_val(ws_income, inc_row, 4) # D列
other_cost = safe_val(ws_income, inc_row, 5) # E列
tax_surcharge = safe_val(ws_income, inc_row, 6) # F列
selling_exp = safe_val(ws_income, inc_row, 7) # G列
admin_exp = safe_val(ws_income, inc_row, 8) # H列
rd_exp = safe_val(ws_income, inc_row, 9) # I列
finance_exp = safe_val(ws_income, inc_row, 10) # J列
asset_impair = safe_val(ws_income, inc_row, 11) # K列
credit_impair = safe_val(ws_income, inc_row, 12) # L列
fair_value = safe_val(ws_income, inc_row, 13) # M列
asset_disposal = safe_val(ws_income, inc_row, 14) # N列
invest_income = safe_val(ws_income, inc_row, 15) # O列
other_income = safe_val(ws_income, inc_row, 16) # P列
non_op_income = safe_val(ws_income, inc_row, 17) # Q列
non_op_expense = safe_val(ws_income, inc_row, 18) # R列
income_tax = safe_val(ws_income, inc_row, 19) # S列
# 轉千元
revenue = to_thousands(main_revenue + other_revenue)
net_service = revenue
cost = to_thousands(main_cost + other_cost)
tax_surcharge_k = to_thousands(tax_surcharge)
selling_exp_k = to_thousands(selling_exp)
admin_exp_k = to_thousands(admin_exp)
rd_exp_k = to_thousands(rd_exp)
finance_exp_k = to_thousands(finance_exp)
asset_impair_k = to_thousands(-asset_impair) # 相反數
credit_impair_k = to_thousands(-credit_impair) # 相反數
fair_value_k = to_thousands(fair_value)
asset_disposal_k = to_thousands(asset_disposal)
invest_income_k = to_thousands(invest_income)
net_hedge_k = 0
other_income_k = 0 # ⚠️ 其他收益固定填0,不取數
# 營業利潤(不含其他收益)
operating_profit = (revenue - cost - tax_surcharge_k - selling_exp_k - admin_exp_k
- rd_exp_k - finance_exp_k + asset_impair_k + credit_impair_k
+ fair_value_k + asset_disposal_k + invest_income_k + net_hedge_k)
# 營業外收入 = 利潤表營業外收入 + 其他收益
non_op_income_k = to_thousands(non_op_income + other_income)
non_op_expense_k = to_thousands(non_op_expense)
# 利潤總額
total_profit = operating_profit + non_op_income_k - non_op_expense_k
income_tax_k = to_thousands(income_tax)
# --- 科目餘額及用工 (資料在第6行起) ---
oth_row = other_row_map.get(company_name)
payroll_k = 0
tax_vat_k = 0
employee_count = 0
if oth_row:
payroll_k = to_thousands(safe_val(ws_other, oth_row, 2)) # B列
tax_vat_k = to_thousands(safe_val(ws_other, oth_row, 3)) # C列(2221.01應交增值稅)
emp_val = ws_other.cell(oth_row, 4).value # D列
employee_count = int(emp_val) if emp_val else 0
# --- 填入模板 ---
output_path = f'{company_name}.xlsx'
shutil.copy2(template_path, output_path)
wb_dst = openpyxl.load_workbook(output_path)
ws_dst = wb_dst['Sheet1']
fill_map = {
12: fixed_assets,
13: accounts_recv,
14: total_assets,
15: total_liabilities,
16: equity,
18: revenue,
19: net_service,
20: cost,
21: tax_surcharge_k,
22: selling_exp_k,
23: admin_exp_k,
24: rd_exp_k,
25: finance_exp_k,
26: asset_impair_k,
27: credit_impair_k,
28: fair_value_k,
29: asset_disposal_k,
30: invest_income_k,
31: net_hedge_k,
32: other_income_k, # 固定0
33: operating_profit,
34: non_op_income_k,
35: non_op_expense_k,
36: total_profit,
37: income_tax_k,
39: payroll_k, # 應付職工薪酬
40: tax_vat_k, # 應交增值稅(2221.01)
41: employee_count, # 平均用工人數
}
for row, val in fill_map.items():
ws_dst.cell(row, 8).value = val
# --- 校驗公式 ---
# P16: 資產總計 - 負債合計 - 所有者權益合計
ws_dst.cell(16, 16).value = '=H14-H15-H16'
ws_dst.cell(16, 16).number_format = '0'
ws_dst.conditional_formatting.add(
'P16',
CellIsRule(operator='notEqual', formula=['0'], font=red_font, fill=red_fill)
)
# P33: 營業利潤勾稽
ws_dst.cell(33, 16).value = '=H18-H20-H21-H22-H23-H24-H25+H26+H27+H28+H29+H30+H31+H32-H33'
ws_dst.cell(33, 16).number_format = '0'
ws_dst.conditional_formatting.add(
'P33',
CellIsRule(operator='notEqual', formula=['0'], font=red_font, fill=red_fill)
)
wb_dst.save(output_path)
print(f"已生成: {output_path}")
生成後驗證: - 讀取每個輸出檔案,確認每行H列值正確 - 對比利潤表原始營業利潤/利潤總額與計算值(千元級可能因四捨五入有±1差異) - 確認P16和P33校驗公式不為0的已標紅 - 確認檔案以公司名稱命名 - 如有多家公司,確認全部生成
import xlrd
import openpyxl
import shutil
import os
# 定位檔案
xls_path = '...' # 使用者提供
template_path = '財務狀況模版.xlsx'
wb_src = xlrd.open_workbook(xls_path)
# ⚠️ 按名稱查詢sheet,不要按索引(不同公司sheet數量/順序可能不同)
def find_sheet_by_name(wb, target_name):
for i in range(wb.nsheets):
ws = wb.sheet_by_index(i)
if ws.name.strip() == target_name.strip():
return ws
return None
ws_balance = find_sheet_by_name(wb_src, '資產負債表-對外填報')
ws_income = find_sheet_by_name(wb_src, '利潤表')
# 提取編制單位名稱
company_raw = ws_balance.cell(1, 0).value # "編制單位:XXX"
company_name = company_raw.replace('編制單位:', '').replace('編制單位:', '').strip()
def to_thousands(val):
"""元→千元,四捨五入取整"""
if val is None or val == 0:
return 0
return round(float(val) / 1000)
def safe_float(val):
"""安全轉換為float,空值返回0"""
if val is None or val == '':
return 0.0
try:
return float(val)
except (ValueError, TypeError):
return 0.0
# --- 資產負債表-對外填報(關鍵詞匹配取數)---
fixed_assets = 0
accounts_recv = 0
total_assets = 0
total_liabilities = 0
equity = 0
for r in range(ws_balance.nrows):
cell_a = str(ws_balance.cell(r, 0).value).strip()
cell_d = str(ws_balance.cell(r, 3).value).strip()
val_col1 = safe_float(ws_balance.cell(r, 1).value) # 左側(資產)期末數
val_col4 = safe_float(ws_balance.cell(r, 4).value) # 右側(負債及權益)期末數
if '固定資產原值' in cell_a or '固定資產原價' in cell_a:
fixed_assets = val_col1
elif '應收賬款' in cell_a and '壞賬' not in cell_a and '淨值' not in cell_a:
accounts_recv = val_col1
elif '資產總計' in cell_a:
total_assets = val_col1
elif '負債合計' in cell_d:
total_liabilities = val_col4
elif '股東權益合計' in cell_d or '所有者權益合計' in cell_d:
equity = val_col4
# --- 利潤表(關鍵詞匹配取數,本年累計金額 = col 2)---
main_revenue = 0
other_revenue = 0
main_cost = 0
other_cost = 0
tax_surcharge = 0
selling_exp = 0
admin_exp = 0
rd_exp = 0
finance_exp = 0
asset_impair = 0
credit_impair = 0
fair_value = 0
asset_disposal = 0
invest_income = 0
other_income = 0
non_op_income = 0
non_op_expense = 0
income_tax = 0
for r in range(ws_income.nrows):
cell_a = str(ws_income.cell(r, 0).value).strip()
val = safe_float(ws_income.cell(r, 2).value)
if '主營業務收入' in cell_a and '成本' not in cell_a and '利潤' not in cell_a:
main_revenue = val
elif '其他業務收入' in cell_a:
other_revenue = val
elif '主營業務成本' in cell_a:
main_cost = val
elif '其他業務成本' in cell_a:
other_cost = val
elif '營業稅金及附加' in cell_a or '稅金及附加' in cell_a:
tax_surcharge = val
elif '銷售費用' in cell_a:
selling_exp = val
elif '管理費用' in cell_a:
admin_exp = val
elif '研發費用' in cell_a:
rd_exp = val
elif '財務費用' in cell_a and '其中' not in cell_a and '利息' not in cell_a and '手續費' not in cell_a and '匯兌' not in cell_a and '使用權' not in cell_a and '關聯方' not in cell_a:
finance_exp = val
elif '資產減值損失' in cell_a:
asset_impair = val
elif '信用減值損失' in cell_a:
credit_impair = val
elif '公允價值變動收益' in cell_a:
fair_value = val
elif '資產處置損益' in cell_a or '資產處置收益' in cell_a:
asset_disposal = val
elif '投資收益' in cell_a and '聯營' not in cell_a and '合營' not in cell_a:
invest_income = val
elif '其他收益' in cell_a and '營業' not in cell_a:
other_income = val
elif '營業外收入' in cell_a:
non_op_income = val
elif '營業外支出' in cell_a and '非流動' not in cell_a:
non_op_expense = val
elif '所得稅費用' in cell_a and '當期' not in cell_a and '遞延' not in cell_a:
income_tax = val
# 轉千元
fixed_assets_k = to_thousands(fixed_assets)
accounts_recv_k = to_thousands(accounts_recv)
total_assets_k = to_thousands(total_assets)
total_liabilities_k = to_thousands(total_liabilities)
equity_k = to_thousands(equity)
revenue_k = to_thousands(main_revenue + other_revenue)
net_service_k = revenue_k
cost_k = to_thousands(main_cost + other_cost)
tax_surcharge_k = to_thousands(tax_surcharge)
selling_exp_k = to_thousands(selling_exp)
admin_exp_k = to_thousands(admin_exp)
rd_exp_k = to_thousands(rd_exp)
finance_exp_k = to_thousands(finance_exp)
asset_impair_k = to_thousands(-asset_impair)
credit_impair_k = to_thousands(-credit_impair)
fair_value_k = to_thousands(fair_value)
asset_disposal_k = to_thousands(asset_disposal)
invest_income_k = to_thousands(invest_income)
net_hedge_k = 0
other_income_k = 0 # ⚠️ 其他收益固定填0,不取數
# 營業利潤(不含其他收益)
operating_profit_k = (revenue_k - cost_k - tax_surcharge_k - selling_exp_k
- admin_exp_k - rd_exp_k - finance_exp_k
+ asset_impair_k + credit_impair_k
+ fair_value_k + asset_disposal_k + invest_income_k + net_hedge_k)
# 營業外收入 = 利潤表營業外收入 + 其他收益
non_op_income_k = to_thousands(non_op_income + other_income)
non_op_expense_k = to_thousands(non_op_expense)
# 利潤總額
total_profit_k = operating_profit_k + non_op_income_k - non_op_expense_k
income_tax_k = to_thousands(income_tax)
import zipfile, io
import re
from openpyxl import load_workbook
from openpyxl.styles import Font, PatternFill
from openpyxl.formatting.rule import CellIsRule
CASE_MAP = {
'[content_types].xml': '[Content_Types].xml',
'xl/sharedstrings.xml': 'xl/sharedStrings.xml',
}
def load_fixed(path):
"""修復金蝶匯出xlsx的大小寫問題"""
buf = io.BytesIO()
with zipfile.ZipFile(path, 'r') as zin:
with zipfile.ZipFile(buf, 'w') as zout:
for item in zin.namelist():
zout.writestr(CASE_MAP.get(item, item), zin.read(item))
buf.seek(0)
return load_workbook(buf, data_only=True)
def strip_code(level_str):
"""從'深圳卓正瑞祥管理諮詢有限公司(002)'中提取純公司名"""
if not level_str:
return ''
return re.sub(r'\([^)]*\)$', '', str(level_str)).strip()
# --- 讀取應付職工薪酬(2211)---
wb_2211 = load_fixed('多賬簿科目餘額表_2211.xlsx') # 2211應付職工薪酬
ws_2211 = wb_2211['Table1']
payroll_map = {}
current_section = None
for r in range(3, ws_2211.max_row + 1):
code = ws_2211.cell(r, 2).value
level = ws_2211.cell(r, 1).value
if code and str(code).strip() == '2211':
current_section = '2211'
continue
if not level or str(level).strip() == 'None' or '合計' in str(ws_2211.cell(r, 3).value or ''):
continue
company = strip_code(str(level))
ytd_credit = safe_float(ws_2211.cell(r, 9).value) # ⚠️ Col 9 = 本年累計(貸),不是Col 11
payroll_map[company] = ytd_credit
# --- 讀取應交增值稅(2221.01)---
wb_2221 = load_fixed('多賬簿科目餘額表_2221.01.xlsx') # ⚠️ 2221.01應交增值稅子科目
ws_2221 = wb_2221['Table1']
vat_map = {}
for r in range(4, ws_2221.max_row + 1): # 從第4行開始(跳過標題和合計行)
name_raw = ws_2221.cell(r, 1).value
if not name_raw or str(name_raw).strip() == 'None':
continue
company = strip_code(str(name_raw))
ytd_credit = safe_float(ws_2221.cell(r, 9).value) # ⚠️ Col 9 = 本年累計(貸)
vat_map[company] = ytd_credit
payroll_k = to_thousands(payroll_map.get(company_name, 0))
tax_vat_k = to_thousands(vat_map.get(company_name, 0))
# --- 讀取發薪人數 ---
wb_emp = openpyxl.load_workbook('發薪人數累計平均數v2.xlsx', data_only=True)
ws_emp = wb_emp['累計平均發薪人數']
employee_count = 0
for r in range(2, ws_emp.max_row + 1):
name = ws_emp.cell(r, 1).value
if name and str(name).strip() == company_name:
avg4 = ws_emp.cell(r, 9).value # I列 = 4月累計平均
employee_count = int(avg4) if avg4 else 0
break
output_path = f'{company_name}.xlsx'
shutil.copy2(template_path, output_path)
wb_dst = openpyxl.load_workbook(output_path)
ws_dst = wb_dst['Sheet1']
fill_map = {
12: fixed_assets_k,
13: accounts_recv_k,
14: total_assets_k,
15: total_liabilities_k,
16: equity_k,
18: revenue_k,
19: net_service_k,
20: cost_k,
21: tax_surcharge_k,
22: selling_exp_k,
23: admin_exp_k,
24: rd_exp_k,
25: finance_exp_k,
26: asset_impair_k,
27: credit_impair_k,
28: fair_value_k,
29: asset_disposal_k,
30: invest_income_k,
31: net_hedge_k,
32: other_income_k, # 固定0
33: operating_profit_k,
34: non_op_income_k,
35: non_op_expense_k,
36: total_profit_k,
37: income_tax_k,
39: payroll_k, # 應付職工薪酬(2211)
40: tax_vat_k, # 應交增值稅(2221.01)
41: employee_count, # 平均用工人數
}
for row, val in fill_map.items():
ws_dst.cell(row, 8).value = val
# --- 校驗公式 ---
red_font = Font(color='FF0000', bold=True)
red_fill = PatternFill(start_color='FFC7CE', end_color='FFC7CE', fill_type='solid')
# P16: 資產總計 - 負債合計 - 所有者權益合計
ws_dst.cell(16, 16).value = '=H14-H15-H16'
ws_dst.cell(16, 16).number_format = '0'
ws_dst.conditional_formatting.add(
'P16',
CellIsRule(operator='notEqual', formula=['0'], font=red_font, fill=red_fill)
)
# P33: 營業利潤勾稽
ws_dst.cell(33, 16).value = '=H18-H20-H21-H22-H23-H24-H25+H26+H27+H28+H29+H30+H31+H32-H33'
ws_dst.cell(33, 16).number_format = '0'
ws_dst.conditional_formatting.add(
'P33',
CellIsRule(operator='notEqual', formula=['0'], font=red_font, fill=red_fill)
)
wb_dst.save(output_path)
生成後驗證: - 讀取輸出檔案,確認每行H列值正確 - 對比利潤表原始營業利潤/利潤總額與計算值(千元級可能因四捨五入有±1差異) - 確認P16和P33校驗公式不為0的已標紅 - 確認檔案以編制單位命名
深圳卓正瑞祥管理諮詢有限公司.xlsx)load_fixed() 修復後再讀取這是一個高質量的財務表生成技能,能根據財務資料自動生成符合統計局要求的F203報表。優點是支援兩種輸入方式(簡單2檔案或多檔案),對易錯點有明確提醒,資料校驗機制完善。不足是文件較長,查詢特定資訊需要花費較多時間;部分專業術語較多,對非財務背景使用者可能造成理解障礙。總體而言,這是一個功能完整、業務細節考慮周到的實用技能。