From 363f0ec2d6742f4ac38728515f8f349fa889f585 Mon Sep 17 00:00:00 2001 From: jmulhall00 Date: Tue, 30 Jun 2026 14:51:56 -0400 Subject: [PATCH] updated fx: build_excel_file to fix traceback error --- trial_balance_parser.py | 124 ++++++++++++++++++++++++++-------------- 1 file changed, 80 insertions(+), 44 deletions(-) diff --git a/trial_balance_parser.py b/trial_balance_parser.py index 6f28029..f457ba7 100644 --- a/trial_balance_parser.py +++ b/trial_balance_parser.py @@ -66,6 +66,9 @@ def parse_trial_balance_pdf(pdf_file): - title """ + if hasattr(pdf_file, "seek"): + pdf_file.seek(0) + transactions = [] acct_summaries = [] @@ -233,68 +236,101 @@ def build_excel_file(df_txn, df_acct, title): - bytes suitable for Streamlit download_button """ - initial_buffer = io.BytesIO() + import io + import pandas as pd + import openpyxl - with pd.ExcelWriter(initial_buffer, engine="openpyxl") as writer: - df_txn.to_excel(writer, "Transactions", index=False, startrow=2) - df_acct.to_excel(writer, "Account Summary", index=False, startrow=2) + from openpyxl.styles import Font + from openpyxl.worksheet.table import Table, TableStyleInfo + from openpyxl.utils import get_column_letter - initial_buffer.seek(0) + txn_columns = [ + "ACCOUNT_NAME", + "ACCOUNT", + "JOURNAL", + "DATE", + "BCH", + "ENTRY", + "DEBITS", + "CREDITS", + "DESCRIPTION", + ] - wb = openpyxl.load_workbook(initial_buffer) + acct_columns = [ + "ACCOUNT_NAME", + "ACCOUNT", + "OPEN", + "TOTAL_DEBITS", + "TOTAL_CREDITS", + "NET_CHANGE", + "CLOSE", + ] - for sheet_name in ["Transactions", "Account Summary"]: - ws = wb[sheet_name] + if df_txn is None or df_txn.empty: + df_txn = pd.DataFrame(columns=txn_columns) + else: + df_txn = df_txn.reindex(columns=txn_columns) - ws["A1"] = title - ws.merge_cells( - start_row=1, - start_column=1, - end_row=1, - end_column=ws.max_column, - ) - ws["A1"].font = Font(bold=True) - ws.freeze_panes = "A4" + if df_acct is None or df_acct.empty: + df_acct = pd.DataFrame(columns=acct_columns) + else: + df_acct = df_acct.reindex(columns=acct_columns) - header_row = 3 + output = io.BytesIO() - if ws.max_row >= header_row and ws.max_column >= 1: - end_col = get_column_letter(ws.max_column) - table_ref = f"A{header_row}:{end_col}{ws.max_row}" + with pd.ExcelWriter(output, engine="openpyxl") as writer: + df_txn.to_excel(writer, sheet_name="Transactions", index=False, startrow=2) + df_acct.to_excel(writer, sheet_name="Account Summary", index=False, startrow=2) - table_name = sheet_name.replace(" ", "") + "Table" + wb = writer.book - tab = Table(displayName=table_name, ref=table_ref) + for sheet_name in ["Transactions", "Account Summary"]: + ws = wb[sheet_name] - style = TableStyleInfo( - name="TableStyleMedium9", - showFirstColumn=False, - showLastColumn=False, - showRowStripes=True, - showColumnStripes=False, + ws["A1"] = title or "MEDITECH Detail Trial Balance" + ws.merge_cells( + start_row=1, + start_column=1, + end_row=1, + end_column=ws.max_column, ) + ws["A1"].font = Font(bold=True) + ws.freeze_panes = "A4" - tab.tableStyleInfo = style - ws.add_table(tab) + header_row = 3 - for col in ws.columns: - max_length = 0 - col_letter = get_column_letter(col[0].column) + # Only add Excel table if there is at least one data row + if ws.max_row > header_row: + end_col = get_column_letter(ws.max_column) + table_ref = f"A{header_row}:{end_col}{ws.max_row}" - for cell in col: - try: + table_name = sheet_name.replace(" ", "") + "Table" + + tab = Table(displayName=table_name, ref=table_ref) + + style = TableStyleInfo( + name="TableStyleMedium9", + showFirstColumn=False, + showLastColumn=False, + showRowStripes=True, + showColumnStripes=False, + ) + + tab.tableStyleInfo = style + ws.add_table(tab) + + for col in ws.columns: + max_length = 0 + col_letter = get_column_letter(col[0].column) + + for cell in col: if cell.value: max_length = max(max_length, len(str(cell.value))) - except Exception: - pass - ws.column_dimensions[col_letter].width = min(max_length + 2, 60) + ws.column_dimensions[col_letter].width = min(max_length + 2, 60) - final_buffer = io.BytesIO() - wb.save(final_buffer) - final_buffer.seek(0) - - return final_buffer.getvalue() + output.seek(0) + return output.getvalue() def parse_trial_balance_to_excel(pdf_file):