如何检测文件格式并检查文件是否已加密
Contents
[
Hide
]
有时,在打开文件之前需要检测文件的格式,因为文件扩展名不能保证文件内容的正确性。文件可能已被加密(密码保护),因此不能直接读取,或者我们不应读取它。Aspose.Cells for Python via .NET 提供了FileFormatUtil.detect_file_format()静态方法及相关API,可用于处理文档。
以下示例代码说明如何通过文件路径检测文件格式并检查其扩展名。您还可以确定该文件是否已加密。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import FileFormatUtil | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Detect file format | |
info = FileFormatUtil.detect_file_format(dataDir + "Book1.xlsx") | |
# Gets the detected load format | |
print("The spreadsheet format is: " + FileFormatUtil.load_format_to_extension(info.load_format)) | |
# Check if the file is encrypted. | |
print("The file is encrypted: " + str(info.is_encrypted)) |