[docs]defOfficeFile(file):"""Return an office file object based on the format of given file. Args: file (:obj:`_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. """file.seek(0)# required by isOleFileifolefile.isOleFile(file):ole=olefile.OleFileIO(file)elifzipfile.is_zipfile(file):# Heuristicfrommsoffcrypto.format.ooxmlimportOOXMLFilereturnOOXMLFile(file)else:raiseexceptions.FileFormatError("Unsupported file format")# TODO: Make format specifiable by option in case of obstruction# Try this first; see https://github.com/nolze/msoffcrypto-tool/issues/17ifole.exists("EncryptionInfo"):frommsoffcrypto.format.ooxmlimportOOXMLFilereturnOOXMLFile(file)# MS-DOC: The WordDocument stream MUST be present in the file.# https://msdn.microsoft.com/en-us/library/dd926131(v=office.12).aspxelifole.exists("wordDocument"):frommsoffcrypto.format.doc97importDoc97FilereturnDoc97File(file)# MS-XLS: A file MUST contain exactly one Workbook Stream, ...# https://msdn.microsoft.com/en-us/library/dd911009(v=office.12).aspxelifole.exists("Workbook"):frommsoffcrypto.format.xls97importXls97FilereturnXls97File(file)# MS-PPT: A required stream whose name MUST be "PowerPoint Document".# https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-ppt/1fc22d56-28f9-4818-bd45-67c2bf721ccfelifole.exists("PowerPoint Document"):frommsoffcrypto.format.ppt97importPpt97FilereturnPpt97File(file)else:raiseexceptions.FileFormatError("Unrecognized file format")