Fre*_*edG 5 python encryption excel xlrd
我有一个简短的程序,用于收集文件夹/子文件夹中所有 .xls 文件的列表,然后循环遍历文件列表,打开每个 xls 文档(尝试: book = xlrd.open_workbook(f) )以查找特定信息。如果引发异常,我会将文件名写入异常列表。我发现我有很多文件 xlrd 抛出错误:
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
book = xlrd.open_workbook(f)
File "C:\Python32\lib\site-packages\xlrd\__init__.py", line 435, in open_workbook
ragged_rows=ragged_rows,
File "C:\Python32\lib\site-packages\xlrd\book.py", line 116, in open_workbook_xls
bk.parse_globals()
File "C:\Python32\lib\site-packages\xlrd\book.py", line 1206, in parse_globals
self.handle_filepass(data)
File "C:\Python32\lib\site-packages\xlrd\book.py", line 924, in handle_filepass
raise XLRDError("Workbook is encrypted")
xlrd.biffh.XLRDError: Workbook is encrypted
Run Code Online (Sandbox Code Playgroud)
但我可以毫无问题地使用 Excel 打开文件。有谁知道为什么当文件似乎没有加密时 xlrd 会抛出加密错误?
谢谢,
弗雷德
我遇到了同样的问题,正如 @zindorsky 在他们的评论中提到的那样,当文件具有受保护的工作表时,或者由于 Excel 使用神奇密码静默加密文件的其他原因,可能会发生这种情况VelvetSweatshop。
XLRD 无法自行处理加密文件(事实上,自述文件将其列为“不可能完成”),但最近有另一个 Python 库可以解密各种 MS Office 文件(包括 .xls 文件) - msoff加密工具。
我能够使用它成功解决该问题 - 这是代码的缩写(且未经测试!)片段版本
import xlrd
import msoffcrypto
def handle_protected_workbook(wb_filepath):
try:
_book = xlrd.open_workbook(wb_filepath)
except xlrd.biffh.XLRDError, e:
if e.message == "Workbook is encrypted":
# Try and unencrypt workbook with magic password
wb_msoffcrypto_file = msoffcrypto.OfficeFile(open(wb_filepath, 'rb'))
try:
# Yes, this is actually a thing
# https://nakedsecurity.sophos.com/2013/04/11/password-excel-velvet-sweatshop/
wb_msoffcrypto_file.load_key(password='VelvetSweatshop')
except AssertionError, e:
if e.message == "Failed to verify password":
# Encrypted with some other password
raise # or do something else
else:
# Some other error occurred
raise
except:
# Some other error occurred
raise
else:
# Magic Excel password worked
assert wb_filepath.endswith('.xls')
wb_unencrypted_filename = wb_filepath[:-(len('.xls'))] + '__unencrypted.xls'
with tempfile.NamedTemporaryFile() as tmp_wb_unencrypted_file:
# Decrypt into the tempfile
wb_msoffcrypto_file.decrypt(tmp_wb_unencrypted_file)
# --- Do something with the file ---
# return true to indicate file was touched
return True # or do something else
else:
# some other xlrd error occurred.
return False # or do something else
except:
# some non-xlrd error occurred.
return False # or do something else
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7404 次 |
| 最近记录: |