Spaces:
Sleeping
Sleeping
Update app.py
Browse files43 unmodified lines444546474748495013 unmodified lines64656667676869702 unmodified lines7374757676777879808182828384854 unmodified lines909192939495969798991001011021031041051061079394959697989910010110210310410510610710810911011111211311411111511611711811611912012112212312412212512612712812913012813113213013113213313413513613713843 unmodified lines out_ws = out_wb.active out_ws.title = "合併報表"
# 複製欄位寬度 # A. 複製全域欄位寬度 for i in range(1, 31): col_l = get_column_letter(i) if col_l in temp_ws.column_dimensions:13 unmodified lines if "彙總表" in first_val or "近10年度" in first_val: fund_name = first_val.replace("近10年度", "").replace("收支概況彙總表", "").strip() records = [] # 抓取 10 年數據 (假設從標題下 4 列開始) # 數據區通常在標題下 4 列 (第 5 列到 14 列) 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)]2 unmodified lines
# 寫入輸出表 for fund in file_funds_data: # A. 複製樣式與內容 # 1. 複製每一格的樣式與內容 for r in range(1, template_height + 1): for c in range(1, 25): s_cell = temp_ws.cell(row=r, column=c) t_cell = out_ws.cell(row=current_base_row + r - 1, column=c) # 只在非數據區填入模板文字 # 除了數據區外,其餘複製模板內容 if not (5 <= r <= 14): t_cell.value = s_cell.value 4 unmodified lines t_cell.alignment = copy(s_cell.alignment) t_cell.number_format = copy(s_cell.number_format)
# B. 安全複製合併儲存格 (修正錯誤點) if hasattr(temp_ws, "merged_cells"): for merged_range in list(temp_ws.merged_cells.ranges): # 確保合併範圍在當前模板高度內才處理 if merged_range.min_row <= template_height: try: # 建立偏移後的範圍 new_range = merged_range.shift(row_shift=current_base_row - 1) if new_range and hasattr(new_range, 'coord'): if new_range.coord not in out_ws.merged_cells: out_ws.merge_cells(new_range.coord) except Exception: continue # 忽略無法偏移的範圍
# C. 填入資料 # 2. 強制重新套用合併儲存格 (關鍵修正) for merged_range in temp_ws.merged_cells.ranges: # 只處理位於當前模板高度內的合併區域 if merged_range.min_row <= template_height: m_min_col, m_min_row, m_max_col, m_max_row = merged_range.bounds # 計算新表中的相對列位置 new_min_row = current_base_row + m_min_row - 1 new_max_row = current_base_row + m_max_row - 1 # 執行合併 out_ws.merge_cells( start_row=new_min_row, start_column=m_min_col, end_row=new_max_row, end_column=m_max_col )
# 3. 填入基金名稱與數據 out_ws.cell(row=current_base_row, column=1).value = f"{fund['name']}近10年度收支概況彙總表" 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
# 補白 # 4. 補白邏輯 (換檔跳頁) 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, "合併成功!" return output_path, "格式修復完成!請再次確認下載檔案。"
except Exception as e: return None, f"發生錯誤: {str(e)}"
with gr.Blocks() as app: gr.Markdown("# 基金報表合併系統 (修正合併錯誤版)") gr.Markdown("# 基金報表合併系統 (格式完全修��版)") with gr.Row(): f_in = gr.File(label="上傳 Excel", file_count="multiple") h_in = gr.Number(label="模板高度", value=20) btn = gr.Button("開始合併", variant="primary") f_in = gr.File(label="上傳原始 Excel", file_count="multiple") h_in = gr.Number(label="設定模板高度 (例如:20)", 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])app.py+29-26