Skip to content

API reference

The four writers exported from bankstatementparser_writer_swift, generated from the docstrings by mkdocstrings.

bankstatementparser_writer_swift.writer

SWIFT MT940 Customer Statement & MT942 Interim Report Writer.

to_mt940(data, transaction_ref='NONREF', account_id=None, statement_number='00001/001', opening_balance=Decimal('0.00'), currency='EUR')

Serialise bank transactions into standard SWIFT MT940 Customer Statement format.

Parameters:

Name Type Description Default
data Sequence[Transaction] | DataFrame | Sequence[Mapping[str, Any]] | Any

Transactions as DataFrame, Transaction list, or dict records.

required
transaction_ref str

Tag :20: reference (default 'NONREF').

'NONREF'
account_id str | None

Tag :25: account number (inferred from records if None).

None
statement_number str

Tag :28C: statement/sequence number.

'00001/001'
opening_balance Decimal | float

Tag :60F: opening balance amount.

Decimal('0.00')
currency str

ISO 4217 Currency code.

'EUR'

Returns:

Type Description
str

Formatted SWIFT MT940 string.

Source code in bankstatementparser_writer_swift/writer.py
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
def to_mt940(
    data: (
        Sequence[Transaction]
        | pd.DataFrame
        | Sequence[Mapping[str, Any]]
        | Any
    ),
    transaction_ref: str = "NONREF",
    account_id: str | None = None,
    statement_number: str = "00001/001",
    opening_balance: Decimal | float = Decimal("0.00"),
    currency: str = "EUR",
) -> str:
    """Serialise bank transactions into standard SWIFT MT940 Customer Statement format.

    Args:
        data: Transactions as DataFrame, Transaction list, or dict records.
        transaction_ref: Tag :20: reference (default 'NONREF').
        account_id: Tag :25: account number (inferred from records if None).
        statement_number: Tag :28C: statement/sequence number.
        opening_balance: Tag :60F: opening balance amount.
        currency: ISO 4217 Currency code.

    Returns:
        Formatted SWIFT MT940 string.
    """
    records = _normalize_records(data)
    op_bal = _coerce_decimal(opening_balance)
    acct, curr = _infer_account_and_currency(records, account_id, currency)
    first_dt, last_dt = _date_bounds(records)
    lines = _header(transaction_ref, acct, statement_number)
    lines.append(_balance(":60F:", op_bal, first_dt, curr))
    running_bal = op_bal
    for rec in records:
        amt, entry = _entry(rec)
        running_bal += amt
        lines.extend(entry)
    lines.append(_balance(":62F:", running_bal, last_dt, curr))
    lines.append("-}")
    return "\n".join(lines) + "\n"

to_mt942(data, transaction_ref='NONREF', account_id=None, statement_number='00001/001', currency='EUR')

Serialise bank transactions into standard SWIFT MT942 Interim Transaction Report format.

Parameters:

Name Type Description Default
data Sequence[Transaction] | DataFrame | Sequence[Mapping[str, Any]] | Any

Transactions as DataFrame, Transaction list, or dict records.

required
transaction_ref str

Tag :20: reference.

'NONREF'
account_id str | None

Tag :25: account number.

None
statement_number str

Tag :28C: statement/sequence number.

'00001/001'
currency str

ISO 4217 Currency code.

'EUR'

Returns:

Type Description
str

Formatted SWIFT MT942 string.

Source code in bankstatementparser_writer_swift/writer.py
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
def to_mt942(
    data: (
        Sequence[Transaction]
        | pd.DataFrame
        | Sequence[Mapping[str, Any]]
        | Any
    ),
    transaction_ref: str = "NONREF",
    account_id: str | None = None,
    statement_number: str = "00001/001",
    currency: str = "EUR",
) -> str:
    """Serialise bank transactions into standard SWIFT MT942 Interim Transaction Report format.

    Args:
        data: Transactions as DataFrame, Transaction list, or dict records.
        transaction_ref: Tag :20: reference.
        account_id: Tag :25: account number.
        statement_number: Tag :28C: statement/sequence number.
        currency: ISO 4217 Currency code.

    Returns:
        Formatted SWIFT MT942 string.
    """
    records = _normalize_records(data)
    acct, curr = _infer_account_and_currency(records, account_id, currency)
    lines = _header(transaction_ref, acct, statement_number)
    lines.append(f":34F:{curr}0,00")
    lines.append(f":13D:{datetime.now().strftime('%y%m%d%H%M')}+0000")
    amounts = []
    for rec in records:
        amt, entry = _entry(rec)
        amounts.append(amt)
        lines.extend(entry)
    lines.extend(_summary(amounts, curr))
    lines.append("-}")
    return "\n".join(lines) + "\n"

