copybook

raw JSON →
1.0.16 verified Fri May 01 auth: no python

A Python library for parsing COBOL copybooks (data definition records). Current version 1.0.16. Development appears infrequent with no recent releases.

pip install copybook
error AttributeError: module 'copybook' has no attribute 'Copybook'
cause Trying to import Copybook directly without from syntax.
fix
Use 'from copybook import Copybook'.
error ParseError: Unexpected token
cause Copybook contains unsupported COBOL constructs like OCCURS or REDEFINES.
fix
Simplify the copybook to only elementary items with PIC clauses. The library does not fully support complex COBOL.
gotcha The parse method does not accept file paths; you must read the copybook file yourself and pass the content as a string.
fix Use open('copybook.cbl').read() to get the content, then pass it to parse.
gotcha Copybook layout indices are 1-based, not 0-based.
fix When using layout offsets, remember that the first byte is 1. Adjust if your downstream expects 0-based.

Parse a COBOL copybook string and get field positions.

from copybook import Copybook

cb = Copybook('')
cb.parse(
    struct="
        01 RECORD.
           05 NAME    PIC X(30).
           05 AGE     PIC 9(3).
    "
)
print(cb.layout)
# Example output: {'NAME': (1, 30), 'AGE': (31, 33)}