API Reference

class dni.DNI(potentiaL_dni_string: str, fix_issues: bool = False)

A DNI.

Parameters
  • potentiaL_dni_string – the string that may contain a DNI.

  • fix_issues – whether to fix check letter issues if found.

property number: str

Get the number part of the DNI.

Returns

the number.

property check_letter: str

Get the check letter of the DNI.

Returns

the check letter.

format(case: str = 'upper', separator: str = '') str

Get a string representation of the DNI, with some formatting options.

Parameters
  • case – whether to put the check letter in upper or lower case.

  • separator – an option seperator string to put between the number and the check letter.

Returns

a string with the DNI, formatted as defined.

classmethod random(quantity: int = 1) Union[dni.DNI, List[dni.DNI]]

Generate one or more random, valid DNI instances. Uniqueness is not guaranteed, so duplicates are extremely improbable, but possible.

Parameters

quantity – the number of DNI intances to generate.

Returns

the random DNI instances.

dni.is_valid(potential_dni_string: str) bool

Check if a string contains a valid DNI.

Parameters

potential_dni_string – the string that may contain a DNI.

Returns

True if so, False otherwise.

dni.has_check_letter(potential_dni_string: str) bool

Check if the DNI string has a check letter. Will raise an exception if the string does not, at least, match a DNI number. Does not check the validity of the check letter if one is found.

Parameters

potential_dni_string – the string that may contain a DNI.

Returns

True if so, False otherwise.

dni.compute_check_letter(dni_number: str) str

Given a DNI number, obtain the correct check letter.

Parameters

dni_number – a valid dni number.

Returns

the check letter for the number as an uppercase, single character string.

dni.add_or_fix_check_letter(dni_or_number_string: str) str

Add the right letter to a DNI number, or replace the existing one if it is not valid. DNIs without issues will return unchanged.

Parameters

dni_or_number_string – the string that contains a complete DNI or a DNI number without the check letter.

Returns

the DNI number with the valid check letter.

dni.text_contains_dni(text: str) bool

Check if a text contains or more DNI occurrences.

Parameters

text – a text that may contain some or no DNI-valid substrings.

Returns

True if so, False otherwise.

dni.extract_dnis_from_text(text: str) List[dni.DNI]

Find DNI-valid substrings in a text and generate DNI instances from them.

Parameters

text – a text that may contain some or no DNI-valid substrings.

Returns

a list with the found DNIs as instances of the DNI class.

exception dni.MissingCheckLetterException(exception_details: dni.exceptions.DNIExceptionDetails)

No check letter was found in the string.

exception dni.InvalidCheckLetterException(exception_details: dni.exceptions.DNIExceptionDetails)

The check letter does not match with the number.

exception dni.NoNumberFoundException(exception_details: dni.exceptions.DNIExceptionDetails)

No DNI number was found in the string.