Spaces:
Sleeping
Sleeping
Update app.py
Browse files30 unmodified lines31323334353616 unmodified lines5354555657585910 unmodified lines7071727374757613 unmodified lines909192939495961 unmodified line989910010110210310410510610710810911011111211311411511611711811912012112212312412530 unmodified linestemp_ws = temp_wb.worksheets[0]out_wb = openpyxl.Workbook()out_ws = out_wb.active# 1. 預先複製欄寬for i in range(1, 31):16 unmodified linesrecords = []start_row_idx = row[0].row + 4for r_idx in range(start_row_idx, start_row_idx + 10):# 跳過原始檔 B 欄year_val = ws.cell(row=r_idx, column=1).valuedata_row = [ws.cell(row=r_idx, column=c).value for c in range(3, 22)]records.append([year_val] + data_row)10 unmodified linesfor c in range(1, 26):s_cell = temp_ws.cell(row=r, column=c)t_cell = out_ws.cell(row=target_row, column=c)if not (5 <= r <= 14): t_cell.value = s_cell.valueif s_cell.has_style:t_cell.font = copy(s_cell.font)t_cell.border = copy(s_cell.border)13 unmodified linesend_column=max_col)# 填入內容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):1 unmodified linecurrent_base_row += template_height# --- 關鍵修正:補空白列時也要同步範本列高 ---if len(file_funds_data) % 2 != 0:for r in range(1, template_height + 1):target_row = current_base_row + r - 1if r in temp_ws.row_dimensions:out_ws.row_dimensions[target_row].height = temp_ws.row_dimensions[r].height# 如果希望補位的空白頁也有表格框線,可以在此處加入複製 border 的代碼current_base_row += template_heightoutput_path = os.path.abspath("Final_Height_Sync_Report.xlsx")out_wb.save(output_path)return output_path, f"合併成功!已將高度設為 {template_height} 並同步空白列高。"except Exception as e:return None, f"發生錯誤: {str(e)}"# Gradio UI 保持不變with gr.Blocks() as app:gr.Markdown("# 基金報表合併系統 (列高完全同步版)")with gr.Row():f_in = gr.File(label="上傳 Excel", file_count="multiple")h_in = gr.Number(label="設定模板高度 (目前您是 15 列)", value=15)btn = gr.Button("🚀 開始一鍵生成", variant="primary")f_out = gr.File(label="下載結果")status = gr.Textbox(label="狀態")30 unmodified lines31323334353637383940414243444546474849505116 unmodified lines6869707172737410 unmodified lines8586878889909192939413 unmodified lines1081091101111121131141 unmodified line11611711811912012112212312412512612712812913013113213313413513613713813914014114230 unmodified linestemp_ws = temp_wb.worksheets[0]out_wb = openpyxl.Workbook()out_ws = out_wb.activeout_ws.title = "合併報表"# --- 頁面設置:水平方向與邊界調整 ---# 設定為水平 (Landscape)out_ws.page_setup.orientation = out_ws.ORIENTATION_LANDSCAPE# 設定邊界 (單位英吋): 2cm ≈ 0.787, 1cm ≈ 0.394out_ws.page_margins.top = 0.787 # 上 2cmout_ws.page_margins.bottom = 0.787 # 下 2cmout_ws.page_margins.left = 0.394 # 左 1cmout_ws.page_margins.right = 0.394 # 右 1cm# 強制每一頁寬度符合一頁 (避免欄位溢出)out_ws.page_setup.fitToWidth = 1out_ws.page_setup.fitToHeight = 0# 1. 預先複製欄寬for i in range(1, 31):16 unmodified linesrecords = []start_row_idx = row[0].row + 4for r_idx in range(start_row_idx, start_row_idx + 10):# 跳過原始檔 B 欄,讀取 A + C~U 欄year_val = ws.cell(row=r_idx, column=1).valuedata_row = [ws.cell(row=r_idx, column=c).value for c in range(3, 22)]records.append([year_val] + data_row)10 unmodified linesfor c in range(1, 26):s_cell = temp_ws.cell(row=r, column=c)t_cell = out_ws.cell(row=target_row, column=c)# 非數據區複製模板文字if not (5 <= r <= 14): t_cell.value = s_cell.valueif s_cell.has_style:t_cell.font = copy(s_cell.font)t_cell.border = copy(s_cell.border)13 unmodified linesend_column=max_col)# 填入內容 (標題與 10 年數據)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):1 unmodified linecurrent_base_row += template_height# 補空白列時同步範本列高if len(file_funds_data) % 2 != 0:for r in range(1, template_height + 1):target_row = current_base_row + r - 1if r in temp_ws.row_dimensions:out_ws.row_dimensions[target_row].height = temp_ws.row_dimensions[r].heightcurrent_base_row += template_heightoutput_path = os.path.abspath("Final_Merged_Report.xlsx")out_wb.save(output_path)return output_path, f"合併成功!範本高度 {template_height} 列,水平列印且邊界已設定。"except Exception as e:return None, f"發生錯誤: {str(e)}"# Gradio UIwith gr.Blocks() as app:gr.Markdown("# 基金報表合併系統 (15 列範本版)")with gr.Row():f_in = gr.File(label="上傳 Excel", file_count="multiple")h_in = gr.Number(label="設定模板高度 (已設為新範本 15)", value=15)btn = gr.Button("🚀 開始一鍵生成", variant="primary")f_out = gr.File(label="下載結果")status = gr.Textbox(label="狀態")
app.py+26-9