Skip to main content

Semantic parser/writer for Skatteverket SRU files (BLANKETTER.SRU, INFO.SRU)

Project description

pysru-mab

A small Python library and CLI for working with SRU files — the file format used by the Swedish Tax Agency (Skatteverket) for filing corporate income tax declarations (INK2 and its appendices), as defined in the SKV 269 technical specification.

pysru-mab cn:

  • Parse BLANKETTER.SRU and INFO.SRU into typed Python objects.
  • Write them back out, with byte-identical round-trip for unmodified data.
  • Look up what a numeric field code (7011, 7104, ...) actually means, via a small built-in code table for INK2/INK2R/INK2S.
  • A pysru-mab CLI for converting SRU files to/from JSON.

Install

pip install pysru-mab

Quick start

from pysru_mab import parse_file, describe

sru_file = parse_file("BLANKETTER.SRU")  # defaults to cp850 encoding

for blankett in sru_file.blanketter:
    print(blankett.form_id, blankett.namn)

    for uppgift in blankett.uppgifter:
        info = describe(blankett.form_name, uppgift.code)
        label = info["label"] if info else "?"
        print(f"  {uppgift.code} ({label}): {uppgift.value}")

Access individual fields:

ink2 = sru_file.find("INK2")[0]
ink2.get("7104")        # -> "-67222" (or None if absent)
ink2.get_all("7104")    # -> all values for that code, in order
ink2["7104"]            # -> "-67222" (raises KeyError if absent)

Writing / round-trip

from pysru_mab import parse_file, write_file

sru_file = parse_file("BLANKETTER.SRU")
# ... modify sru_file.blanketter[...] ...
write_file(sru_file, "BLANKETTER_OUT.SRU")

write_file always emits CRLF line endings (as required by the SRU format), regardless of the input. Unmodified files round-trip byte-for-byte.

INFO.SRU

INFO.SRU uses a different, nested-section format and has its own parser/writer:

from pysru_mab import parse_info_file, write_info_file

info = parse_info_file("INFO.SRU")
print(info.orgnr, info.namn)

write_info_file(info, "INFO_OUT.SRU")

CLI usage

# Convert to JSON (optionally annotate fields with human-readable labels)
pysru-mab to-json BLANKETTER.SRU --describe -o blanketter.json

# Convert back to SRU
pysru-mab from-json blanketter.json -o BLANKETTER.SRU

# Look up what a field code means
pysru-mab describe INK2 7011
pysru-mab describe INK2          # list all known codes for a form

# INFO.SRU <-> JSON
pysru-mab info-to-json INFO.SRU -o info.json
pysru-mab info-from-json info.json -o INFO.SRU

If -o/--output is omitted, output goes to stdout.

Code tables

describe(form_name, code) looks up a field code against the bundled tables in pysru_mab/data/ (ink2.json, ink2r.json, ink2s.json). To add support for another form, drop in a new data/<form>.json file with the schema:

{
  "form": "INK2",
  "source": "SKV 269 (INK2, period 2025P4)",
  "codes": {
    "7011": {"label": "...", "description": "...", "field_type": "date"}
  }
}

field_type is one of "amount", "date", "checkbox", "text" (defaults to "amount"). You can also load a table from your own JSON file at runtime with load_custom_table(path).

Status: code tables are currently shipped for INK2, INK2R and INK2S covering the fields most commonly used. Labels are best-effort based on common SKV269/INK2 terminology — contributions and corrections against the official SKV269 appendix are welcome. Other forms parse and write correctly, but describe() returns None for them until a table is added.

Format notes

  • Encoding: SKV269 specifies CP850/IBM850. cp850 is the default for all read/write functions, but it's configurable via the encoding keyword argument.
  • Line endings: always CRLF (\r\n) on output.
  • Form IDs: #BLANKETT lines use IDs like INK2-2025P4, parsed into form_name="INK2", year=2025, period="P4".
  • Values: #UPPGIFT values are kept as raw strings to preserve sign, leading zeros, and the "X" checkbox marker; use Uppgift.as_int() / Uppgift.is_checkbox for typed access.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pysru_mab-0.3.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file pysru_mab-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pysru_mab-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pysru_mab-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a54c9e9507c50da06534af4d4c1cf105c59cd0528c2385795c7df6b692593c1f
MD5 dff25e25201b931277365adfdda3c9e7
BLAKE2b-256 b32447330e73cd4e20b63400ca09b5c06dd946840e228b6fce85dba0fef40776

See more details on using hashes here.

Provenance

The following attestation bundles were made for pysru_mab-0.3.0-py3-none-any.whl:

Publisher: pypi-publish.yml on monperrus/pysru-mab

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page