Skip to main content

sru-generator

CI PyPI Python 3.11+ License: MIT Not tax advice

Generate the SRU files (INFO.SRU + BLANKETTER.SRU) that Skatteverket's e-service imports for bilaga K4: sales of shares, funds and crypto. Pure Python, no dependencies.

Not tax advice. Not affiliated with Skatteverket. You are responsible for every figure you submit: check the imported K4 in Skatteverket's e-service before you sign. Provided as is, without warranty — see the disclaimer (also in Swedish) and the license.

Who it is for: people writing a tool that turns broker or exchange exports into a K4. Most such tools are tied to one broker and re-implement the same file details. This package is that shared part: page grouping, field codes, Skatteverket's rounding rules, page totals and the file encoding. You bring the figures, already in SEK; it writes the file.

På svenska: ett Python-bibliotek som skapar SRU-filer för bilaga K4 (försäljning av aktier, fonder och kryptovalutor) som kan importeras i Skatteverkets e-tjänst. Du lämnar beloppen i kronor; biblioteket sköter filformatet, avrundningen och summeringen. Inte skatterådgivning — du ansvarar själv för deklarationen.

Install

pip install sru-generator

Python 3.11 or newer.

Quick start

from sru_generator import build_blanketter_sru, build_info_sru, write_sru_file

me = {
    "personal_number": "198112189876",  # made-up example
    "full_name": "Test Person",
    "postal_code": "11122",
    "city_name": "Stockholm",
}
sales = [
    {
        "quantity": 100,
        "stock": "Ericsson B",
        "net value": 15000.75,  # försäljningspris, SEK
        "total net value of purchase": 14000.20,  # omkostnadsbelopp, SEK
    },
    {
        "quantity": 2.5,  # fractional shares are fine; written rounded up
        "stock": "Investor B",
        "net value": 2000,
        "total net value of purchase": 2600,
    },
]

write_sru_file("INFO.SRU", build_info_sru(me))
write_sru_file("BLANKETTER.SRU", build_blanketter_sru(sales, me, year=2025))

BLANKETTER.SRU then contains (the header's date and time will differ):

#BLANKETT K4-2025P4
#IDENTITET 198112189876 20260301 120000
#NAMN Test Person
#UPPGIFT 7014 1
#UPPGIFT 3100 100
#UPPGIFT 3101 Ericsson B
#UPPGIFT 3102 15000
#UPPGIFT 3103 14001
#UPPGIFT 3104 999
#UPPGIFT 3110 3
#UPPGIFT 3111 Investor B
#UPPGIFT 3112 2000
#UPPGIFT 3113 2600
#UPPGIFT 3115 600
#UPPGIFT 3300 17000
#UPPGIFT 3301 16601
#UPPGIFT 3304 999
#UPPGIFT 3305 600
#BLANKETTSLUT
#FIL_SLUT

Note the rounding: 15000.75 became 15000 and 14000.20 became 14001 (see Rounding). Up to nine sales go on each K4 page; more sales give more pages.

Then, in Skatteverket's Inkomstdeklaration 1 e-service, open Bilagor, choose K4, then Importera and select BLANKETTER.SRU (see Skatteverket's page Lämna en bilaga till deklarationen). INFO.SRU is needed when you send files through Skatteverket's Filöverföring service. Check every row and total in the e-service before you sign.

Command line

The same from a shell. Personal details can come from options, environment variables or a JSON profile; prefer the environment or a profile for the personnummer, because command-line arguments end up in shell history.

export SRU_PERSONAL_NUMBER=198112189876 SRU_FULL_NAME="Test Person"
sru-generator info --postal-code 11122 --city-name Stockholm   # writes INFO.SRU
sru-generator trades --data sales.json --year 2025             # writes BLANKETTER.SRU

# or keep everything in a profile file (git-ignored if named profile*.json):
sru-generator info --profile profile.json

sales.json is a JSON list of the same dictionaries as sales above. --year defaults to last year. Run sru-generator trades --help for the other options (crypto file, character conversion, output path, -v for log messages).

What the package does — and does not do

It does:

  • validate each sale and your personal details, with clear errors;
  • round each amount the way Skatteverket's K4 help text says (below);
  • derive vinst/förlust from the rounded amounts, so every row adds up;
  • split sales into pages of nine, number them, and write each page's totals;
  • write ISO-8859-1, refusing characters that encoding cannot hold instead of writing ?;
  • merge K4 section D pages from an existing crypto SRU file with the share pages.

It does not:

  • read broker or exchange exports;
  • convert foreign currency for you (helpers exist, but nothing converts automatically);
  • choose a cost-basis method (genomsnittsmetoden or schablonmetoden) or decide which K4 section an asset belongs in;
  • generate section D rows itself (it merges them from an existing file). Crypto belongs in section D, not in the share rows this package writes;
  • contact Skatteverket or anything else on the network.

Rounding