write_mt940(data, destination, transaction_ref='NONREF', account_id=None, statement_number='00001/001', opening_balance=Decimal('0.00'), currency='EUR')

Write MT940 Customer Statement to a file on disk.

Parameters:

Name Type Description Default
data Sequence[Transaction] | DataFrame | Sequence[Mapping[str, Any]] | Any

Transactions input.

required
destination str | PathLike[str]

Filesystem destination path.

required
transaction_ref str

Tag :20: reference.

'NONREF'
account_id str | None

Tag :25: account number.

None
statement_number str

Tag :28C: statement/sequence number.

'00001/001'
opening_balance Decimal | float

Tag :60F: opening balance.

Decimal('0.00')
currency str

ISO Currency code.

'EUR'

Returns:

Type Description
Path

Path object of the written file.

Source code in bankstatementparser_writer_swift/writer.py
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
def write_mt940(
    data: (
        Sequence[Transaction]
        | pd.DataFrame
        | Sequence[Mapping[str, Any]]
        | Any
    ),
    destination: str | os.PathLike[str],
    transaction_ref: str = "NONREF",
    account_id: str | None = None,
    statement_number: str = "00001/001",
    opening_balance: Decimal | float = Decimal("0.00"),
    currency: str = "EUR",
) -> Path:
    """Write MT940 Customer Statement to a file on disk.

    Args:
        data: Transactions input.
        destination: Filesystem destination path.
        transaction_ref: Tag :20: reference.
        account_id: Tag :25: account number.
        statement_number: Tag :28C: statement/sequence number.
        opening_balance: Tag :60F: opening balance.
        currency: ISO Currency code.

    Returns:
        Path object of the written file.
    """
    content = to_mt940(
        data,
        transaction_ref=transaction_ref,
        account_id=account_id,
        statement_number=statement_number,
        opening_balance=opening_balance,
        currency=currency,
    )
    target = Path(destination)
    target.parent.mkdir(parents=True, exist_ok=True)
    target.write_text(content, encoding="utf-8")
    return target

write_mt942(data, destination, transaction_ref='NONREF', account_id=None, statement_number='00001/001', currency='EUR')

Write MT942 Interim Transaction Report to a file on disk.

Parameters:

Name Type Description Default
data Sequence[Transaction] | DataFrame | Sequence[Mapping[str, Any]] | Any

Transactions input.

required
destination str | PathLike[str]

Filesystem destination path.

required
transaction_ref str

Tag :20: reference.

'NONREF'
account_id str | None

Tag :25: account number.

None
statement_number str

Tag :28C: statement/sequence number.

'00001/001'
currency str

ISO Currency code.

'EUR'

Returns:

Type Description
Path

Path object of the written file.

Source code in bankstatementparser_writer_swift/writer.py
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
def write_mt942(
    data: (
        Sequence[Transaction]
        | pd.DataFrame
        | Sequence[Mapping[str, Any]]
        | Any
    ),
    destination: str | os.PathLike[str],
    transaction_ref: str = "NONREF",
    account_id: str | None = None,
    statement_number: str = "00001/001",
    currency: str = "EUR",
) -> Path:
    """Write MT942 Interim Transaction Report to a file on disk.

    Args:
        data: Transactions input.
        destination: Filesystem destination path.
        transaction_ref: Tag :20: reference.
        account_id: Tag :25: account number.
        statement_number: Tag :28C: statement/sequence number.
        currency: ISO Currency code.

    Returns:
        Path object of the written file.
    """
    content = to_mt942(
        data,
        transaction_ref=transaction_ref,
        account_id=account_id,
        statement_number=statement_number,
        currency=currency,
    )
    target = Path(destination)
    target.parent.mkdir(parents=True, exist_ok=True)
    target.write_text(content, encoding="utf-8")
    return target