0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonで全銀フォーマット(固定長形式)の振込データファイルを作成する方法

Posted at

Pythonで全銀フォーマット(固定長形式)の振込データファイルを作成する方法

はじめに

GMOあおぞらネット銀行をはじめ、法人向けの総合振込では**全銀協フォーマット(固定長形式)**のファイルを用意する必要があります。

この形式は非常に厳密で、**1レコードあたり120バイト(半角文字)**に固定され、レコードの種別に応じて内容が決まっています。

本記事では、Pythonでこの全銀ファイル(固定長)を自動生成する方法を紹介します。


ファイル構成(全銀フォーマット)

全銀フォーマットの構成は以下の通りです。


ヘッダレコード       1行
データレコード       最大9999行
トレーラレコード     1行
エンドレコード       1行

各行は半角文字で120文字固定でなければなりません。


サンプルデータ(振込先情報)

まずはPythonで取り扱う辞書型のデータ例です。

transfer_data = [
    {
        "bank_code": "0005",               # 銀行コード(例:三菱UFJ)
        "branch_code": "123",              # 支店コード
        "account_type": "1",               # 預金種目(1:普通、2:当座)
        "account_number": "1234567",       # 口座番号
        "recipient_name": "YAMADATAROU",   # 受取人名(半角英字)
        "amount": 150000,                  # 振込金額
        "identifier": "",                  # 顧客コード
        "edi_info": "",                    # EDI情報(未使用なら空)
        "flag": " "                        # 'Y'=EDI情報, ' '=顧客コード
    },
    # 他のデータを必要に応じて追加
]

ヘッダレコード生成

def create_header_record():
    return (
        "1" +                              # データ区分
        "21" +                             # 種別コード
        "0" +                              # コード区分(0:JIS)
        " " * 10 +                         # 委託者コード(任意)
        "KAISHA TAROU".ljust(40) +         # 振込依頼人名
        "0706" +                           # 振込実施日(MMDD)
        "0310" +                           # 仕向銀行番号(GMO青空固定)
        " " * 15 +                         # 銀行名(任意)
        "123" +                            # 支店番号
        " " * 15 +                         # 支店名(任意)
        "1" +                              # 預金種目(1:普通)
        "1234567".zfill(7) +               # 口座番号(7桁)
        " " * 17                           # ダミー
    )

データレコード生成

def create_data_record(entry):
    return (
        "2" +
        entry["bank_code"].zfill(4) +
        " " * 15 +
        entry["branch_code"].zfill(3) +
        " " * 15 +
        " " * 4 +                                 # 統一手形交換所番号
        entry["account_type"] +
        entry["account_number"].zfill(7) +
        entry["recipient_name"].ljust(30) +
        str(entry["amount"]).zfill(10) +
        "0" +                                     # 新規コード
        entry["identifier"].ljust(10) +
        " " * 10 +
        "7" +                                     # 振込指定区分
        entry["flag"] +
        " " * 7
    )

トレーラ/エンドレコード生成

def create_trailer_record(total_count, total_amount):
    return (
        "8" +
        str(total_count).zfill(6) +
        str(total_amount).zfill(12) +
        " " * 101
    )

def create_end_record():
    return "9" + " " * 119

ファイル出力関数(Shift-JISで保存)

def write_zengin_file(data, filename="zengin.txt"):
    with open(filename, "w", encoding="shift_jis") as f:
        f.write(create_header_record() + "\n")
        total_amount = 0
        for entry in data:
            f.write(create_data_record(entry) + "\n")
            total_amount += entry["amount"]
        f.write(create_trailer_record(len(data), total_amount) + "\n")
        f.write(create_end_record() + "\n")

実行例

if __name__ == "__main__":
    write_zengin_file(transfer_data)

注意点(アップロード時エラー対策)

  • ファイルはShift-JISで保存する必要あり(UTF-8不可)
  • 全角文字は使用不可。半角英数字と一部記号のみ使用可能
  • 1行120文字(バイト)でなければならない
  • 口座番号は7桁にゼロ埋め
  • 半角スペースで桁埋めを適切に行う

まとめ

  • 全銀固定長ファイルはフォーマットが厳密だが、Pythonで自動化可能
  • 企業システムと銀行の連携をスムーズにするのに有効
  • 「固定長」「桁数」「Shift-JIS」を守ることが成功の鍵

今後の拡張(予定)

  • 全銀CSV形式対応
  • ファイルの検証ツール
  • Excelベースのデータ入力からファイル生成するツール化

参考資料

  • GMOあおぞらネット銀行「総合振込 アップロードファイル作成ガイド Ver 1.0.7」
  • 全国銀行協会 全銀EDIシステム仕様
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?