K4 amounts are whole kronor, and Skatteverket gives a different rule per column (help text for bilaga K4):

  • Försäljningspris: öre rounded down.
  • Omkostnadsbelopp: öre rounded up.
  • Vinst/förlust: försäljningspris minus omkostnadsbelopp, after rounding. It is never rounded on its own. A profit/loss you supply is only cross-checked against the unrounded difference (tolerance 1 kr) and a warning is logged if it differs; it is not written.

A float is first read at 15 significant digits, so its binary noise cannot move a figure by a krona: 0.1 * 3 * 1000 is 300.00000000000006, which would otherwise round up to an omkostnadsbelopp of 301. Decimal, int and str are exact and are used as given, so Decimal("100.001") still rounds up to 101. (A float with more than 15 significant digits, such as 1.999999999999999, cannot be told apart from noise and is read as 2.) Prefer Decimal or strings for money; the CLI reads JSON numbers as Decimal.

Page totals (3300, 3301, 3304, 3305) are the sums of the rows printed above them. Many other K4 tools round every amount to the nearest krona, so their figures can differ from this package's by 1 kr per row.

quantity (antal) may be fractional and is rounded up to a whole number, so a sale of 0.25 units shows 1, not 0: section A's antal is a whole number in Skatteverket's file description. Antal does not affect the gain or loss.

year must be a four-digit income year from 2000: K4-2025P4 declares income year 2025. items_per_group (rows per page) may be 1–9, the rows K4 section A has.

Input reference

A sale

Key Type Required Meaning Allowed
quantity number yes Units sold; fractions allowed, written rounded up 0 – 999,999,999,999
stock string yes Name of the security (Beteckning) 1–80 characters, no # or line breaks
net value number yes Försäljningspris in SEK, after fees 0 – 999,999,999,999
total net value of purchase number yes Omkostnadsbelopp in SEK, including fees 0 – 999,999,999,999
profit/loss number no Your own figure, cross-checked only
currency string no Validated only (see below); amounts must already be SEK one of the supported codes
exchange_rate number no Validated only 0.000001 – 1,000,000

Numbers may be int, float, Decimal or numeric strings; all arithmetic uses Decimal.

Personal details

Key Meaning Allowed
personal_number Personnummer with century, as Skatteverket requires: 198112189876 (a hyphen before the last four digits is accepted) 12 digits, correct check digit
full_name Name 1–100 characters, no # or line breaks
postal_code Postnummer 5 digits
city_name Postort 1–50 characters, no # or line breaks

Characters

SRU files are ISO-8859-1, so Swedish letters (å, ä, ö, é) are written as they are and need no conversion. Text the encoding cannot hold — Greek, Cyrillic, €, typographic quotes — makes write_sru_file (returns False) and encode_sru_content (raises DataFormatError) fail with a message naming the characters, instead of silently writing ?. Pass a character_converter (any str -> str function) to transliterate stock names.

Converters come from text-converters. Install it to use the named converters (greek, swedish, german, french, spanish):

pip install text-converters
from sru_generator import build_blanketter_sru, load_character_converter

greek = load_character_converter("greek")  # from text-converters when installed
content = build_blanketter_sru(sales, me, year=2025, character_converter=greek)

sru-generator still has no dependencies: text-converters is optional and imported only when a named converter is asked for ("none" never imports it). Without it, load_character_converter falls back to a copy bundled with sru-generator and warns (DeprecationWarning) that the copy will be removed in 2.0; the CLI's --character-conversion prints the same note on stderr. An outdated text-converters (one whose Greek converter does not give Thessaloniki for Θεσσαλονίκη) is not used.

greek follows the Greek standard ELOT 743 letter by letter (η is i, ου is ou, Θ/Χ/Ψ give Th/Ch/Ps, or TH/CH/PS in a word written in capitals) but not its other letter pairs: Ευαγγελισμός becomes Eyaggelismos, not Evangelismos. french writes œ/æ as oe/ae. The swedish converter strips å, ä and ö, which SRU files do not require.

For names in other scripts: convert only the characters ISO-8859-1 cannot hold. Apply the Greek converter first, then a transliteration library such as anyascii (ISC licence) for any other script:

from anyascii import anyascii
from text_converters import convert_greek_characters_to_english

from sru_generator import build_blanketter_sru


def to_latin1(text: str) -> str:
    """Transliterate only what ISO-8859-1 cannot hold; keep å, ä, ö as they are."""
    text = convert_greek_characters_to_english(text)
    return "".join(c if ord(c) <= 0xFF else anyascii(c) for c in text)


content = build_blanketter_sru(sales, me, year=2025, character_converter=to_latin1)
Input to_latin1 anyascii alone greek
Åsa Öberg Åsa Öberg Asa Oberg Åsa Öberg
Θεσσαλονίκη Χανιά Thessaloniki Chania Thessaloniki Chania Thessaloniki Chania
Παπαδόπουλος Papadopoulos Papadopoylos Papadopoulos
Москва Moskva Moskva Москва (still fails)
Ørsted € Ørsted EUR Orsted EUR Ørsted € (still fails)

Don't pass anyascii directly: it converts every non-ASCII character, so Swedish names would lose their å, ä and ö for no reason.

More features

Crypto pages (K4 section D)

