Spaces:
Sleeping
Sleeping
File size: 6,017 Bytes
2eb873b d4fd873 2eb873b 4b2e2b7 1c004a9 2eb873b 4b2e2b7 1c004a9 4b2e2b7 d4fd873 1c67ebf 4b2e2b7 fcdb8b9 d4fd873 fcdb8b9 4b2e2b7 1c67ebf 4b2e2b7 1c004a9 4b2e2b7 1c67ebf 4b2e2b7 1c004a9 4b2e2b7 1c004a9 fcdb8b9 1c004a9 1c67ebf 1c004a9 1c67ebf 97a5450 87bd541 1c004a9 4b2e2b7 2eb873b 4b2e2b7 1c67ebf 4b2e2b7 1c67ebf fcdb8b9 1c67ebf 97a5450 1c67ebf 97a5450 fcdb8b9 97a5450 4b2e2b7 1c67ebf 4b2e2b7 1c67ebf 4b2e2b7 87bd541 2eb873b 4b2e2b7 1c67ebf 4b2e2b7 87bd541 4b2e2b7 1c67ebf 4b2e2b7 2eb873b 97a5450 4b2e2b7 87bd541 1c67ebf 4b2e2b7 1c67ebf 87bd541 2eb873b fcdb8b9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 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 | import gradio as gr
import openpyxl
import os
from openpyxl.utils import get_column_letter
from copy import copy
TEMPLATE_FILE = "template.xlsx"
def process_direct_merge(file_objs, template_height):
if not file_objs:
return None, "請先上傳檔案!"
try:
template_height = int(template_height)
except:
return None, "模板高度必須是數字!"
files = file_objs if isinstance(file_objs, list) else [file_objs]
# 1. 檔案排序 (OF- > DF- > SF- > PF-)
prefix_priority = {"OF-": 1, "DF-": 2, "SF-": 3, "PF-": 4}
def sort_key(f):
name = os.path.basename(f.name).upper()
for prefix, weight in prefix_priority.items():
if name.startswith(prefix):
return (weight, name)
return (99, name)
files.sort(key=sort_key)
if not os.path.exists(TEMPLATE_FILE):
return None, f"找不到 {TEMPLATE_FILE}"
try:
temp_wb = openpyxl.load_workbook(TEMPLATE_FILE)
temp_ws = temp_wb.worksheets[0]
out_wb = openpyxl.Workbook()
out_ws = out_wb.active
out_ws.title = "合併報表"
# 複製全域列寬
for i in range(1, 26):
col_l = get_column_letter(i)
if col_l in temp_ws.column_dimensions:
out_ws.column_dimensions[col_l].width = temp_ws.column_dimensions[col_l].width
current_base_row = 1
for file_obj in files:
wb = openpyxl.load_workbook(file_obj.name, data_only=True)
file_funds_data = []
for sheet_name in wb.sheetnames:
ws = wb[sheet_name]
for row in ws.iter_rows():
first_val = str(row[0].value) if row[0].value else ""
if "彙總表" in first_val or "近10年度" in first_val:
fund_name = first_val.replace("近10年度", "").replace("收支概況彙總表", "").replace("行政院國家發展基金", "").strip()
records = []
# 抓取數據 (假設標題下 4 列開始抓 10 列)
for r_offset in range(4, 14):
r_idx = row[0].row + r_offset
row_data = [ws.cell(row=r_idx, column=c).value for c in range(1, 21)]
records.append(row_data)
file_funds_data.append({"name": fund_name, "data": records})
for fund in file_funds_data:
# A. 複製模板每一格的樣式與內容
for r in range(1, template_height + 1):
for c in range(1, 26):
s_cell = temp_ws.cell(row=r, column=c)
t_row = current_base_row + r - 1
t_cell = out_ws.cell(row=t_row, column=c)
# 除了數據填入區,其餘複製模板內容
if not (4 <= r <= 14):
t_cell.value = s_cell.value
if s_cell.has_style:
t_cell.font = copy(s_cell.font)
t_cell.border = copy(s_cell.border)
t_cell.fill = copy(s_cell.fill)
t_cell.alignment = copy(s_cell.alignment)
t_cell.number_format = copy(s_cell.number_format)
# B. 強制複製合併儲存格資訊
if temp_ws.merged_cells:
for m_range in temp_ws.merged_cells.ranges:
# 只處理高度內的合併
if m_range.min_row <= template_height:
min_col, min_row, max_col, max_row = m_range.bounds
new_start_row = current_base_row + min_row - 1
new_end_row = current_base_row + max_row - 1
# 避開已合併區域,強制執行
try:
out_ws.merge_cells(
start_row=new_start_row,
start_column=min_col,
end_row=new_end_row,
end_column=max_col
)
except:
pass
# C. 填入動態資料 (標題與數據)
# 重新修正標題 (第一列)
out_ws.cell(row=current_base_row, column=1).value = f"{fund['name']}近10年度收支概況彙總表"
# 填入 10 年數據 (第 5 列開始)
for r_idx, rec_row in enumerate(fund['data']):
for c_idx, val in enumerate(rec_row):
out_ws.cell(row=current_base_row + 4 + r_idx, column=c_idx + 1).value = val
current_base_row += template_height
# 補白
if len(file_funds_data) % 2 != 0:
current_base_row += template_height
output_path = os.path.abspath("Final_Combined_Report.xlsx")
out_wb.save(output_path)
return output_path, "合併完成,已加強修復合併儲存格格式。"
except Exception as e:
return None, f"發生錯誤: {str(e)}"
with gr.Blocks() as app:
gr.Markdown("# 基金報表合併系統 (精準格式修復版)")
with gr.Row():
f_in = gr.File(label="上傳 Excel", file_count="multiple")
h_in = gr.Number(label="設定模板高度 (請嘗試 20 或 22)", value=20)
btn = gr.Button("開始一鍵合併", variant="primary")
f_out = gr.File(label="下載結果")
status = gr.Textbox(label="狀態")
btn.click(process_direct_merge, [f_in, h_in], [f_out, status])
if __name__ == "__main__":
app.launch(server_name="0.0.0.0", server_port=7860) |