msoffcrypto package

Submodules

msoffcrypto.exceptions module

exception msoffcrypto.exceptions.DecryptionError[source]

Bases: Exception

Raised when the file cannot be decrypted.

exception msoffcrypto.exceptions.FileFormatError[source]

Bases: Exception

Raised when the format of given file is unsupported or unrecognized.

exception msoffcrypto.exceptions.InvalidKeyError[source]

Bases: msoffcrypto.exceptions.DecryptionError

Raised when the given password or key is incorrect or cannot be verified.

exception msoffcrypto.exceptions.ParseError[source]

Bases: Exception

Raised when the file cannot be parsed correctly.

Module contents

msoffcrypto.OfficeFile(file)[source]

Return an office file object based on the format of given file.

Parameters:file (_io.BufferedReader) – Input file.
Returns:BaseOfficeFile object.

Examples

>>> with open("tests/inputs/example_password.docx", "rb") as f:
...     officefile = OfficeFile(f)
...     officefile.keyTypes
('password', 'private_key', 'secret_key')
>>> with open("tests/inputs/example_password.docx", "rb") as f:
...     officefile = OfficeFile(f)
...     officefile.load_key(password="Password1234_", verify_password=True)
>>> with open("README.md", "rb") as f:
...     officefile = OfficeFile(f)
Traceback (most recent call last):
    ...
msoffcrypto.exceptions.FileFormatError: ...
>>> with open("tests/inputs/example_password.docx", "rb") as f:
...     officefile = OfficeFile(f)
...     officefile.load_key(password="0000", verify_password=True)
Traceback (most recent call last):
    ...
msoffcrypto.exceptions.InvalidKeyError: ...

Given file handle will not be closed, the file position will most certainly change.