updated fx: build_excel_file to fix traceback error

This commit is contained in:
John Mulhall 2026-06-30 14:51:56 -04:00
parent 09601981ba
commit 363f0ec2d6

View file

@ -66,6 +66,9 @@ def parse_trial_balance_pdf(pdf_file):
- title - title
""" """
if hasattr(pdf_file, "seek"):
pdf_file.seek(0)
transactions = [] transactions = []
acct_summaries = [] acct_summaries = []
@ -233,68 +236,101 @@ def build_excel_file(df_txn, df_acct, title):
- bytes suitable for Streamlit download_button - 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: from openpyxl.styles import Font
df_txn.to_excel(writer, "Transactions", index=False, startrow=2) from openpyxl.worksheet.table import Table, TableStyleInfo
df_acct.to_excel(writer, "Account Summary", index=False, startrow=2) 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"]: if df_txn is None or df_txn.empty:
ws = wb[sheet_name] df_txn = pd.DataFrame(columns=txn_columns)
else:
df_txn = df_txn.reindex(columns=txn_columns)
ws["A1"] = title if df_acct is None or df_acct.empty:
ws.merge_cells( df_acct = pd.DataFrame(columns=acct_columns)
start_row=1, else:
start_column=1, df_acct = df_acct.reindex(columns=acct_columns)
end_row=1,
end_column=ws.max_column,
)
ws["A1"].font = Font(bold=True)
ws.freeze_panes = "A4"
header_row = 3 output = io.BytesIO()
if ws.max_row >= header_row and ws.max_column >= 1: with pd.ExcelWriter(output, engine="openpyxl") as writer:
end_col = get_column_letter(ws.max_column) df_txn.to_excel(writer, sheet_name="Transactions", index=False, startrow=2)
table_ref = f"A{header_row}:{end_col}{ws.max_row}" 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( ws["A1"] = title or "MEDITECH Detail Trial Balance"
name="TableStyleMedium9", ws.merge_cells(
showFirstColumn=False, start_row=1,
showLastColumn=False, start_column=1,
showRowStripes=True, end_row=1,
showColumnStripes=False, end_column=ws.max_column,
) )
ws["A1"].font = Font(bold=True)
ws.freeze_panes = "A4"
tab.tableStyleInfo = style header_row = 3
ws.add_table(tab)
for col in ws.columns: # Only add Excel table if there is at least one data row
max_length = 0 if ws.max_row > header_row:
col_letter = get_column_letter(col[0].column) end_col = get_column_letter(ws.max_column)
table_ref = f"A{header_row}:{end_col}{ws.max_row}"
for cell in col: table_name = sheet_name.replace(" ", "") + "Table"
try:
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: if cell.value:
max_length = max(max_length, len(str(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() output.seek(0)
wb.save(final_buffer) return output.getvalue()
final_buffer.seek(0)
return final_buffer.getvalue()
def parse_trial_balance_to_excel(pdf_file): def parse_trial_balance_to_excel(pdf_file):