Spaces:
Sleeping
Sleeping
Update app.py
Browse files26 unmodified lines27282930313233343511 unmodified lines474849505152539 unmodified lines63646566676869707172737475767778798081828384858687888990919293945 unmodified lines10010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313426 unmodified linesreturn (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]11 unmodified linescurrent_base_row = 1for file_obj in files:# 讀取原始資料,data_only=True 確保抓到計算後的值wb = openpyxl.load_workbook(file_obj.name, data_only=True)file_funds_data = []9 unmodified linesstart_row_idx = row[0].row + 4for r_idx in range(start_row_idx, start_row_idx + 10):# --- 核心邏輯:讀取時跳過 B 欄 (第2欄) ---# 抓取 A 欄 (年度)year_val = ws.cell(row=r_idx, column=1).value# 直接從 C 欄 (第3欄) 抓到第 21 欄data_row = [ws.cell(row=r_idx, column=c).value for c in range(3, 22)]# 合併成一筆記錄:[年度, 數據1, 數據2, ...]records.append([year_val] + data_row)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_cell = out_ws.cell(row=current_base_row + r - 1, column=c)if not (5 <= r <= 14): # 非數據區複製模板內容t_cell.value = s_cell.valueif s_cell.has_style:t_cell.font, t_cell.border = copy(s_cell.font), copy(s_cell.border)t_cell.fill, t_cell.alignment = copy(s_cell.fill), 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:5 unmodified linesend_column=max_col)# C. 填入數據 (從模板 B 欄開始填寫,即 column=2)out_ws.cell(row=current_base_row, column=1).value = f"{fund['name']}近10年度收支概況彙總表"for r_idx, row_vals in enumerate(fund['data']):for c_idx, val in enumerate(row_vals):# 填入位置:模板的 B 欄是第 2 欄,所以 c_idx + 2out_ws.cell(row=current_base_row + 4 + r_idx, column=c_idx + 2).value = valcurrent_base_row += template_height# D. 補白邏輯 (每個檔案結束,如果是奇數則留空)if len(file_funds_data) % 2 != 0:current_base_row += template_heightoutput_path = os.path.abspath("Merged_Report_Final.xlsx")out_wb.save(output_path)return output_path, "合併成功!已跳過原始檔 B 欄並正確對齊。"except Exception as e:return None, f"發生錯誤: {str(e)}"# Gradio 介面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)", value=20)btn = gr.Button("🚀 開始一鍵合併", variant="primary")f_out = gr.File(label="下載合併後 Excel")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)26 unmodified lines27282930313211 unmodified lines4445464748499 unmodified lines59606162636465666768697071727374757677787980818283848586878889909192939495965 unmodified lines10210310410510610710810911011111211311411511611711811912012112212326 unmodified linesreturn (99, name)files.sort(key=sort_key)try:temp_wb = openpyxl.load_workbook(TEMPLATE_FILE)temp_ws = temp_wb.worksheets[0]11 unmodified linescurrent_base_row = 1for file_obj in files:wb = openpyxl.load_workbook(file_obj.name, data_only=True)file_funds_data = []9 unmodified linesstart_row_idx = row[0].row + 4for r_idx in range(start_row_idx, start_row_idx + 10):# --- 核心修正:對應合併儲存格的偏移 ---# 假設原始檔 A, B 合併 (資料在 A),模板 A 是年度,B 是業務總收入# 我們讀取原始檔的 A 欄後,直接跳過原始檔的 B 欄,從 C 欄開始讀row_data = []# 1. 抓取原始檔 A 欄 (年度別)row_data.append(ws.cell(row=r_idx, column=1).value)# 2. 跳過原始檔 B 欄,從 C 欄 (第3欄) 開始抓取後續所有數據# 這樣抓出來的 row_data 就會是 [年度, 業務總收入, 業務總支出...]for c_idx in range(3, 22):row_data.append(ws.cell(row=r_idx, column=c_idx).value)records.append(row_data)file_funds_data.append({"name": fund_name, "data": records})# 寫入輸出檔for fund in file_funds_data:# 複製樣式與模板內容for r in range(1, template_height + 1):for c in range(1, 26):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.valueif s_cell.has_style:t_cell.font, t_cell.border = copy(s_cell.font), copy(s_cell.border)t_cell.fill, t_cell.alignment = copy(s_cell.fill), copy(s_cell.alignment)t_cell.number_format = copy(s_cell.number_format)# 套用合併格格式if temp_ws.merged_cells:for m_range in temp_ws.merged_cells.ranges:if m_range.min_row <= template_height:5 unmodified linesend_column=max_col)# 填入數據:從模板 A 欄 (column=1) 開始填入對正後的 row_dataout_ws.cell(row=current_base_row, column=1).value = f"{fund['name']}近10年度收支概況彙總表"for r_idx, row_vals in enumerate(fund['data']):for c_idx, val in enumerate(row_vals):# 寫入位置:模板 A 欄是 1,所以 c_idx + 1out_ws.cell(row=current_base_row + 4 + r_idx, column=c_idx + 1).value = valcurrent_base_row += template_heightif len(file_funds_data) % 2 != 0:current_base_row += template_heightoutput_path = os.path.abspath("Final_Corrected_Alignment.xlsx")out_wb.save(output_path)return output_path, "成功修復 A、B 欄合併導致的位移問題!"except Exception as e:return None, f"發生錯誤: {str(e)}"# Gradio UI 介面... (保持不變)
app.py+23-34