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,20 +236,58 @@ 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",
]
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)
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)
output = io.BytesIO()
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)
wb = writer.book
for sheet_name in ["Transactions", "Account Summary"]: for sheet_name in ["Transactions", "Account Summary"]:
ws = wb[sheet_name] ws = wb[sheet_name]
ws["A1"] = title ws["A1"] = title or "MEDITECH Detail Trial Balance"
ws.merge_cells( ws.merge_cells(
start_row=1, start_row=1,
start_column=1, start_column=1,
@ -258,7 +299,8 @@ def build_excel_file(df_txn, df_acct, title):
header_row = 3 header_row = 3
if ws.max_row >= header_row and ws.max_column >= 1: # 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) end_col = get_column_letter(ws.max_column)
table_ref = f"A{header_row}:{end_col}{ws.max_row}" table_ref = f"A{header_row}:{end_col}{ws.max_row}"
@ -282,19 +324,13 @@ def build_excel_file(df_txn, df_acct, title):
col_letter = get_column_letter(col[0].column) col_letter = get_column_letter(col[0].column)
for cell in col: for cell in col:
try:
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):