OFXParse

0.21 · active · verified Fri Apr 17

OFXParse (version 0.21) is a Python library designed for parsing and working with the Open Financial Exchange (OFX) file format. It extracts data such as transactions, account information, and balance statements from OFX files, making it easier to integrate financial data into applications. The library is actively maintained with a generally stable release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse a simple OFX file. It creates a temporary OFX file, parses it using `OfxParser.parse()`, and then prints out basic account and transaction information. Note the use of `"rb"` (binary read mode) for opening the file.

import os
from ofxparse import OfxParser

# Create a dummy OFX file for demonstration
ofx_content_simple = """
OFXHEADER:100
DATA:OFXSGML
AOFX:V1
<OFX>
    <BANKMSGSRQV1>
        <STMTTRNRS>
            <TRNUID>0</TRNUID>
            <STATUS><CODE>0</CODE><SEVERITY>INFO</SEVERITY></STATUS>
            <STMTRS>
                <CURDEF>USD</CURDEF>
                <BANKACCTFROM>
                    <BANKID>123456789</BANKID>
                    <ACCTID>0000000000</ACCTID>
                    <ACCTTYPE>CHECKING</ACCTTYPE>
                </BANKACCTFROM>
                <BANKTRANLIST>
                    <DTSTART>20230101000000</DTSTART>
                    <DTEND>20230131000000</DTEND>
                    <STMTTRN>
                        <TRNTYPE>DEBIT</TRNTYPE>
                        <DTPOSTED>20230105000000</DTPOSTED>
                        <TRNAMT>-50.00</TRNAMT>
                        <FITID>12345</FITID>
                        <NAME>Grocery Store</NAME>
                        <MEMO>Weekly groceries</MEMO>
                    </STMTTRN>
                    <STMTTRN>
                        <TRNTYPE>CREDIT</TRNTYPE>
                        <DTPOSTED>20230110000000</DTPOSTED>
                        <TRNAMT>1000.00</TRNAMT>
                        <FITID>67890</FITID>
                        <NAME>Payroll</NAME>
                        <MEMO>Monthly salary</MEMO>
                    </STMTTRN>
                </BANKTRANLIST>
                <LEDGERBAL>
                    <BALAMT>1500.00</BALAMT>
                    <DTASOF>20230131000000</DTASOF>
                </LEDGERBAL>
            </STMTRS>
        </STMTTRNRS>
    </BANKMSGSRQV1>
</OFX>
    """
    
file_path = "example.ofx"
with open(file_path, "w", encoding="utf-8") as f:
    f.write(ofx_content_simple)

try:
    # Always open OFX files in binary read mode ('rb')
    with open(file_path, "rb") as f:
        ofx = OfxParser.parse(f)

    if ofx.accounts:
        account = ofx.accounts[0]
        print(f"Bank ID: {account.bank_id}")
        print(f"Account ID: {account.account_id}")
        print(f"Account Type: {account.account_type}")
        
        if account.transactions:
            print("\nTransactions:")
            for transaction in account.transactions:
                print(f"  Date: {transaction.date.strftime('%Y-%m-%d')}, Amount: {transaction.amount}, Description: {getattr(transaction, 'memo', 'N/A')}")
        else:
            print("\nNo transactions found.")

        if account.ledger_balance:
            print(f"\nLedger Balance: {account.ledger_balance}")
        else:
            print("\nNo ledger balance found.")
    else:
        print("No accounts found in OFX file.")

except Exception as e:
    print(f"Error parsing OFX: {e}")
finally:
    # Clean up the dummy file
    if os.path.exists(file_path):
        os.remove(file_path)

view raw JSON →