Getting started¶
Install¶
The package needs Python 3.12 or newer and is installed from the Python Package Index, as a dependency of your program:
uv add sdb-reader
pip install sdb-reader
Sheets whose pages are pictures are read by text recognition, which needs the
program tesseract with German language data on the machine
(Installing).
The package is under the AGPL, version 3 or later, because the PDF library it is built on is. A program that uses it takes on the AGPL’s obligations.
What you import¶
Everything you need is exported by the package itself:
import sdb_reader
What is reached only through a submodule, such as sdb_reader.parser, is
internal and can change with any release
(What is promised).
A file to read¶
The examples need a file. This makes a small one; with a file of your own,
skip it and set path.
import tempfile
from pathlib import Path
import pymupdf
text = """Sicherheitsdatenblatt
gemäß Verordnung (EG) Nr. 1907/2006
Überarbeitet am: 12.03.2024 Version: 3.1
ABSCHNITT 1: Bezeichnung des Stoffs bzw. des Gemischs und des Unternehmens
1.1 Produktidentifikator
Handelsname: Musterreiniger
1.3 Einzelheiten zum Lieferanten, der das Sicherheitsdatenblatt bereitstellt
Muster GmbH
Musterweg 1
12345 Musterstadt
Telefon: +49 555 0142-0
E-Mail: info@muster.example
ABSCHNITT 2: Mögliche Gefahren
2.1 Einstufung des Stoffs oder Gemischs
Skin Irrit. 2, H315
2.2 Kennzeichnungselemente
Signalwort: Achtung
H315 Verursacht Hautreizungen.
P102 Darf nicht in die Hände von Kindern gelangen.
2.3 Sonstige Gefahren
Keine bekannt.
"""
path = Path(tempfile.mkdtemp()) / "sheet.pdf"
with pymupdf.open() as document:
document.new_page().insert_text((72, 72), text, fontsize=9)
document.save(path)
Read it¶
read_sheet reads the file, judges what it is and, for a German safety data
sheet, parses it. It takes the path and nothing else: the file is judged by
its content, and its name plays no part.
from sdb_reader import DocumentKind, read_sheet
reading = read_sheet(path)
assert reading.verification.kind is DocumentKind.SAFETY_DATA_SHEET
assert reading.parser_result is not None
sheet = reading.parser_result.sheet
assert sheet.product_identifier == "Musterreiniger"
assert sheet.hazard_statements.hazard_codes == ["H315"]
assert sheet.hazard_statements.signal_word == "Achtung"
read_sheet does not raise for a file it cannot read or that is something
else; the reading says so. A reading and its parts goes through
what comes back.