If another tool produced a K4 SRU file with section D rows (for example from a crypto tax service), merge its pages with your share pages:

from sru_generator import build_blanketter_sru, read_crypto_sru_content

crypto_pages = read_crypto_sru_content("crypto_k4.sru")  # UTF-8 or ISO-8859-1
content = build_blanketter_sru(sales, me, year=2025, crypto_groups=crypto_pages)

With only crypto pages and no share sales, pass sales=[].

The pages are checked before they are merged: every line must be a section D field (#UPPGIFT 3410–3475, or the totals 3500, 3501, 3503, 3504), and the pages must be numbered 1..n. 7014 (the page number) may appear anywhere on a page; a page without it is numbered by its position. A file that cannot be read raises FileOperationError, an unreadable page number DataFormatError; a missing file gives an empty list and a warning.

Currency helpers

Nothing is converted automatically. If you want help converting before you build:

from sru_generator import convert_to_sek, set_exchange_rate

set_exchange_rate("USD", "SEK", 10.5)
sek = convert_to_sek(1500, "USD")  # Decimal("15750.00")

Supported codes: SEK, USD, EUR, GBP, NOK, DKK, CHF, JPY, CAD, AUD. Which rate to use (the transaction day's or an average) is a tax question; check Skatteverket's guidance.

Validation on its own

from sru_generator import ValidationError, validate_personal_info, validate_trade_data

try:
    rows = validate_trade_data(sales)
    person = validate_personal_info(me)
except ValidationError as e:
    print(f"{e.field}: {e}")

build_info_sru and build_blanketter_sru run these for you.

Configuration

SRUConfig / create_config(...) bundle the character converter, default currency and exchange rates for applications that want one object to pass around. The K4 rounding rule is not configurable. Several older fields (validation_level, rounding_mode, auto_convert_currency, log_level, custom_validators, …) are read by nothing; setting one raises a DeprecationWarning, and they will be removed in 2.0.

Logging

The package logs through the standard logging module under the name sru_generator and is silent unless your application configures logging, for example logging.basicConfig(level=logging.INFO). Messages can include stock names and amounts. The CLI shows warnings by default and more with -v.

API overview

  • Build whole files: build_info_sru, build_blanketter_sru, write_sru_file, encode_sru_content (bytes, for downloads or HTTP responses)
  • Lower level: generate_sru_info_content, generate_sru_trade_content (pages only, without the closing #FIL_SLUT), merge_sru_groups, read_crypto_sru_content, round_k4_sale_price, round_k4_cost_basis, round_k4_quantity
  • Validation: validate_trade_data, validate_personal_info, TradeDataValidator, PersonalInfoValidator, create_custom_validator
  • Characters and encoding: load_character_converter (text-converters, or the bundled copy until 2.0), encode_sru_text, decode_sru_bytes, SRU_ENCODING. get_character_converter, CHARACTER_CONVERTERS, no_conversion and the convert_*_characters_to_english functions at the top level are deprecated.
  • Currency: convert_to_sek, convert_currency, set_exchange_rate, CurrencyConverter
  • Configuration: SRUConfig, create_config, get_default_config
  • Exceptions (all subclass SRUGeneratorError): ValidationError, BusinessRuleError, DataFormatError, CurrencyError, ConfigurationError, FileOperationError

Privacy and security

SRU files hold your personnummer and your transactions. The package writes them readable by you only, never touches the network, and logs nothing unless asked. Never paste a real SRU file or personnummer into an issue. See SECURITY.md.

Broker-specific K4 tools that could build on a library like this include K4SRU (Java, several brokers), IbkrToSru (C#, IBKR) and K4Skatt (Python, IBKR). pysru-mab reads and writes general SRU files and targets the company forms (INK2).

Contributing, changelog, license

Metadata

Release files for sru-generator 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sru-generator 1.0.1
File Size Uploaded Provenance Actions
sru_generator-1.0.1.tar.gz 72.2 kB With provenance Details for sru_generator-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sru-generator 1.0.1
File Interpreter ABI Platform Actions
sru_generator-1.0.1-py3-none-any.whl Python 3 none any Details for sru_generator-1.0.1-py3-none-any.whl

Total release size: 116.4 kB

Release files / sru_generator-1.0.1.tar.gz

Download URL sru_generator-1.0.1.tar.gz
Size 72.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ddda85742415d27505c9d13c2fd570baf431cfafc3fbcba6dee86248b5a4fcd3
BLAKE2b-256 checksum
How to use checksums
ff4e530a03c68767957642d375ce31c9f9556f088afc3e4864814aed12955f49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / sru_generator-1.0.1-py3-none-any.whl

Download URL sru_generator-1.0.1-py3-none-any.whl
Size 44.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c3a76aaa3ad23cba68aa3aa68799338fb73903d968bf28088367a72805c66e51
BLAKE2b-256 checksum
How to use checksums
5ec51277b18ff57afa6ae91b45f4c1a7053e48f52cb7fc3491fc1f8cb7cc6f6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Security for sru-generator 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for sru_generator-1.0.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page